0
votes

I'm trying to load a saved model by saver and evaluate the output of the network over the test data, restoring a saved model and using the network as a function:

def model(x, x_size):
   with tf.variable_scope("my_net", reuse=tf.AUTO_REUSE):
       W1 = tf.get_variable('w1', [x_size, x_size], 
                initializer=tf.random_normal_initializer())
       b1 = tf.get_variable('b1', [x_size], 
                initializer=tf.random_normal_initializer())  
       y1 = tf.nn.relu(tf.matmul(x, W1) + b1,'y1')
       return y1 


eval_x = tf.placeholder(tf.float32, name='eval_x', shape=[None, x_size])
eval_probs = model(eval_x, x_size)
with tf.Session() as sess:
    new_saver = tf.train.import_meta_graph('save_model/model.ckpt.meta')
    new_saver.restore(sess, "save_model/model.ckpt")
    probs = sess.run(eval_probs, feed_dict={eval_x: test_x})

The error I get is:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value my_net/w1 [[Node: my_net/w1/read = IdentityT=DT_FLOAT, _class=["loc:@my_net/w1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Can anybody help?

2

2 Answers

1
votes

You need to run a special operation to initialize variables like w1. An easy way to do this is to execute sess.run(tf.global_variables_initializer()) after you create the session but before you use your variables.

0
votes

From the code it seems that you are not passing any value for

x_size

so I don't think your variables get initialised.