python - 类型错误 : Variable to save is not a Variable

标签 python session tensorflow distributed-computing

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

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)

请帮我想出正确的方法来使用这个函数。

最佳答案

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

y = tf.Variable(x + 300)

关于python - 类型错误 : Variable to save is not a Variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41498883/

相关文章:

powershell - Scope vs Runspace vs Session vs AppDomain

并发 session 和 AJAX 的 PHP 问题

python - np.concatenate 的内存错误

python - Groupby 应用自定义函数 Pandas

python - 我们如何使用 Spark 和 Scala 为 Elasticsearch 索引定义过滤器/属性?

python - 随时间序列迭代

php - 在 session 过期时将 session 变量保存到数据库

tensorflow - 我的训练数据集对于我的神经网络来说是否太复杂?

tensorflow - 为二维张量创建索引

python - 使用 python 记录文件和进程超时的输出