我想将两个高维张量(2,5,3)*(2,5)乘以(2,5,3),将每个行向量乘以标量。
例如
emb = nn.Embedding(6, 3)
input = torch.tensor([[1, 2, 3, 4, 5,],
[2, 3, 1, 4, 5,]])
input_emb = emb(input)
print(input.shape)
> torch.Size([2, 5])
print(input_emb.shape)
> torch.Size([2, 5, 3])
print(input_emb)
> tensor([[[-1.9114, -0.1580, 1.2186],
[ 0.4627, 0.9119, -1.1691],
[ 0.6452, -0.6944, 1.9659],
[-0.5048, 0.6411, -1.3568],
[-0.2328, -0.9498, 0.7216]],
[[ 0.4627, 0.9119, -1.1691],
[ 0.6452, -0.6944, 1.9659],
[-1.9114, -0.1580, 1.2186],
[-0.5048, 0.6411, -1.3568],
[-0.2328, -0.9498, 0.7216]]], grad_fn=<EmbeddingBackward>)
我想乘以如下:
// It is written in this way for convenience, not mathematical true.
// multiply each row vector by a scalar
[[
[-1.9114, -0.1580, 1.2186] * 1
[ 0.4627, 0.9119, -1.1691] * 2
[ 0.6452, -0.6944, 1.9659] * 3
[-0.5048, 0.6411, -1.3568] * 4
[-0.2328, -0.9498, 0.7216] * 5
]
[
[ 0.4627, 0.9119, -1.1691] * 2
[ 0.6452, -0.6944, 1.9659] * 3
[-1.9114, -0.1580, 1.2186] * 1
[-0.5048, 0.6411, -1.3568] * 4
[-0.2328, -0.9498, 0.7216] * 5
]]
除了“多循环”方式之外,如何通过PyTorch
API以简洁的方式实现它?
预先感谢。
答案 0 :(得分:1)
您可以通过正确对齐两个张量的尺寸来实现:
std::addressof