Pytorch-tensor的转置,运算

1.矩阵的转置

方法:t()
a=torch.randint(1,10,[2,3])
print(a,'
')
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,'
')
print(b,'
')
print(a+b,'
')

输出结果

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,'
')
print(b,'
')
print(a+b,'
')

输出结果

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,'
')
print(b,'
')
print(a+b,'
')

输出结果

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

输出结果

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

原文地址:https://www.cnblogs.com/52dxer/p/13771717.html