为什么tf.nn.dynamic_rnn返回关于sequence_length的不同结果

时间:2017-08-05 15:08:04

标签: python tensorflow deep-learning

当我在Scikit-Learn和TensorFlow的手动机器学习书中学习RNN时,我遇到了不同的结果,必须是相同的。

第一个代码

while (true) {
    try {
        System.out.println("Press \"1\" to chat" + " & " + "\"2\" to play games" + " & \"3\" to edit the conversations");
        System.out.println("Typing other numbers will end the Chatbot");
        startup = userinput.nextInt();
        switch (startup) {
          // cases in here as before, omitted for brevity
        }
        String returningCode = returnChoiceOfChatbot(startup);
        System.out.println(returningCode);
    } catch (InputMismatchException e) {
        System.out.println("Invalid User Input. Please enter a value from 0 to 4.");
    }
}

结果

n_steps = 2
n_inputs = 3
n_neurons = 5

reset_graph()

X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
basic_cell = tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons)

seq_length = tf.placeholder(tf.int32, [None])
outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)


init = tf.global_variables_initializer()

X_batch = np.array([
        # step 0     step 1
        [[0, 1, 2], [9, 8, 7]], # instance 1
        [[3, 4, 5], [0, 0, 0]], # instance 2 (padded with zero vectors)
        [[6, 7, 8], [6, 5, 4]], # instance 3
        [[9, 0, 1], [3, 2, 1]], # instance 4
    ])

with tf.Session() as sess:
    init.run()
    outputs_val, states_val = sess.run(
        [outputs, states], feed_dict={X: X_batch})

第二个代码

[[[ 0.10273618  0.03536123  0.14367972  0.1572928   0.23754682]
  [ 0.41665766  0.49650002  0.1549654   0.07568012  0.82703578]]

 [[ 0.35701871  0.20796996  0.13533755  0.21938165  0.64902753]
  [ 0.15402019  0.14915846  0.31022152  0.13305351  0.36220491]]

 [[ 0.48224443  0.24930702  0.07341093  0.18052572  0.72496963]
  [ 0.3561081   0.55856758  0.31825539  0.13380432  0.90042865]]

 [[ 0.02311822 -0.16510175  0.49798414 -0.06049323  0.23668778]
  [ 0.28713134  0.16252561  0.4774358   0.07630309  0.50222367]]]

结果

    n_steps = 2
    n_inputs = 3
    n_neurons = 5

    reset_graph()

    X = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
    basic_cell = tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons)

    seq_length = tf.placeholder(tf.int32, [None])
    outputs, states = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32,
    sequence_length=seq_length)

    init = tf.global_variables_initializer()

    X_batch = np.array([
            # step 0     step 1
            [[0, 1, 2], [9, 8, 7]], # instance 1
            [[3, 4, 5], [0, 0, 0]], # instance 2 (padded with zero vectors)
            [[6, 7, 8], [6, 5, 4]], # instance 3
            [[9, 0, 1], [3, 2, 1]], # instance 4
        ])
    seq_length_batch = np.array([2, 1, 2, 2])


    with tf.Session() as sess:
        init.run()
        outputs_val, states_val = sess.run(
            [outputs, states], feed_dict={X: X_batch,seq_length:seq_length_batch})

Tensorflow文档说'如果未提供sequence_length,则假定所有批处理条目都是完整序列;并且对于每个序列,从时间0到max_time应用时间反转。 '

结果必须一样吗?

1 个答案:

答案 0 :(得分:0)

由于您使用的是图形参数的随机初始化,因此输出的方差。