gpt4 book ai didi

tensorflow - 为优化器分配名称以供将来恢复

转载 作者:行者123 更新时间:2023-12-05 04:09:05 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com