pytorch:相同输入的向量乘法结果不同

时间:2019-05-31 04:12:50

标签: pytorch

我不明白为什么这些乘法输出不同。

Image

print(features*weights)
print('------------')
print(features*weights.view(5,1))
print('------------')
print(torch.mm(features,weights.view(5,1)))

输出:

tensor([[ 0.1314, -0.2796,  1.1668, -0.1540, -2.8442]])
------------
tensor([[ 0.1314, -0.7035, -0.8472,  0.9971, -1.5130],
        [ 0.0522, -0.2796, -0.3367,  0.3963, -0.6013],
        [-0.1809,  0.9688,  1.1668, -1.3733,  2.0837],
        [-0.0203,  0.1086,  0.1308, -0.1540,  0.2336],
        [ 0.2469, -1.3224, -1.5927,  1.8745, -2.8442]])
------------
tensor([[-1.9796]])

2 个答案:

答案 0 :(得分:1)

如果我没记错,您想了解的是:

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))

答案 1 :(得分:0)

似乎featuresweights都是5个向量。
-当使用*运算符简单地将它们相乘时,就会得到它们的逐元素乘法。 -当转置其中一个(使用view()),然后使用*运算符进行逐元素乘法时,Pytorch广播相应的单例尺寸,并得出两个向量的外积:res_ij = w_i * f_j
-最后,您将矩阵乘法torch.mm应用于两个向量,并得到它们的内积。