torch二维矩阵相乘和对应位相乘

import torch

if __name__ == '__main__':

    a_1 = torch.tensor([1, 2, 3, 4, 5])
    b_1 = torch.tensor([1, 0, 1, 1, 0])
    # result1 = torch.mm(a_1,b_1)#此处会出错
    result2 = torch.mul(a_1,b_1)
    result3 = a_1*a_1#对应位相乘
    # print(result1)
    print("result2:" , result2)
    print("result3:" , result3)

    a_2 = torch.tensor([[1,1],
                      [2,2]])
    b_2 = torch.tensor([[1,1],
                     [0,2]])
    result4 = torch.mm(a_2,b_2)#矩阵相乘
    result5 = torch.mul(a_2,b_2)#对应位相乘
    print("result4:" , result4)
    print("result5:" , result5)

错误输出:

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

删除错误语句

result2: tensor([1, 0, 3, 4, 0])
result3: tensor([ 1,  4,  9, 16, 25])
result4: tensor([[1, 3],
        [2, 6]])
result5: tensor([[1, 1],
        [0, 4]])
发布了41 篇原创文章 · 获赞 44 · 访问量 7649

猜你喜欢

转载自blog.csdn.net/tailonh/article/details/105313783