tensorflow - 为优化器分配名称以供将来恢复
tensorflow - assign name to optimizer for future restoration
我在 tensorflow 中创建模型,其中最后一行是
import tensorflow as tf
...
train_step = tf.train.AdagradOptimizer(LEARNING_RATE).minimize(some_loss_function)
不知能否给这个tensor/operation起个名字,这样存盘后可以按名字恢复?
或者,如果我不能给它一个名字,我怎么能在输出中找到它
以下命令:
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 中创建模型,其中最后一行是
import tensorflow as tf
...
train_step = tf.train.AdagradOptimizer(LEARNING_RATE).minimize(some_loss_function)
不知能否给这个tensor/operation起个名字,这样存盘后可以按名字恢复?
或者,如果我不能给它一个名字,我怎么能在输出中找到它 以下命令:
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