如何初始化包含tensorflow中元组列表的变量



我正在努力阅读本指南。

对于这行代码,我得到错误:

model.fit(training_x, training_y, epochs=1000, steps_per_epoch=10)
FailedPreconditionError: Attempting to use uninitialized value Variable_2
[[{{node Variable_2/read}} = Identity[T=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Variable_2)]]

如何初始化tensorflow中的元组列表?

我试图将这行代码从更改为

training_y = tf.Variable([[0], [0], [1], [1], [1], [0], [1],[0], [1], [1], [1], [1], [1], [0]])

至:

training_y = tf.global_variables_initializer([[0], [0], [1], [1], [1], [0], [1],[0], [1], [1], [1], [1], [1], [0]])

但出现错误:

TypeError: global_variables_initializer() takes 0 positional arguments but 1 was given

初始化变量时我做错了什么?

整个代码:

import tensorflow as tf
from tensorflow import keras
model = keras.Sequential()
input_layer = keras.layers.Dense(3, input_shape=[3], activation='tanh')
model.add(input_layer)
output_layer = keras.layers.Dense(1, activation='sigmoid')
model.add(output_layer)
gd = tf.train.GradientDescentOptimizer(0.01)
model.compile(optimizer=gd, loss='mse')

training_x = tf.Variable([[1, 1, 0], [1, 1, 1], [0, 1, 0], [-1, 1, 0], [-1, 0, 0], [-1, 0, 1],[0, 0, 1], [1, 1, 0], [1, 0, 0], [-1, 0, 0], [1, 0, 1], [0, 1, 1], [0, 0, 0], [-1, 1, 1]])
training_y = tf.Variable([[0], [0], [1], [1], [1], [0], [1],[0], [1], [1], [1], [1], [1], [0]])

model.fit(training_x, training_y, epochs=1000, steps_per_epoch=10)
# model.save_weights('demo_model.h5')
# model.load_weights('demo_model.h5')
text_x = tf.Variable([[1, 0, 0]])
test_y = model.predict(text_x, verbose=0, steps=1)

print(test_y)

更新代码:

import tensorflow as tf
from tensorflow import keras
model = keras.Sequential()
input_layer = keras.layers.Dense(3, input_shape=[3], activation='tanh')
model.add(input_layer)
output_layer = keras.layers.Dense(1, activation='sigmoid')
model.add(output_layer)
gd = tf.train.GradientDescentOptimizer(0.01)
model.compile(optimizer=gd, loss='mse')
sess = tf.Session()  #NEW LINE
training_x = tf.Variable([[1, 1, 0], [1, 1, 1], [0, 1, 0], [-1, 1, 0], [-1, 0, 0], [-1, 0, 1],[0, 0, 1], [1, 1, 0], [1, 0, 0], [-1, 0, 0], [1, 0, 1], [0, 1, 1], [0, 0, 0], [-1, 1, 1]])
training_y = tf.Variable([[0], [0], [1], [1], [1], [0], [1],[0], [1], [1], [1], [1], [1], [0]])
#init_op = tf.initialize_variables([training_x, training_y])
init_op = tf.initializers.global_variables()

sess.run(init_op)  #NEW LINE
model.fit(training_x, training_y, epochs=1000, steps_per_epoch=10)
# model.save_weights('demo_model.h5')
# model.load_weights('demo_model.h5')
text_x = tf.Variable([[1, 0, 0]])
test_y = model.predict(text_x, verbose=0, steps=1)
print(test_y)
sess = tf.Session()  #NEW LINE
training_x = tf.Variable([[1, 1, 0], [1, 1, 1], [0, 1, 0], [-1, 1, 0], [-1, 0, 0], [-1, 0, 1],[0, 0, 1], [1, 1, 0], [1, 0, 0], [-1, 0, 0], [1, 0, 1], [0, 1, 1], [0, 0, 0], [-1, 1, 1]])
training_y = tf.Variable([[0], [0], [1], [1], [1], [0], [1],[0], [1], [1], [1], [1], [1], [0]])
init_op = tf.initialize_variables([training_x, training_y, test_x, test_y])  #NEW LINE
sess.run(init_op)  #NEW LINE
model.fit(training_x, training_y, epochs=1000, steps_per_epoch=10)
# model.save_weights('demo_model.h5')
# model.load_weights('demo_model.h5')
text_x = tf.Variable([[1, 0, 0]])
test_y = model.predict(text_x, verbose=0, steps=1)
print(test_y)

希望这能让你解决"未初始化变量"的问题。

基本上,您遇到的问题是TF是C++的包装器。因此,为了处理一些优化问题,它们需要您1(定义所有变量,2(在执行任何操作之前初始化它们。因此,您面临的问题

model.fit(training_x, training_y, epochs=1000, steps_per_epoch=10)

给出"未初始化变量"错误。

希望这能有所帮助!

最新更新