tczj
tczj

Reputation: 448

TensorFlow cannot restore saved values for existing variables

I try to save a variable whose value is changed after initialized. And then I restore the model, but the value restored is still the initial value for this variable, why does it not restore the current value of the variable when I save the model?

Here is my code:

import tensorflow as tf
import numpy as np
a = tf.Variable(tf.zeros([3, 2]), name='a')
saver = tf.train.Saver()
tf_config = tf.ConfigProto(allow_soft_placement=True)
tf_config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=tf_config)
tf.global_variables_initializer().run()

print(a.eval())
#[[ 0.  0.]
# [ 0.  0.]
# [ 0.  0.]]
a = a + np.random.randint(0, 5, (3, 2))
print(a.eval())
#[[ 0.  3.]
# [ 2.  3.]
# [ 0.  3.]]
saver.save(sess, './model/model.ckpt')
a = a - np.random.randint(0, 5, (3, 2))
print(a.eval())
#[[-1.  3.]
# [ 2.  2.]
# [-4.  3.]]
saver.restore(sess, './model/model.ckpt')
print(a.eval())
#[[-1.  3.]
# [ 2.  2.]
# [-4.  3.]]

Obviously, value of a restored here is not the one I intended to save.

Upvotes: 1

Views: 167

Answers (1)

etarion
etarion

Reputation: 17131

You have a misunderstanding of how TF operates. The TF variable you define on line 3 never changes its value after initializing.

a = a + np.random.randint(0, 5, (3, 2))

This does not change the TF variable, it adds nodes to the graph to create a tensor that is the sum of the variable and the numpy array you create. The value of the TF variable is still zeroes, and the python variable now points to the tensor that is the sum (so the python variable a does not point to the TF variable anymore).

To change the value of the TF variable, use something like

sess.run(tf.assign(a, a + np.random.randint(0, 5, (3, 2))))

This way, you update the TF variable with the new value and the python variable still points to the TF variable.

Upvotes: 1

Related Questions