gpt4 book ai didi

tensorflow - 如何在 Tensorflow 的检查点中保存张量?

转载 作者:行者123 更新时间:2023-12-03 22:45:34 25 4
gpt4 key购买 nike

我想使用 tf.train.Saver() 来创建张量的检查点,这是我的代码片段:

import tensorflow as tf

with tf.Graph().as_default():
var = tf.Variable(tf.zeros([10]), name="biases")
temp = tf.add(var, 0.1)
init_op = tf.global_variables_initializer()

saver = tf.train.Saver({'w':temp})

with tf.Session() as sess:
sess.run(init_op)
print(sess.run(temp))

但是报错如下:

Traceback (most recent call last):
File "./test_counter.py", line 61, in <module>
saver = tf.train.Saver({'w':temp})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1043, in __init__
self.build()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1073, in build
restore_sequentially=self._restore_sequentially)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 649, in build
saveables = self._ValidateAndSliceInputs(names_to_saveables)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 578, in _ValidateAndSliceInputs
variable)
TypeError: names_to_saveables must be a dict mapping string names to Tensors/Variables. Not a variable: Tensor("TransformFeatureToIndex:0", shape=(100,), dtype=string)

我想到的一种方法是通过 sess.run(temp) 将 Tensor 存储在客户端并保存,但是有没有更重要的方法?

最佳答案

temp 不是 tf.Variable,而是一个操作。它“没有”任何值或状态,它只是图中的一个节点。如果你想明确地保存添加到var的结果,你可以通过tf.assigntemp分配给另一个变量并保存这个另一个变量.更简单的方法可能是保存 var(或整个 session ),并在恢复后再次评估 temp

关于tensorflow - 如何在 Tensorflow 的检查点中保存张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587689/

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