2017-10-13 60 views
1

我遇到了正确恢复保存的张量流模型的问题。我创建了双向RNN模型tensorflow与以下代码:如何在张量流中恢复已保存的BiRNN模型,使所有输出神经元正确绑定到相应的输出类

batchX_placeholder = tf.placeholder(tf.float32, [None, timesteps, 1], 
            name="batchX_placeholder")]) 
batchY_placeholder = tf.placeholder(tf.float32, [None, num_classes], 
            name="batchY_placeholder") 
weights = tf.Variable(np.random.rand(2*STATE_SIZE, num_classes), 
         dtype=tf.float32, name="weights") 
biases = tf.Variable(np.zeros((1, num_classes)), dtype=tf.float32, 
        name="biases") 
logits = BiRNN(batchX_placeholder, weights, biases) 
with tf.name_scope("prediction"): 
    prediction = tf.nn.softmax(logits) 
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=batchY_placeholder)) 
lr = tf.Variable(learning_rate, trainable=False, dtype=tf.float32, 
       name='lr') 
optimizer = tf.train.AdamOptimizer(learning_rate=lr) 
train_op = optimizer.minimize(loss_op) 
init_op = tf.initialize_all_variables() 
saver = tf.train.Saver() 

BiRNN的体系结构用下面的函数创建的:

def BiRNN(x, weights, biases): 
    # Unstack to get a list of 'time_steps' tensors of shape (batch_size, 
    # num_input) 
    x = tf.unstack(x, time_steps, 1) 
    # Forward and Backward direction cells 
    lstm_fw_cell = rnn.BasicLSTMCell(STATE_SIZE, forget_bias=1.0) 
    lstm_bw_cell = rnn.BasicLSTMCell(STATE_SIZE, forget_bias=1.0) 
    outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, 
     lstm_bw_cell, x, dtype=tf.float32) 
    # Linear activation, using rnn inner loop last output 
    return tf.matmul(outputs[-1], weights) + biases 

然后我训练模型,并且每个200步后保存:

with tf.Session() as sess: 
    sess.run(init_op) 
    current_step = 0 
    for batch_x, batch_y in get_minibatch(): 
     sess.run(train_op, feed_dict={batchX_placeholder: batch_x, 
             batchY_placeholder: batch_y}) 
     current_step += 1 
     if current_step % 200 == 0: 
      saver.save(sess, os.path.join(model_dir, "model") 

要以推理模式保存的模型我用“model.meta”文件保存tensorflow图:

graph = tf.get_default_graph() 
saver = tf.train.import_meta_graph(os.path.join(model_dir, "model.meta")) 
sess = tf.Session() 
saver.restore(sess, tf.train.latest_checkpoint(model_dir) 
weights = graph.get_tensor_by_name("weights:0") 
biases = graph.get_tensor_by_name("biases:0") 
batchX_placeholder = graph.get_tensor_by_name("batchX_placeholder:0") 
batchY_placeholder = graph.get_tensor_by_name("batchY_placeholder:0") 
logits = BiRNN(batchX_placeholder, weights, biases) 
prediction = graph.get_operation_by_name("prediction/Softmax") 
argmax_pred = tf.argmax(prediction, 1) 
init = tf.global_variables_initializer() 
sess.run(init) 
for x_seq, y_gt in get_sequence(): 
    _, y_pred = sess.run([prediction, argmax_pred], 
        feed_dict={batchX_placeholder: [x_seq]], 
           batchY_placeholder: [[0.0, 0.0]]}) 
    print("Y ground true: " + str(y_gt) + ", Y pred: " + str(y_pred[0])) 

当我在推理模式下运行代码时,每次启动它时都会得到不同的结果。似乎来自softmax层的输出神经元随机地与不同的输出类捆绑在一起。

所以,我的问题是:如何保存并正确恢复张量流模型,使所有神经元与相应的输出类正确绑定?

回答

2

有没有必要拨打tf.global_variables_initializer(),我认为这是你的问题。

我删除了一些操作:logitsweightsbiases,因为你并不需要它们,所有这些都已经加载,使用graph.get_tensor_by_name得到它们。

对于prediction,得到代替操作的。 (见本answer):

这是代码:

graph = tf.get_default_graph() 
saver = tf.train.import_meta_graph(os.path.join(model_dir, "model.meta")) 
sess = tf.Session() 
saver.restore(sess, tf.train.latest_checkpoint(model_dir)) 

batchX_placeholder = graph.get_tensor_by_name("batchX_placeholder:0") 
batchY_placeholder = graph.get_tensor_by_name("batchY_placeholder:0") 
prediction = graph.get_tensor_by_name("prediction/Softmax:0") 
argmax_pred = tf.argmax(prediction, 1) 

编辑1:我发现我不是为什么会得到不同的结果清晰。

而当我在推理模式下运行代码时,每次启动它时我都会得到不同的结果 。

注意,虽然你从所加载的模型使用的权,你又创建BiRNN,和BasicLSTMCell也有重量,你不从你加载的模型设置其他变量,因此他们需要初始化(使用新的随机值)再次导致未经训练的模型。

相关问题