Tensorflow:恢复会话时的结果不同

时间:2016-07-29 00:22:37

标签: tensorflow lstm

在一个选项卡中,我运行并保存模型,然后在另一个选项卡中加载之前保存的会话。 然后,我使用加载的会话来预测数据集上的值。当打印到终端时,可以看到模型的权重相同。但奇怪的是,加载会话的预测非常不准确,并且与受过训练的会话完全不同。 这是一个经常性的网络。任何想法出了什么问题?



def dynamicRNN(x, seqlen, weights, biases):

    # Prepare data shape to match `rnn` function requirements
    # Current data input shape: (batch_size, n_steps, n_input)
    # Required shape: 'n_steps' tensors list of shape (batch_size, n_input)

    # Permuting batch_size and n_steps
    x = tf.transpose(x, [1, 0, 2])
    # Reshaping to (n_steps*batch_size, n_input)
    x = tf.reshape(x, [-1,n_input])
    #x  = tf.matmul(x, weights['hidden'])+ biases['hidden']
    # Split to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    x = tf.split(0, n_steps, x)

    # Define a lstm cell with tensorflow
    lstm_cell = rnn_cell.BasicLSTMCell(n_hidden, forget_bias=1.0)

    # Get lstm cell output, providing 'sequence_length' will perform dynamic
    # calculation.
    outputs, states = tf.nn.rnn(lstm_cell, x, dtype=tf.float32, sequence_length=seqlen)

    # When performing dynamic calculation, we must retrieve the last
    # dynamically computed output, i.e, if a sequence length is 10, we need
    # to retrieve the 10th output.
    # However TensorFlow doesn't support advanced indexing yet, so we build
    # a custom op that for each sample in batch size, get its length and
    # get the corresponding relevant output.

    # 'outputs' is a list of output at every timestep, we pack them in a Tensor
    # and change back dimension to [batch_size, n_step, n_input]
    outputs = tf.pack(outputs)
    outputs = tf.transpose(outputs, [1, 0, 2])

    # Hack to build the indexing and retrieve the right output.
    batch_size = tf.shape(outputs)[0]
    # Start indices for each sample
    index = tf.range(0, batch_size) * n_steps + (seqlen - 1)
    # Indexing
    outputs = tf.gather(tf.reshape(outputs, [-1, n_hidden]), index)

    # Linear activation, using outputs computed above
    return tf.matmul(outputs, weights['out']) + biases['out']

def RNNpred(test_x, test_seqlen):
    saver.restore(sess,'./variables/dynamicRNNreturnReason.ckpt')
    y_prob, y_pred= sess.run([pred, y_p], feed_dict={x: test_x, seqlen: test_seqlen})
    return y_prob, y_pred

n_steps = 10
n_input = 14
n_classes = 6
n_hidden = 100
weights = {'out': tf.Variable(tf.random_normal([n_hidden, n_classes]), name='W')}
biases = {'out': tf.Variable(tf.random_normal([n_classes]), name='b')}
x = tf.placeholder("float", [None, n_steps, n_input])
y = tf.placeholder("float", [None, n_classes])
seqlen = tf.placeholder(tf.int32, [None])
saver = tf.train.Saver()
sess = tf.Session()
pred = dynamicRNN(x, seqlen, weights, biases)
y_p =tf.argmax(pred,1)
init = tf.initialize_all_variables()
sess.run(init)




0 个答案:

没有答案