我在 tensorflow 中创建模型,其中最后一行是
import tensorflow as tf
...
train_step = tf.train.AdagradOptimizer(LEARNING_RATE).minimize(some_loss_function)
我想知道我是否可以给这个张量/操作一个名字,以便我可以在保存到磁盘后按名称恢复它?
或者,如果我不能给它一个名字,我怎么能在输出中找到它 以下命令:
tf.get_default_graph().get_operations()
最佳答案
根据the docs for tf.train.Optimizer
是的,是的,你可以。
train_step = tf.train.AdamOptimizer().minimize(loss, name='my_training_step')
然后您可以稍后恢复操作:
saver = tf.train.Saver(...)
sess = tf.Session()
saver.restore(sess, 'path/to/model')
train_op = sess.graph.get_operation_by_name('my_training_step')
您还可以将训练操作存储在一个集合中,并通过importing the meta graph 恢复它。 .添加到集合并保存如下所示:
saver = tf.train.Saver(...)
tf.add_to_collection('train_step', train_step)
# ...
with tf.Session() as sess:
# ...
sess.save(sess, ...)
恢复看起来像:
new_saver = tf.train.import_meta_graph('path/to/metagraph')
new_saver.restore(sess, 'path/to/model')
train_op = tf.get_collection('train_step')[0] # restore the op
关于tensorflow - 为优化器分配名称以供将来恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653668/