我正在读书Machine Learning with Tensorflow。它具有以下示例:
import tensorflow as tf
sess = tf.InteractiveSession()
raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13]
spikes = tf.Variable([False] * len(raw_data), name='spikes')
spikes.initializer.run()
saver = tf.train.Saver()
for i in range(1, len(raw_data)):
if raw_data[i] - raw_data[i-1] > 5:
spikes_val = spikes.eval()
spikes_val[i] = True
updater = tf.assign(spikes, spikes_val)
updater.eval()
save_path = saver.save(sess, "spikes.ckpt")
print("spikes data saved in file: %s" % save_path)
sess.close()
此代码给我错误:Attempting to use uninitialized value Variable
我可以使用tf.global_variables_initializer()
使用该代码,但是我不明白为什么如果初始化变量“ spikes”,其他代码也无法正常工作。
如果我在save_path = saver.save(sess, "spikes.ckpt")
行中添加注释,则程序运行正常。但是我不能初始化“ saver”,因为它不是变量。那么这里发生了什么? tf.global_variables_initializer()
还会初始化其他哪些变量以使其起作用?