1

在保存并训练了张量流图之后,我将其恢复为使用不同的损失函数进行重新训练,如下所示:

 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)]]

这让我有点惊讶,因为:

  1. 我使用以下行初始化了所有变量:

     tf.global_variables_initializer().run(session=sess)
    

    这可能是因为未按照此处的建议使用此行初始化权重和偏差:Uninitialized value error while using Adadelta optimizer in Tensorflow

  2. 我在以下几行中初始化权重和偏差:

    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

我很欣赏在这里解决我的问题的任何想法。

4

2 回答 2

2

问题出在以下位置:

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)

于 2018-01-15T10:38:11.090 回答
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])
于 2018-01-14T21:31:21.013 回答