简体   繁体   中英

How to initialize variables that contains list of tuples in tensorflow?

I am trying to work through this guide .

for this line of code, I get error:

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)]]

How do I initialize a list of tuples in tensorflow?

I tried to change this line of code from:

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

to:

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

but get error:

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

what am I doing wrong to initialize the variable?

entire code:

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)

Updated code:

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)

Hopefully that gets your through your "uninitialized variables" issue.

Basically, the issue you're running into is that TF is a wrapper around C++. So, to handle some optimization stuff, they require you to 1) define all the variables, 2) initialize them BEFORE doing any operations. Hence, you faced the issue of

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

giving you the "uninitialized variables" error.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM