【问题标题】:Dot product between constant vector and variable with batch size in TensorflowTensorflow中常量向量和具有批量大小的变量之间的点积
【发布时间】:2021-02-25 19:20:35
【问题描述】:
在张量流中我有:
- 一个由 2000 个向量组成的常数,维数为 1500。(dim = (2000, 1500)) 命名为 X
- 名为 y 的 75 个维度为 1500 (dim = (?, 75, 1500)) 的向量的批量输入变量
我希望 X 的每个向量在 y 的每个向量之间的点积得到一个维度为 (?, 75, 2000) 的向量
有没有办法使用点或批处理点来做到这一点?
【问题讨论】:
标签:
tensorflow
keras
vector
tensorflow2.0
【解决方案1】:
是的。
使用tf.matmul()。它将适用于未知批次。
import tensorflow as tf
# random X
X = tf.random.normal([2000, 1500])
print(X.shape)
# (2000, 1500)
# variable-batch y
y = tf.keras.Input([75, 1500])
print(y.shape)
# (None, 75, 1500)
# dot-product
out = tf.matmul(y, X, transpose_b=True)
print(out.shape)
# (None, 75, 2000)