【问题标题】:TypeError: Variable to save is not a VariableTypeError:要保存的变量不是变量
【发布时间】:2017-01-06 22:29:11
【问题描述】:

我有以下代码在分布式张量流中做一些简单的算术计算。一个最小的可重现示例是:-

import tensorflow as tf

global_step_tensor = tf.Variable(10, trainable=False, name='global_step')

cluster = tf.train.ClusterSpec({"local": ["localhost:2222", "localhost:2223","localhost:2224", "localhost:2225"]})
x = tf.constant(2)

with tf.device("/job:local/task:0"):
    y = x + 300

model = tf.global_variables_initializer()

saver = tf.train.Saver([y])

ChiefSessionCreator = tf.train.ChiefSessionCreator(scaffold=None, master='grpc://localhost:2222', config=None, checkpoint_dir='/home/chaitanya/tensorflow/codes/checkpoints')
saver_hook = tf.train.CheckpointSaverHook(checkpoint_dir='/home/chaitanya/tensorflow/codes/checkpoints', save_secs=10, save_steps=None, saver=y, checkpoint_basename='model.ckpt', scaffold=None)
summary_hook = tf.train.SummarySaverHook(save_steps=None, save_secs=10, output_dir='/home/chaitanya/tensorflow/codes/savepoints', summary_writer=None, scaffold=None, summary_op=y)

with tf.train.MonitoredTrainingSession(master='grpc://localhost:2222', is_chief=True, checkpoint_dir='/home/chaitanya/tensorflow/codes/checkpoints', 
    scaffold=None, hooks=[saver_hook, summary_hook], chief_only_hooks=None, save_checkpoint_secs=10, save_summaries_steps=None, config=None) as sess:

    while not sess.should_stop():
        sess.run(model)

    while not sess.should_stop():
        result = sess.run(y)
        print(result)

以下是错误:-

Traceback (most recent call last):
  File "add_1.py", line 13, in <module>
    saver = tf.train.Saver([y])
    raise TypeError("Variable to save is not a Variable: %s" % var)
TypeError: Variable to save is not a Variable: Tensor("add_3:0", shape=(), dtype=int32, device=/job:local/task:3)

请帮我找出使用此功能的正确方法。

【问题讨论】:

  • 您提供的代码 sn-p 中似乎没有第 40 行
  • 这是代码的简化版本。我现在已经编辑了错误中的行号。请检查
  • y 是张量,而不是变量

标签: python session tensorflow distributed-computing


【解决方案1】:

当您只写x + 300 时,您并没有创建tf.Variable。您需要显式使用tf.get_variable()tf.Variable() 来创建可以保存的变量。

y = tf.Variable(x + 300)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2014-03-14
    • 2016-12-02
    • 2011-11-11
    • 2012-05-17
    相关资源
    最近更新 更多