两张量相乘
点乘 (矩阵相乘)tensor1.mm(tensor2)
m x n 矩阵 乘以 n x d 得到 m x d 矩阵
data = [[1,2], [3,4], [5, 6]]
tensor = torch.FloatTensor(data)
tensor
Out[31]:
tensor([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
tensor.mm(tensor.t()) # t()是转置
Out[30]:
tensor([[ 5., 11., 17.],
[ 11., 25., 39.],
[ 17., 39., 61.]])
对应元素相乘 (Hadamard product)tensor1.mul(tensor2)
必须保证量张量的维度一致
data = [[1,2], [3,4], [5, 6]]
tensor = torch.FloatTensor(data)
tensor
Out[27]:
tensor([[ 1., 2.],
[ 3., 4.],
[ 5., 6.]])
tensor.mul(tensor)
Out[28]:
tensor([[ 1., 4.],
[ 9., 16.],
[ 25., 36.]])