本文整理汇总了Python中tensorflow.initialize_all_variables方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.initialize_all_variables方法的具体用法?Python tensorflow.initialize_all_variables怎么用?Python tensorflow.initialize_all_variables使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.initialize_all_variables方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: restore_best_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def restore_best_model(self):
"""Load bestmodel file from eval directory, add variables for adagrad, and save to train directory"""
tf.logging.info("Restoring bestmodel for training...")
# Initialize all vars in the model
sess = tf.Session(config=util.get_config())
print("Initializing all variables...")
sess.run(tf.initialize_all_variables())
# Restore the best model from eval dir
saver = tf.train.Saver([v for v in tf.all_variables() if "Adagrad" not in v.name])
print("Restoring all non-adagrad variables from best model in eval dir...")
curr_ckpt = util.load_ckpt(saver, sess, "eval")
print("Restored %s." % curr_ckpt)
# Save this model to train dir and quit
new_model_name = curr_ckpt.split("/")[-1].replace("bestmodel", "model")
new_fname = os.path.join(FLAGS.log_root, "train", new_model_name)
print("Saving model to %s..." % (new_fname))
new_saver = tf.train.Saver() # this saver saves all variables that now exist, including Adagrad variables
new_saver.save(sess, new_fname)
print("Saved.")
exit()
示例2: test_lm
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def test_lm(self):
hps = get_test_hparams()
with tf.variable_scope("model"):
model = LM(hps)
with self.test_session() as sess:
tf.initialize_all_variables().run()
tf.initialize_local_variables().run()
loss = 1e5
for i in range(50):
x, y, w = simple_data_generator(hps.batch_size, hps.num_steps)
loss, _ = sess.run([model.loss, model.train_op], {model.x: x, model.y: y, model.w: w})
print("%d: %.3f %.3f" % (i, loss, np.exp(loss)))
if np.isnan(loss):
print("NaN detected")
break
self.assertLess(loss, 1.0)
示例3: build_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def build_model(self, sess):
self.init_opt()
sess.run(tf.initialize_all_variables())
if len(self.model_path) > 0:
print("Reading model parameters from %s" % self.model_path)
restore_vars = tf.all_variables()
# all_vars = tf.all_variables()
# restore_vars = [var for var in all_vars if
# var.name.startswith('g_') or
# var.name.startswith('d_')]
saver = tf.train.Saver(restore_vars)
saver.restore(sess, self.model_path)
istart = self.model_path.rfind('_') + 1
iend = self.model_path.rfind('.')
counter = self.model_path[istart:iend]
counter = int(counter)
else:
print("Created model with fresh parameters.")
counter = 0
return counter
示例4: build_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def build_model(self, sess):
self.init_opt()
sess.run(tf.initialize_all_variables())
if len(self.model_path) > 0:
print("Reading model parameters from %s" % self.model_path)
all_vars = tf.trainable_variables()
# all_vars = tf.all_variables()
restore_vars = []
for var in all_vars:
if var.name.startswith('g_') or var.name.startswith('d_'):
restore_vars.append(var)
# print(var.name)
saver = tf.train.Saver(restore_vars)
saver.restore(sess, self.model_path)
istart = self.model_path.rfind('_') + 1
iend = self.model_path.rfind('.')
counter = self.model_path[istart:iend]
counter = int(counter)
else:
print("Created model with fresh parameters.")
counter = 0
return counter
示例5: export
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def export(self, last_checkpoint, output_dir):
"""Builds a prediction graph and xports the model.
Args:
last_checkpoint: The latest checkpoint from training.
output_dir: Path to the folder to be used to output the model.
"""
logging.info('Exporting prediction graph to %s', output_dir)
with tf.Session(graph=tf.Graph()) as sess:
# Build and save prediction meta graph and trained variable values.
self.build_prediction_graph()
# Remove this if once Tensorflow 0.12 is standard.
try:
init_op = tf.global_variables_initializer()
except AttributeError:
init_op = tf.initialize_all_variables()
sess.run(init_op)
trained_saver = tf.train.Saver()
trained_saver.restore(sess, last_checkpoint)
saver = tf.train.Saver()
saver.export_meta_graph(filename=os.path.join(output_dir, 'export.meta'))
saver.save(
sess, os.path.join(output_dir, 'export'), write_meta_graph=False)
示例6: train
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def train(self):
self.train_op = self.optim.minimize(self.loss, global_step=self.global_step)
self.writer = tf.train.SummaryWriter("./logs/D_pretrained", self.sess.graph)
self.summary_op = tf.merge_all_summaries()
tf.initialize_all_variables().run()
self.saver = tf.train.Saver(var_list=self.D_params_dict, max_to_keep=self.max_to_keep)
count = 0
for idx in range(self.max_iter//3000):
self.save(self.checkpoint_dir, count)
self.evaluate('test', count)
self.evaluate('train', count)
for k in tqdm(range(3000)):
right_images, right_text, _ = self.dataset.sequential_sample(self.batch_size)
right_length = np.sum((right_text!=self.NOT)+0, 1)
fake_images, fake_text, _ = self.negative_dataset.sequential_sample(self.batch_size)
fake_length = np.sum((fake_text!=self.NOT)+0, 1)
wrong_text = self.dataset.get_wrong_text(self.batch_size)
wrong_length = np.sum((wrong_text!=self.NOT)+0, 1)
feed_dict = {self.right_images:right_images, self.right_text:right_text, self.right_length:right_length,
self.fake_images:fake_images, self.fake_text:fake_text, self.fake_length:fake_length,
self.wrong_images:right_images, self.wrong_text:wrong_text, self.wrong_length:wrong_length}
_, loss, summary_str = self.sess.run([self.train_op, self.loss, self.summary_op], feed_dict)
self.writer.add_summary(summary_str, count)
count += 1
示例7: main
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def main():
sys.stdout.write("start ptb")
raw_data = reader.ptb_raw_data("")
train_data, valid_data, test_data, word_to_id = raw_data
with tf.Graph().as_default(), tf.Session() as session:
initializer = tf.random_uniform_initializer(-0.04, 0.04)
with tf.variable_scope("model", reuse=None, initializer=initializer):
model = PTBModel()
saver = tf.train.Saver()
tf.initialize_all_variables().run()
model.train_writer = tf.train.SummaryWriter('./train', graph=session.graph)
for i in range(13):
sys.stdout.write("Epoch: %d\n" % (i + 1))
train_perplexity = model.train(session, train_data)
sys.stdout.write("Epoch: %d Train Perplexity: %.3f\n" % (i + 1, train_perplexity))
valid_perplexity = model.evaluate(session, valid_data)
sys.stdout.write("Epoch: %d Valid Perplexity: %.3f\n" % (i + 1, valid_perplexity))
test_perplexity = model.evaluate(session, test_data)
sys.stdout.write("Epoch: %d Test Perplexity: %.3f\n" % (i + 1, test_perplexity))
# model.predict(session, test_data, word_to_id)
saver.save(session, 'model.ckpt')
示例8: SplitApplyMerge
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def SplitApplyMerge(self):
# Repeatability. SGD has a tendency to jump around, even here.
tf.set_random_seed(1)
# Use sampling to train REINFORCE
with st.value_type(st.SampleAndReshapeValue(n=1)):
(route_selection,
routing_loss,
final_loss) = build_split_apply_merge_model()
sgd = tf.train.GradientDescentOptimizer(1.0).minimize(final_loss)
tf.initialize_all_variables().run()
for i in range(10):
# Run loss and inference step. This toy problem converges VERY quickly.
(routing_loss_v, final_loss_v, route_selection_v, _) = sess.run(
[routing_loss, final_loss, tf.identity(route_selection), sgd])
print(
"Iteration %d, routing loss: %s, final_loss: %s, "
"route selection: %s"
% (i, routing_loss_v, final_loss_v, route_selection_v))
示例9: main
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def main(argv=None):
X_train, Y_train, X_test, Y_test = gtsrb(FLAGS.train_dataset, FLAGS.test_dataset, labels_filename=FLAGS.labels)
print 'Loaded GTSRB data'
X_train = np.asarray(map(lambda x: pre_process_image(x), X_train.astype(np.uint8)),dtype=np.float32)
X_test = np.asarray(map(lambda x: pre_process_image(x), X_test.astype(np.uint8)),dtype=np.float32)
global total_iterations
global best_validation_accuracy
global last_improvement
global best_test_accuracy
global val_acc_list
global batch_acc_list
global test_acc_list
with tf.Session() as sess:
model = YadavModel()
sess.run(tf.initialize_all_variables())
#X_train, Y_train = gen_transformed_data(X_train,Y_train,43,10,30,5,5,1)
print(X_train.shape)
print(Y_train.shape)
optimize(sess, model, X_train, Y_train, X_test, Y_test, 10000, 128)
示例10: load_model
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def load_model(self, model_dir):
mappings_path = os.path.join(model_dir, 'mappings.pkl')
parameters_path = os.path.join(model_dir, 'parameters.pkl')
item2id, id2item, tag2id, id2tag, word2id, id2word = \
pickle.load(open(mappings_path, 'r'))
parameters = pickle.load(open(parameters_path))
self.item2id = item2id
self.id2item = id2item
self.tag2id = tag2id
self.id2tag = id2tag
self.word2id = word2id
self.id2word = id2word
self.parameters = parameters
print(parameters)
print('Building input graph...', end='')
self.build_graph()
print('Finished.')
print('Initializing variables...', end='')
init_op = tf.initialize_all_variables()
self.sess.run(init_op)
print('Finished.')
print('Reloading parameters...', end='')
saver = tf.train.Saver(tf.global_variables())
checkpoint = tf.train.latest_checkpoint(model_dir)
saver.restore(self.sess, checkpoint)
print('Finished.')
示例11: __prepare_train
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def __prepare_train(self, session):
self.summary_writer = tf.train.SummaryWriter('logs', graph_def=session.graph_def)
session.run(tf.initialize_all_variables())
示例12: test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension(self):
with self.test_session() as sess:
batch_size = 3
num_batches = 2
examples = tf.Variable(tf.constant(2, dtype=tf.int32))
counter = examples.count_up_to(num_batches * batch_size + 2)
boxes = tf.tile(
tf.reshape(tf.range(4), [1, 4]), tf.stack([counter, tf.constant(1)]))
batch_queue = batcher.BatchQueue(
tensor_dict={'boxes': boxes},
batch_size=batch_size,
batch_queue_capacity=100,
num_batch_queue_threads=1,
prefetch_queue_capacity=100)
batch = batch_queue.dequeue()
for tensor_dict in batch:
for tensor in tensor_dict.values():
self.assertAllEqual([None, 4], tensor.get_shape().as_list())
tf.initialize_all_variables().run()
with slim.queues.QueueRunners(sess):
i = 2
for _ in range(num_batches):
batch_np = sess.run(batch)
for tensor_dict in batch_np:
for tensor in tensor_dict.values():
self.assertAllEqual(tensor, np.tile(np.arange(4), (i, 1)))
i += 1
with self.assertRaises(tf.errors.OutOfRangeError):
sess.run(batch)
示例13: test_batch_and_unpad_2d_tensors_of_different_sizes_in_all_dimensions
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def test_batch_and_unpad_2d_tensors_of_different_sizes_in_all_dimensions(
self):
with self.test_session() as sess:
batch_size = 3
num_batches = 2
examples = tf.Variable(tf.constant(2, dtype=tf.int32))
counter = examples.count_up_to(num_batches * batch_size + 2)
image = tf.reshape(
tf.range(counter * counter), tf.stack([counter, counter]))
batch_queue = batcher.BatchQueue(
tensor_dict={'image': image},
batch_size=batch_size,
batch_queue_capacity=100,
num_batch_queue_threads=1,
prefetch_queue_capacity=100)
batch = batch_queue.dequeue()
for tensor_dict in batch:
for tensor in tensor_dict.values():
self.assertAllEqual([None, None], tensor.get_shape().as_list())
tf.initialize_all_variables().run()
with slim.queues.QueueRunners(sess):
i = 2
for _ in range(num_batches):
batch_np = sess.run(batch)
for tensor_dict in batch_np:
for tensor in tensor_dict.values():
self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
i += 1
with self.assertRaises(tf.errors.OutOfRangeError):
sess.run(batch)
示例14: test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions(self):
with self.test_session() as sess:
batch_size = 3
num_batches = 2
examples = tf.Variable(tf.constant(1, dtype=tf.int32))
counter = examples.count_up_to(num_batches * batch_size + 1)
image = tf.reshape(tf.range(1, 13), [4, 3]) * counter
batch_queue = batcher.BatchQueue(
tensor_dict={'image': image},
batch_size=batch_size,
batch_queue_capacity=100,
num_batch_queue_threads=1,
prefetch_queue_capacity=100)
batch = batch_queue.dequeue()
for tensor_dict in batch:
for tensor in tensor_dict.values():
self.assertAllEqual([4, 3], tensor.get_shape().as_list())
tf.initialize_all_variables().run()
with slim.queues.QueueRunners(sess):
i = 1
for _ in range(num_batches):
batch_np = sess.run(batch)
for tensor_dict in batch_np:
for tensor in tensor_dict.values():
self.assertAllEqual(tensor, np.arange(1, 13).reshape((4, 3)) * i)
i += 1
with self.assertRaises(tf.errors.OutOfRangeError):
sess.run(batch)
示例15: test_batcher_when_batch_size_is_one
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import initialize_all_variables [as 别名]
def test_batcher_when_batch_size_is_one(self):
with self.test_session() as sess:
batch_size = 1
num_batches = 2
examples = tf.Variable(tf.constant(2, dtype=tf.int32))
counter = examples.count_up_to(num_batches * batch_size + 2)
image = tf.reshape(
tf.range(counter * counter), tf.stack([counter, counter]))
batch_queue = batcher.BatchQueue(
tensor_dict={'image': image},
batch_size=batch_size,
batch_queue_capacity=100,
num_batch_queue_threads=1,
prefetch_queue_capacity=100)
batch = batch_queue.dequeue()
for tensor_dict in batch:
for tensor in tensor_dict.values():
self.assertAllEqual([None, None], tensor.get_shape().as_list())
tf.initialize_all_variables().run()
with slim.queues.QueueRunners(sess):
i = 2
for _ in range(num_batches):
batch_np = sess.run(batch)
for tensor_dict in batch_np:
for tensor in tensor_dict.values():
self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i)))
i += 1
with self.assertRaises(tf.errors.OutOfRangeError):
sess.run(batch)