我想使用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, insaver = 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
明确,则可以temp
通过另一个变量分配tf.assign
并保存其他变量.更简单的方法可能是保存var
(或整个会话),并在恢复之后temp
再次进行评估.