pytorch:tensor维度变化

import torch
import numpy as np

维度变换

a = torch.randint(1,10,[4,1,3,6]) #4张照片,1个通道(即灰度图片),长是3个pixel,宽是6个pixel
a.reshape(-1,3,6) #reshape只要保证numel相同就可以了
tensor([[[6, 1, 8, 2, 3, 8],
         [1, 7, 6, 9, 2, 7],
         [1, 6, 2, 5, 7, 6]],

        [[9, 4, 3, 9, 4, 2],
         [4, 6, 8, 7, 7, 1],
         [1, 3, 9, 7, 1, 8]],

        [[6, 4, 5, 3, 6, 4],
         [7, 9, 7, 1, 2, 1],
         [1, 5, 7, 6, 5, 4]],

        [[5, 6, 4, 2, 1, 6],
         [9, 5, 4, 9, 3, 8],
         [9, 1, 2, 4, 5, 8]]])
  • squeeze and unsqueeze
a = torch.randint(1,10,[3,4])
a
tensor([[6, 6, 9, 7],
        [6, 5, 1, 5],
        [6, 3, 8, 9]])
a.unsqueeze(0)  #unsqueeze括号里面填的是维数,取值范围是【-a.dim()-1,a.dim()】比如本题中就是-3~3。0表示在第1个维前面增加一个维度即变成1*3*4
tensor([[[6, 6, 9, 7],
         [6, 5, 1, 5],
         [6, 3, 8, 9]]])
a.unsqueeze(1) #1表示在第2个维前面加一个维度,变成3*1*4
tensor([[[6, 6, 9, 7]],

        [[6, 5, 1, 5]],

        [[6, 3, 8, 9]]])
a.unsqueeze(2) #2表示在第三个维度前加一个维度,由于a.dim()=2,应该是没有第三个维度,所以a.unsqueeze(a.dim())是在a的最后增加一个维度 本例中变成3*4*1
tensor([[[6],
         [6],
         [9],
         [7]],

        [[6],
         [5],
         [1],
         [5]],

        [[6],
         [3],
         [8],
         [9]]])
a.unsqueeze(-1).shape #4对应的是-1,则在4后面增加一个维度,变成3*4*1
torch.Size([3, 4, 1])
a.unsqueeze(-3).shape #a.dim()=2 所以-3就表示在第一个维度前面增加一个维度 变成1*3*4
torch.Size([1, 3, 4])
b=a.reshape(2,1,2,3)
print(b.shape)
b
torch.Size([2, 1, 2, 3])





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


        [[[1, 5, 6],
          [3, 8, 9]]]])
print(b.squeeze())
print(b.squeeze().shape)#不写入内容的话,就把是“1”的全部去掉.squeeze括号中可以填的数的范围是【-a.dim(),a.dim()-1】
tensor([[[6, 6, 9],
         [7, 6, 5]],

        [[1, 5, 6],
         [3, 8, 9]]])
torch.Size([2, 2, 3])
b.squeeze(0).shape #0表示挤压掉第一个维度,但是第一个维度不是“1”,所以挤不掉(squeeze只能挤掉“1”,UNsqueeze只能增加“1”)
torch.Size([2, 1, 2, 3])
b.squeeze(1).shape #1表示挤压掉第二个维度,因为第二个维度是“1”,所以被成功挤压掉
torch.Size([2, 2, 3])
c = a.reshape(1,1,6,2,1)
c.squeeze(-5).shape
torch.Size([1, 6, 2, 1])
c.squeeze(4).shape
torch.Size([1, 1, 6, 2])
  • expand and repeat
a = torch.randint(1,10,[2,4,5])
b = torch.randint(1,10,[2,1,1])
b
tensor([[[5]],

        [[1]]])
b.expand(2,4,5) #b是2*1*1 要想变成2*4*5 则在行上重复4次,列上重复5次 (ps:只能在size=1的维度上进行重复)
tensor([[[5, 5, 5, 5, 5],
         [5, 5, 5, 5, 5],
         [5, 5, 5, 5, 5],
         [5, 5, 5, 5, 5]],

        [[1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1],
         [1, 1, 1, 1, 1]]])
b.expand(2,-1,3) #-1表示这个维度上的size不变(expand可以用-1,repeat不行)
tensor([[[5, 5, 5]],

        [[1, 1, 1]]])
b.repeat(2,1,3) #repeat表示要在这个维度上重复几次
tensor([[[5, 5, 5]],

        [[1, 1, 1]],

        [[5, 5, 5]],

        [[1, 1, 1]]])
  • transpose and permute
a = torch.arange(24).reshape(2,3,4)
a.transpose(1,0).contiguous().reshape(2,3,4)
tensor([[[ 0,  1,  2,  3],
         [12, 13, 14, 15],
         [ 4,  5,  6,  7]],

        [[16, 17, 18, 19],
         [ 8,  9, 10, 11],
         [20, 21, 22, 23]]])
    • 填1,0或者0,1 都可以 原来的Xijk 变成了Xjik
a.permute(2,1,0).shape #(2,1,0)表示原来第三个维度的size变成了第一个维度的size,第二个维度的size还是第二个维度的size,第一个维度的size变成了第三个维度的size
torch.Size([4, 3, 2])
a.permute(1,2,0).shape#第二个维度的size(3)变成了第一个维度的size,第三个维度的size(4)变成了第二个维度的size,第一个维度的size(2)变成了第三个维度的size
torch.Size([3, 4, 2])
发布了43 篇原创文章 · 获赞 1 · 访问量 761

猜你喜欢

转载自blog.csdn.net/weixin_41391619/article/details/104679630