Pytorch-tensor的转置,运算

1.矩阵的转置

方法:t()

a=torch.randint(1,10,[2,3])
print(a,'\n')
print(a.t())
输出结果
tensor([[2, 8, 2],
        [9, 2, 4]])

tensor([[2, 9],
        [8, 2],
        [2, 4]])

transpose(维度下标1,维度下标2):任意两个维度之间的转换

a=torch.randint(1,10,[2,3,4,5])
print(a.shape)
a1=a.transpose(1,3)
print(a1.shape)
输出结果
torch.Size([2, 3, 4, 5])
torch.Size([2, 5, 4, 3])

permute(维度的下标):所有维度之间的任意转换

a=torch.randint(1,10,[2,3,4,5])
print(a.shape)
a1=a.permute(2,3,1,0)
print(a1.shape)
输出结果
torch.Size([2, 3, 4, 5])
torch.Size([4, 5, 3, 2])
2.矩阵的四则运算

矩阵的加法:2行3列矩阵+2行3列矩阵:

a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[2,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')
输出结果
tensor([[4, 1, 8],
        [6, 7, 4]])

tensor([[9, 7, 1],
        [5, 1, 6]])

tensor([[13,  8,  9],
        [11,  8, 10]])

2行3列矩阵+1行3列矩阵:会先将第二个矩阵复制一行,然后再相加

a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[1,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')
输出结果
tensor([[9, 2, 3],
        [2, 7, 9]])

tensor([[7, 8, 2]])

tensor([[16, 10,  5],
        [ 9, 15, 11]])

2行1列矩阵+1行3列矩阵:会先将第一个矩阵复制成三列,然后将第二个矩阵复制成两行,再进行相加

a=torch.randint(1,10,[2,1])
b=torch.randint(1,10,[1,3])
print(a,'\n')
print(b,'\n')
print(a+b,'\n')
输出结果
tensor([[3],
        [2]])

tensor([[4, 2, 5]])

tensor([[7, 5, 8],
        [6, 4, 7]])

cat(所要相加的矩阵,维度):两个矩阵的某个维度相加

除了相加的维度之外,其余的维度的值必须相同

a=torch.randint(1,10,[2,3])
b=torch.randint(1,10,[1,3])
print(a.shape)
print(b.shape)
print(torch.cat([a,b],dim=0).shape,'\n')
输出结果
torch.Size([2, 3])
torch.Size([1, 3])
torch.Size([3, 3])

stack():会在所相加维度之前加一个2维的维度,用于两个tensor相加,但不想合并。

a=torch.randint(1,10,[1,3])
b=torch.randint(1,10,[1,3])
print(a.shape)
print(b.shape)
print(torch.stack([a,b],dim=0).shape)
print(torch.stack([a,b],dim=1).shape)
输出结果
torch.Size([1, 3])
torch.Size([1, 3])
torch.Size([2, 1, 3])
torch.Size([1, 2, 3])

矩阵的外积

a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
print(a)
print(b)
print(a*b)
输出结果
tensor([[1, 2],
        [3, 4]])
tensor([[1, 2],
        [3, 4]])
tensor([[ 1,  4],
        [ 9, 16]])

matmul(矩阵a,矩阵b): 计算矩阵的内积(推荐)
@:计算矩阵的内积

a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
print(a)
print(b)
print(torch.matmul(a,b))    #推荐使用此方法
print(a@b)  # 不推荐
输出结果
tensor([[1, 2],
        [3, 4]])
tensor([[1, 2],
        [3, 4]])
tensor([[ 7, 10],
        [15, 22]])
tensor([[ 7, 10],
        [15, 22]])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gy-7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值