我想使用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存储在客户端并保存,但是有更重要的方法吗?
答案 0 :(得分:5)
temp
不是tf.Variable
,而是一项操作。它“没有”任何值或状态,它只是图中的一个节点。如果要明确保存添加到var
的结果,可以temp
将tf.assign
分配给另一个变量并保存此其他变量。更简单的方法可能是保存var
(或整个会话),并在恢复后再次评估temp
。