点乘/hadamard product
使用 torch.mul
。
In [14]: import numpy as np
In [15]: a = torch.from_numpy(np.arange(12).reshape(3, 4))
In [16]: a
Out[16]:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]], dtype=torch.int32)
In [17]: b = torch.from_numpy(np.arange(12).reshape(3, 4))
In [18]: c = torch.mul(a, b)
In [19]: c
Out[19]:
tensor([[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]], dtype=torch.int32)
使用 torch.mm
进行矩阵乘法matrix multiply。
In [21]: a
Out[21]:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]], dtype=torch.int32)
In [22]: b = torch.from_numpy(np.arange(12).reshape(4, 3))
In [23]: d = torch.mm(a, b)
In [24]: d
Out[24]:
tensor([[ 42, 48, 54],
[114, 136, 158],
[186, 224, 262]], dtype=torch.int32)