I declared the some Tensorflow variables representing weights and biases and updated their values in training before saving them, as shown:
# # 5 x 5 x 5 patches, 1 channel, 32 features to compute.
weights = {'W_conv1':tf.Variable(tf.random_normal([3,3,3,1,32]), name='w_conv1'),
# 5 x 5 x 5 patches, 32 channels, 64 features to compute.
'W_conv2':tf.Variable(tf.random_normal([3,3,3,32,64]), name='w_conv2'),
# 64 features
'W_fc':tf.Variable(tf.random_normal([32448,1024]), name='w_fc'), #54080 = ceil(50/2/2) * ceil(50/2/2) * ceil(10/2/2) * 64
#'W_fc':tf.Variable(tf.random_normal([54080,1024]), name='W_fc'), #54080 = ceil(50/2/2) * ceil(50/2/2) * ceil(20/2/2) * 64
'out':tf.Variable(tf.random_normal([1024, n_classes]), name='w_out')}
biases = {'b_conv1':tf.Variable(tf.random_normal([32]), name='b_conv1'),
'b_conv2':tf.Variable(tf.random_normal([64]), name='b_conv2'),
'b_fc':tf.Variable(tf.random_normal([1024]), name='b_fc'),
'out':tf.Variable(tf.random_normal([n_classes]), name='b_out')}
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
#some training code
saver = tf.train.Saver()
saver.save(sess, 'my-save-dir/my-model-10')
Then, I tried restoring the model and accessing the variables as shown below:
weights = {'W_conv1':tf.Variable(-1.0, validate_shape=False, name='w_conv1'),
# 5 x 5 x 5 patches, 32 channels, 64 features to compute.
'W_conv2':tf.Variable(-1.0, validate_shape=False, name='w_conv2'),
# 64 features
'W_fc':tf.Variable(-1.0, validate_shape=False, name='w_fc'), #54080 = ceil(50/2/2) * ceil(50/2/2) * ceil(10/2/2) * 64
#'W_fc':tf.Variable(tf.random_normal([54080,1024]), name='W_fc'), #54080 = ceil(50/2/2) * ceil(50/2/2) * ceil(20/2/2) * 64
'out':tf.Variable(-1.0, validate_shape=False, name='w_out')}
biases = {'b_conv1':tf.Variable(-1.0, validate_shape=False, name='b_conv1'),
'b_conv2':tf.Variable(-1.0, validate_shape=False, name='b_conv2'),
'b_fc':tf.Variable(-1.0, validate_shape=False, name='b_fc'),
'out':tf.Variable(-1.0, validate_shape=False, name='b_out')}
with tf.Session() as sess:
model_saver = tf.train.import_meta_graph('my-save-dir/my-model-10.meta')
model_saver.restore(sess, "my-save-dir/my-model-10")
print("Model restored.")
print('Initialized')
print(sess.run(weights['W_conv1']))
However, I got a "FailedPreconditionError: Attempting to use uninitialized value w_conv1". Please assist.