2017-01-06 6 views
2

분산 된 텐서 흐름에서 간단한 산술 계산을 수행하는 다음 코드가 있습니다. 최소한의 재현 예는 다음과 같습니다 -TypeError : 저장할 변수가 변수가 아닙니다.

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) 

날이 기능을 사용하는 올바른 방법을 알아내는 데 도움이 바랍니다.

+0

제공된 코드 조각에 줄 40이없는 것 같습니다. – martianwars

+1

이것은 코드의 단순화 된 버전입니다. 오류의 줄 번호를 지금 편집했습니다. – itsamineral

+0

을 확인하십시오.'y'는 텐서가 아니라 변수입니다. – martianwars

답변

1

x + 300으로 간단히 작성하면 tf.Variable이 생성되지 않습니다. 명시 적으로 tf.get_variable() 또는 tf.Variable()을 사용하여 저장할 수있는 변수를 만들어야합니다.

y = tf.Variable(x + 300)