Advertisement

在 Pytorch 中, 点积由 torch.mul 实现, 矩阵相乘由 torch.mm 完成

阅读量:

torch.mul() 点乘
指的是对矩阵中的各个元素进行逐个对应的相乘操作

复制代码
    import torch
    
    input1 = torch.Tensor([[1,2],[3,4]])
    input2 = torch.Tensor([[2,2],[2,2]])
    print(input1.shape)
    print(input2.shape)
    output = torch.mul(input1,input2)
    print(output)
    print(output.shape)
    
    
      
      
      
      
      
      
      
      
      
    
复制代码
    torch.Size([2, 2])
    torch.Size([2, 2])
    tensor([[2., 4.],
        [6., 8.]])    # 看结果
    torch.Size([2, 2])
    
    
      
      
      
      
      
    

torch.mm() 矩阵乘法
在进行矩阵相乘操作时,需确保第一个矩阵的列数与第二个矩阵的行数

全部评论 (0)

还没有任何评论哟~