没有转置的指定维度的张量积
Tensor product of specified dimension without transpose
假设我有一个大小为 (a, b, c)
的张量 t1
和另一个大小为 (c, d)
的张量 t2
。有没有办法在不使用 tf.transpose
操作的情况下将它们相乘并获得大小为 (a, d, b)
(不是 (a, b, d)
)的张量 t3
?
工作示例:
import tensorflow as tf # version 2.1.0
t1 = tf.constant(tf.reshape(range(24), (2, 3, 4)))
t2 = tf.constant(tf.reshape(range(20), (4, 5)))
t3 = tf.transpose(tf.tensordot(t1, t2, axes=[[2], [0]]), [0, 2, 1]) # shape = (2, 5, 3)
我想要的是从 t1
和 t2
得到 t3
而不是使用 tf.transpose
,这据说很昂贵 (link 1, )。
我在 Python 3.7.
中使用 Tensorflow 2.1.0
import tensorflow as tf
t1 = tf.constant(tf.reshape(range(24), (2, 3, 4)))
t2 = tf.constant(tf.reshape(range(20), (4, 5)))
t3 = tf.linalg.matmul(t2, t1, transpose_a=True, transpose_b=True)
这是一种无需实际计算和存储转置即可执行矩阵乘法的有效方法。
假设我有一个大小为 (a, b, c)
的张量 t1
和另一个大小为 (c, d)
的张量 t2
。有没有办法在不使用 tf.transpose
操作的情况下将它们相乘并获得大小为 (a, d, b)
(不是 (a, b, d)
)的张量 t3
?
工作示例:
import tensorflow as tf # version 2.1.0
t1 = tf.constant(tf.reshape(range(24), (2, 3, 4)))
t2 = tf.constant(tf.reshape(range(20), (4, 5)))
t3 = tf.transpose(tf.tensordot(t1, t2, axes=[[2], [0]]), [0, 2, 1]) # shape = (2, 5, 3)
我想要的是从 t1
和 t2
得到 t3
而不是使用 tf.transpose
,这据说很昂贵 (link 1,
我在 Python 3.7.
中使用 Tensorflow 2.1.0import tensorflow as tf
t1 = tf.constant(tf.reshape(range(24), (2, 3, 4)))
t2 = tf.constant(tf.reshape(range(20), (4, 5)))
t3 = tf.linalg.matmul(t2, t1, transpose_a=True, transpose_b=True)
这是一种无需实际计算和存储转置即可执行矩阵乘法的有效方法。