1

I am trying to restore a saved model . But it is returning me an error. Please help me out. code to save the model : save_model.py

import tensorflow as tf
v1 = tf.Variable(1.32, name="v1")
v2 = tf.Variable(1.33, name="v2")

init = tf.initialize_all_variables()

saver = tf.train.Saver()

with tf.Session() as sess:
  sess.run(init)
  save_path = saver.save(sess, "model.ckpt")

code to restore model : restore_model.py

import tensorflow as tf
v1 = tf.Variable(0, name="v1")
v2 = tf.Variable(0, name="v2")


saver = tf.train.Saver()

with tf.Session() as sess:
  saver.restore(sess, "model.ckpt")
  print("Model restored.")

I have saved both the files in the same directory.

1 Answers1

0

I suspect the error is raised because in save_model.py you declare the variables as having type tf.float32 (the implicit type of 1.32 and 1.33), whereas in restore_model.py you define the variables as having type tf.int32 (the implicit type of 0).

The easiest solution would be to modify restore_model.py to declare the variables as tf.float32. For example, you could do the following:

v1 = tf.Variable(0.0, name="v1")
v2 = tf.Variable(0.0, name="v2")
mrry
  • 125,488
  • 26
  • 399
  • 400
  • hi, thanks a lot ! it works now. I changed the values to 0.0 and it works fine. But after restoring the values of the variables are still 0.0 instead of 1.32 and 1.33. Any suggestions? – Vikraman Karunanidhi May 14 '16 at 05:17
  • Can you share the full updated source to `restore_model.py` in the question? The variables should never be assigned to 0.0 (unless there's some additional code that runs the initializer for them). – mrry May 14 '16 at 17:12
  • Hi, the updated code. import tensorflow as tf v1 = tf.Variable(0.0, name="v1") v2 = tf.Variable(0.0, name="v2") saver = tf.train.Saver() init = tf.initialize_all_variables() with tf.Session() as sess: save_path="model.ckpt" saver.restore(sess, save_path) #print v1.eval(sess) print("Model restored.") print v1.eval() – Vikraman Karunanidhi May 17 '16 at 06:26