简体   繁体   English

Tensorflow:Tensordot可重现的结果

[英]Tensorflow: Tensordot reproducible results

I am playing around with tf.tensordot in Tensorflow. 我在tf.tensordot中玩tf.tensordot。 However, I am experiencing some inconsistencies which are bugging me. 但是,我遇到了一些困扰我的矛盾之处。 Below is a reproducible example: 下面是一个可重现的示例:

tf.reset_default_graph()
tf.set_random_seed(42)
np.random.seed(42)
X = np.random.rand(150, 196, 268).astype(np.float32)
W = tf.Variable(initial_value=tf.random_normal([268, 22], stddev=0.1))
dotted_150 = tf.tensordot(X, W, axes=[[2], [0]])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output_150 = sess.run(dotted_150)

This returns a tensor that has dimensions (150, 196, 22) 这将返回张量为(150, 196, 22)的张量

tf.reset_default_graph()
tf.set_random_seed(42)
np.random.seed(42)
X = np.random.rand(1, 196, 268).astype(np.float32)
W = tf.Variable(initial_value=tf.random_normal([268, 22], stddev=0.1))
dotted_1 = tf.tensordot(X, W, axes=[[2], [0]])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output_1 = sess.run(dotted_1)

This returns a tensor that has dimensions (1, 196, 22) 这将返回一个张量为(1, 196, 22)的张量

Now, if we test whether the first element from output_150 is almost equal to the first and only element from output_1 , the result is a mismatch between the two arrays. 现在,如果我们测试从第一元件是否output_150几乎等于从所述第一和唯一的元件output_1 ,结果是两个阵列之间的失配。

np.testing.assert_allclose(output_1[0], output_150[0])

On the other hand, if we do: 另一方面,如果我们这样做:

np.random.seed(42)
input_150 = np.random.rand(150, 196, 268).astype(np.float32)
np.random.seed(42)
input_1 = np.random.rand(1, 196, 268).astype(np.float32)
np.testing.assert_equal(input_150[0], input_1[0])

We see that the inputs are exactly the same. 我们看到输入完全相同。 With that said, I would expect that the outputs from the tf.tensordot to be the same as well and they are not. 话虽如此,我希望tf.tensordot的输出也一样,而tf.tensordot并非如此。


On the same note, here is a tf.tensordot equivalent using tf.reshape and tf.matmul : 同样,这是使用tf.reshapetf.matmultf.tensordot等效tf.matmul

tf.reset_default_graph()
tf.set_random_seed(42)
np.random.seed(42)
X = np.random.rand(150, 196, 268).astype(np.float32)
W = tf.Variable(initial_value=tf.random_normal([268, 22], stddev=0.1))
reshaped = tf.reshape(X, [-1, 268])
mulled_150 = tf.reshape(tf.matmul(reshaped, W), [-1, 196, 22])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output_150 = sess.run(mulled_150)


tf.reset_default_graph()
tf.set_random_seed(42)
np.random.seed(42)
X = np.random.rand(1, 196, 268).astype(np.float32)
W = tf.Variable(initial_value=tf.random_normal([268, 22], stddev=0.1))
reshaped = tf.reshape(X, [-1, 268])
mulled_1 = tf.reshape(tf.matmul(reshaped, W), [-1, 196, 22])
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    output_1 = sess.run(mulled_1)

np.testing.assert_allclose(output_1[0], output_150[0])

The outcome is exactly the same, a mismatch between the output arrays. 结果完全相同,输出数组之间不匹配。 How can that be? 怎么可能?

显然,如果我使用tf.float64精度而不是tf.float32则结果是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM