pyTorch 中的矩阵乘法

Matrix multiplication in pyTorch

我正在 pyTorch 中编写一个简单的神经网络,其中特征和权重都是 (1, 5) 张量。我在下面提到的两种方法有什么区别?

y = activation(torch.sum(features*weights) + bias)

yy = activation(torch.mm(features, weights.view(5,1)) + bias)

逐步考虑:

x = torch.tensor([[10, 2], [3,5]])
y = torch.tensor([[1,3], [5,6]])

x * y
# tensor([[10,  6],
#         [15, 30]])

torch.sum(x*y)

#tensor(61)

x = torch.tensor([[10, 2], [3,5]])
y = torch.tensor([[1,3], [5,6]])

np.matmul(x, y)
# array([[20, 42],
#       [28, 39]])

所以matmul* operator是有区别的。此外,torch.sum 从张量中得出一个完整的和,而不是明智地行或列。

features = torch.rand(1, 5) 
weights = torch.Tensor([1, 2, 3, 4, 5])
print(features)
print(weights)

# Element-wise multiplication of shape (1 x 5)
# out = [f1*w1, f2*w2, f3*w3, f4*w4, f5*w5]
print(features*weights)

# weights has been reshaped to (5, 1)
# Element-wise multiplication of shape (5 x 5)
# out =   [f1*w1, f2*w1, f3*w1, f4*w1, f5*w1]
#         [f1*w2, f2*w2, f3*w2, f4*w2, f5*w2]
#         [f1*w3, f2*w3, f3*w3, f4*w3, f5*w3]
#         [f1*w4, f2*w4, f3*w4, f4*w4, f5*w4]
#         [f1*w5, f2*w5, f3*w5, f4*w5, f5*w5]
print(features*weights.view(5, 1))

# Matrix-multiplication
# (1, 5) * (5, 1) -> (1, 1)
# out = [f1*w1 + f2*w2 + f3*w3 + f4*w4 + f5*w5]
print(torch.mm(features, weights.view(5, 1)))

输出

tensor([[0.1467, 0.6925, 0.0987, 0.5244, 0.6491]])  # features
tensor([1., 2., 3., 4., 5.])                        # weights

tensor([[0.1467, 1.3851, 0.2961, 2.0976, 3.2455]])  # features*weights
tensor([[0.1467, 0.6925, 0.0987, 0.5244, 0.6491],
        [0.2934, 1.3851, 0.1974, 1.0488, 1.2982],
        [0.4400, 2.0776, 0.2961, 1.5732, 1.9473],
        [0.5867, 2.7701, 0.3947, 2.0976, 2.5964],
        [0.7334, 3.4627, 0.4934, 2.6220, 3.2455]])  # features*weights.view(5,1)
tensor([[7.1709]])                                  # torch.mm(features, weights.view(5, 1))