在保存并训练了张量流图后,我将其恢复为具有不同损失函数的重新训练,如下所示:
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)
当我尝试通过调用left_output_val
来计算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)]]
对我来说有点意外,因为:
我使用以下行初始化所有变量:
tf.global_variables_initializer().run(session=sess)
这可能是因为权重和偏差未使用此行初始化,如下所示:Uninitialized value error while using Adadelta optimizer in Tensorflow
我正在以下几行初始化权重和偏差:
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)
运行会话并计算left_output_val
我很欣赏在这里解决问题的各种想法。
答案 0 :(得分:1)
您可以从此密集层获取变量并手动初始化它们。
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])
答案 1 :(得分:1)
问题是由于:
的位置 tf.global_variables_initializer().run(session=sess)
应该是:
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)