Tensorflow: New added variables cannot be saved

Created on 24 May 2016  ·  1Comment  ·  Source: tensorflow/tensorflow

I tested by running these 3 pieces of code respectively.

First, init some variables and save.

import tensorflow as tf

sess = tf.InteractiveSession()
v1 = tf.Variable(1,name="v1")
v2 = tf.Variable(2,name="v2")
sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
saver.save(sess,'v12.ckpt')

Then, restore the session, add one more variable, and save.

import tensorflow as tf

sess = tf.InteractiveSession()
v1 = tf.Variable(1,name="v1")
v2 = tf.Variable(2,name="v2")
saver = tf.train.Saver()
saver.restore(sess,'v12.ckpt')  #works fine here

v3 = tf.Variable(3,name="v3")
sess.run(tf.initialize_variables([v3]))

saver.save(sess,'v123.ckpt')

print v3.eval() #show value without problem

Then, restore them.

import tensorflow as tf

sess = tf.InteractiveSession()
v1 = tf.Variable(1,name="v1")
v2 = tf.Variable(2,name="v2")
v3 = tf.Variable(3,name="v3")

saver = tf.train.Saver()
saver.restore(sess,'v123.ckpt') #error here

This is the error:

tensorflow.python.framework.errors.NotFoundError: Tensor name "v3" not found in checkpoint files v123.ckpt [[Node: save/restore_slice_2 = RestoreSlice[dt=DT_INT32, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_2/tensor_name, save/restore_slice_2/shape_and_slice)]] Caused by op u'save/restore_slice_2'

What is the problem here?

I'm using version r0.8 on Ubuntu16.04

Most helpful comment

When you create a tf.train.Saver with no arguments, it will implicitly use the current set of variables _at the time of Saver construction_ when it saves and restores. If you add a new variable (e.g. v3 in your second code block), you have to create a new tf.train.Saver to save it.

import tensorflow as tf

sess = tf.InteractiveSession()
v1 = tf.Variable(1,name="v1")
v2 = tf.Variable(2,name="v2")
saver = tf.train.Saver()
saver.restore(sess,'v12.ckpt')  #works fine here

v3 = tf.Variable(3,name="v3")
sess.run(tf.initialize_variables([v3]))

saver_with_v3 = tf.train.Saver()
saver_with_v3.save(sess,'v123.ckpt')

>All comments

When you create a tf.train.Saver with no arguments, it will implicitly use the current set of variables _at the time of Saver construction_ when it saves and restores. If you add a new variable (e.g. v3 in your second code block), you have to create a new tf.train.Saver to save it.

import tensorflow as tf

sess = tf.InteractiveSession()
v1 = tf.Variable(1,name="v1")
v2 = tf.Variable(2,name="v2")
saver = tf.train.Saver()
saver.restore(sess,'v12.ckpt')  #works fine here

v3 = tf.Variable(3,name="v3")
sess.run(tf.initialize_variables([v3]))

saver_with_v3 = tf.train.Saver()
saver_with_v3.save(sess,'v123.ckpt')
Was this page helpful?
0 / 5 - 0 ratings