Python 将结果与tensorflow'混淆;s tensordot
在使用tensorflow的Python 将结果与tensorflow'混淆;s tensordot,python,tensorflow,Python,Tensorflow,在使用tensorflow的tf.tensordot时,我遇到了一些奇怪的结果。运行以下代码块 import tensorflow as tf import numpy as np a = np.arange(6, dtype=np.int32).reshape(3,2) b = np.arange(1,7, dtype=np.int32).reshape(2,3) sess = tf.Session() print(sess.run(tf.tensordot(a, b, [[0,1],[0,1
tf.tensordot
时,我遇到了一些奇怪的结果。运行以下代码块
import tensorflow as tf
import numpy as np
a = np.arange(6, dtype=np.int32).reshape(3,2)
b = np.arange(1,7, dtype=np.int32).reshape(2,3)
sess = tf.Session()
print(sess.run(tf.tensordot(a, b, [[0,1],[0,1]])))
print(sess.run(tf.tensordot(a, b, [[0,1],[1,0]])))
print(sess.run(tf.tensordot(a, b, [[1,0],[0,1]])))
print(sess.run(tf.tensordot(a, b, [[1,0],[1,0]])))
产生
70
65
65
60
我不知道这里发生了什么收缩。另一件有趣的事情是,尝试使用numpy的tensordot执行此操作将返回几个尝试的轴的错误。您在tensorflow中发现了一个错误 根据
tf.tensordot
的文档
对于范围(0,len(a\u轴))
中的所有i
轴a\u轴[i]
必须与b的轴b\u轴[i]
具有相同的尺寸
例如,tf.tensordot(a,b,[[0,1],[0,1]])
应该返回一个错误,因为a
是3x2
而b
是2x3
。但事实并非如此——这就是问题所在
相反,它继续进行,好像张量是相容的。如果a
和b
是兼容的,那么tf.tensordot(a,b,[[0,1],[0,1]])
将是一个简单的点积。tf.tensordot
在内部所做的是,它基本上将a
和b
展平,并计算点积
在您的例子中,a
和b
具有相同数量的元素,因此点积的计算成功,尽管它们具有不兼容的形状
你可以向TF团队提交一个bug