【问题标题】:Matrix multiplication in pyTorchpyTorch 中的矩阵乘法
【发布时间】:2019-04-29 00:15:15
【问题描述】:

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

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

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

【问题讨论】:

    标签: python neural-network pytorch activation-function


    【解决方案1】:

    逐步考虑:

    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 从张量中得出一个完整的总和,而不是按行或按列。

    【讨论】:

      【解决方案2】:
      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))
      

      【讨论】:

      • @Ravikankt。我想更好地理解您对 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))。为什么最后的矩阵乘法不会产生错误(由于矩阵维度)?
      • @Tin 检查此页面kite.com/python/docs/torch.matmul。它可能会消除您的疑虑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2019-10-29
      • 2019-10-13
      • 2022-11-17
      相关资源
      最近更新 更多