Python pyTorch中的矩阵乘法

Python pyTorch中的矩阵乘法,python,neural-network,pytorch,activation-function,Python,Neural Network,Pytorch,Activation Function,我正在用pyTorch编写一个简单的神经网络,其中特征和权重都是(1,5)张量。我在下面提到的两种方法之间的区别是什么 y = activation(torch.sum(features*weights) + 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

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

y = activation(torch.sum(features*weights) + 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
*操作符之间存在差异。此外,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))

@拉维坎特。我想更好地理解您对2D矩阵的回答,并得出以下代码:
X=torch.arange(6.view(2,3)w=torch.tensor([1,2,3])print(torch.matmul(X,w.view(3,1)))print(torch.matmul(X,w))
。为什么最后一次矩阵乘法没有产生错误(由于矩阵维数)?@t请检查此页。这可能会消除你的疑虑。
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))