After having saved and trained a tensorflow graph, I restore it back for re-training with a different loss function as follows:
import tensorflow as tf
import numpy as np
import pyximport
pyximport.install()
import math
import tensorflow.contrib.slim as slim
raw_data_train = np.loadtxt('all_data/train_all_raw.csv', skiprows = 1, delimiter=',')
users = (np.unique(raw_data_train[ :, 0]))
items = (np.unique(raw_data_train[ :, 1]))
saver = tf.train.import_meta_graph('all_data/my_test_model.meta')
with tf.Session() as sess:
tf.global_variables_initializer().run(session=sess)
saver.restore(sess, tf.train.latest_checkpoint('all_data/'))
# placeholders
user_ids = sess.graph.get_tensor_by_name('user_ids:0')
left_ids = sess.graph.get_tensor_by_name('left_ids:0')
# variables
user_latents = sess.graph.get_tensor_by_name('user_latents:0')
item_latents = sess.graph.get_tensor_by_name('item_latents:0')
# network was initiall defined as variable_scope "nn" that is why I am retrieving them as "nn/*" in the following line
weights_0 = sess.graph.get_tensor_by_name('nn/fully_connected/weights:0')
biases_0 = sess.graph.get_tensor_by_name('nn/fully_connected/biases:0')
weights_1 = sess.graph.get_tensor_by_name('nn/fully_connected_1/weights:0')
biases_1 = sess.graph.get_tensor_by_name('nn/fully_connected_1/biases:0')
# lookups
user_embeddings = sess.graph.get_tensor_by_name('embedding_user:0')
item_left_embeddings = sess.graph.get_tensor_by_name('embedding_left:0')
# dictionary
fd = {
user_ids: users,
left_ids: items,
}
left_emb_val, weights_0_val, biases_0_val, weights_1_val, biases_1_val = sess.run([left_emb, weights_0, biases_0, weights_1, biases_1], feed_dict=fd)
joined_input = tf.concat( [user_embeddings, item_left_embeddings], 1)
net = slim.fully_connected(inputs=joined_input, num_outputs=64, weights_initializer = tf.constant_initializer(weights_0_val), biases_initializer=tf.constant_initializer(biases_0_val), activation_fn=tf.nn.relu)
left_output = slim.fully_connected(inputs=net, num_outputs=1, weights_initializer = tf.constant_initializer(weights_1_val), biases_initializer=tf.constant_initializer(biases_1_val), activation_fn=None)
# ********* below line gives an error *************
left_output_val = sess.run([left_output], feed_dict=fd)
print(left_output_val)
Above code gives the following error when I am trying to compute the value of left_output_val
by calling sess.run
.
FailedPreconditionError (see above for traceback): Attempting to use uninitialized value fully_connected_1/biases
[[Node: fully_connected_1/biases/read = Identity[T=DT_FLOAT, _class=["loc:@fully_connected_1/biases"], _device="/job:localhost/replica:0/task:0/cpu:0"](fully_connected_1/biases)]]
It is a bit surprising to me because:
I initialized all the variables using following line:
tf.global_variables_initializer().run(session=sess)
This may be because weights and biases were not initialized with this line as suggested here: Uninitialized value error while using Adadelta optimizer in Tensorflow
I am initializing weights and biases in following lines:
net = slim.fully_connected(inputs=joined_input, num_outputs=64, weights_initializer = tf.constant_initializer(weights_0_val), biases_initializer=tf.constant_initializer(biases_0_val), activation_fn=tf.nn.relu)
left_output = slim.fully_connected(inputs=net, num_outputs=1, weights_initializer = tf.constant_initializer(weights_1_val), biases_initializer=tf.constant_initializer(biases_1_val), activation_fn=None)
Still there is an unitialized weights and biases error while running session and computing the value of left_output_val
I appreciate any sorts of ideas for solving my problem here.
You can get the variables from this dense layer and initialize them manually.
with tf.variable_scope('fully_connected_1', reuse=True):
weights = tf.get_variable('weights')
biases = tf.get_variable('biases')
sess.run([weights.initializer, biases.initializer])