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

时间:2017-01-11 09:57:52

标签: tensorflow

我想使用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存储在客户端并保存,但是有更重要的方法吗?

1 个答案:

答案 0 :(得分:5)

temp不是tf.Variable,而是一项操作。它“没有”任何值或状态,它只是图中的一个节点。如果要明确保存添加到var的结果,可以temptf.assign分配给另一个变量并保存此其他变量。更简单的方法可能是保存var(或整个会话),并在恢复后再次评估temp