TensorFlow张量上Tensordot操作的数学定义

时间:2019-12-20 15:48:32

标签: tensorflow tensor axes dot-product multiple-axes

我正在尝试对tf.tensordot轴参数的行为进行逆向工程,但是很难。

给出以下代码:

a = tf.constant([[1., 2.], [3., 4.], [4., 5.]])
b = tf.constant([1., 2.])
c = tf.constant([[1., 2.], [2., 3.], [3., 4.]])

print(f'Shape of c: {c.shape}')

ct = tf.transpose(c)

print(f'Shape of ct: {ct.shape}')

print('.................')

d = tf.tensordot(a, ct, axes=1)
print(f'Shape of d: {d.shape}')
print(d)

print('.................')


e = tf.tensordot(a, ct, axes=0)
print(f'Shape of e: {e.shape}')
print(e)


print('.................')


f = tf.tensordot(a, ct, axes=2)
print(f'Shape of f: {f.shape}')
print(e)

我了解“ d”的产生方式,但我不了解“ e”和“ f”的产生方式。 TensorFlow Documentation不足以让我理解。

1 个答案:

答案 0 :(得分:0)

我发布了一个对参数 axes 可以接受的子集的中间答案(当 axes 是整数时):

= 0: 没有加法发生,只有乘法。例如:

c = tf.tensordot(a, b, axes=0)

这意味着如果a的形状为m,n,b的形状为o,p,则c的形状将为m,n,o,p,因为元素是相乘的,而不是相加的

= 1:

c = tf.tensordot(a, b, axes=1)

如果a的形状为m,n,b的形状为n,o,则c的形状将为m,o,这是因为元素相乘,而且也相加了

一旦我对该主题有了更多的了解,就会更新此帖子