为什么添加不可用的张量会改变 TensorFlow 中 RNN 单元的结果?

Why adding unusable tensors will change the result of an RNN cell in tensorflow?

这是可以重现问题的最简单的代码:

import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12345)
np.random.seed(12345)
random.seed(12345)

unusable1 = tf.constant(1e-3, tf.float32)
unusable2 = tf.constant(1e-3, tf.float32)
unusable3 = tf.constant(1e-3, tf.float32)

X = tf.placeholder(tf.float32, shape=[2, 3])

cell = tf.contrib.rnn.BasicRNNCell(5)

changed_data = tf.reduce_sum(cell(X, state = tf.zeros((2, 5)))[0])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    output = sess.run(changed_data, feed_dict={X: np.ones((2, 3))})
    print(output)   # = -1.46618

以上代码的结果在我的机器上是-1.46618

但是,如果我注释掉三个不可用的常量张量声明,结果就变成了1.76918!

import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12345)
np.random.seed(12345)
random.seed(12345)

# unusable1 = tf.constant(1e-3, tf.float32)
# unusable2 = tf.constant(1e-3, tf.float32)
# unusable3 = tf.constant(1e-3, tf.float32)

X = tf.placeholder(tf.float32, shape=[2, 3])

cell = tf.contrib.rnn.BasicRNNCell(5)

changed_data = tf.reduce_sum(cell(X, state = tf.zeros((2, 5)))[0])

with tf.Session() as sess:
    tf.global_variables_initializer().run()
    output = sess.run(changed_data, feed_dict={X: np.ones((2, 3))})
    print(output)   # = 1.76918

其实,你可以add/delete/modify任意声明常量张量,结果千差万别!

有什么问题?

变量的初始值设定项获得不同的操作级种子,因为播种基于 (1) 图级种子,以及 (2) 如果未明确设置操作级种子(确定性当前图中先前创建的操作的功能)。这可以防止每个变量在设置图级种子时获得完全相同的初始化。参见 get_seed 实现。