Pytorch笔记 (3) 科学计算2

一、组织张量的元素

(1)重排张量元素

本节介绍在不改变 张量元素个数 和 各元素的值的情况下改变张量的大小

 

torch.Tensor类的成员方法 reshape()

参数是多个int类型的值。

如果想要把一个张量的大小改成 s[0],s[1],s[2],s[3]....那就让s[0],s[1],s[2],s[3]....作为reshape() 方法的n个参数

 

 使用 reshape()  在不改变元素个数和各元素的值的情况下改变张量大小

tc = torch.arange(12) #张量大小 (12,)
print('tc={}'.format(tc))
t322 = tc.reshape(3,2,2)  #张量大小 (3,2,2)
print('t322={}'.format(t322))
t43 = t322.reshape(4,3)  #张量大小(4,3)
print('t43={}'.format(t43))
tc=tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
t322=tensor([[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]],

        [[ 8,  9],
         [10, 11]]])
t43=tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])

 

在reshape() 参数里,1个维度的大小用 -1 代替。如果某个维度的大小用-1代替,那么该函数就会根据张量总元素的个数 和 其他各维度的元素个数 自动计算这个用 -1指定的维度大小

例如:

t =  torch.arange(24).reshape(2,-1,4)
print(t)
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])

squeeze() 可以消除张量大小中大小为1的维度

t1 =  torch.arange(24).reshape(2,1,3,1,4)
print(t1.squeeze())   #消去一维后,大小为 (2,3,4)
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])

unsqueeze()  增加一些大小为0的维度,增加的维度的位置由关键字参数 dims 指定

t2 =  torch.arange(24).reshape(2,3,4)
print(t2.unsqueeze(dim=2))

 

tensor([[[[ 0,  1,  2,  3]],

         [[ 4,  5,  6,  7]],

         [[ 8,  9, 10, 11]]],


        [[[12, 13, 14, 15]],

         [[16, 17, 18, 19]],

         [[20, 21, 22, 23]]]])

 (2)张量的交换

张量的交换是将张量的各维度重新排列

permute () 交换

tt = torch.arange(24).reshape(1,2,3,4)   #大小 = (1,2,3,4)
print(tt.permute(dims=[2,0,1,3]))  #大小 = (3,1,2,4)      即:交换原维度的第2 ..0 ..1..3。。。个维度

 

 

transpose() 转置      t()  二维张量装置

t12 = torch.tensor([[5.,-9.],])
print('t12={}'.format(t12))
t21 = t12.transpose(0,1)
print('t21={}'.format(t21))
t21 = t12.t()
print('t21={}'.format(t21))
t12=tensor([[ 5., -9.]])
t21=tensor([[ 5.],
        [-9.]])
t21=tensor([[ 5.],
        [-9.]])

 

 

(3)选取部分张量元素

 index_select() 

这个成员方法有两个参数 ———— 参数dim 是一个int 值 —— 表示对 哪个维度进行处理

参数index —— 是一个 torch.Tensor 实例,表示选取那个维度的哪些指标

 

例如: 调用index_select() 的torch.Tensor() 类实例的大小为(s[0]...s[x]..s[n-1]),dim = 1,index = (x,) 那么选取得到的张量大小 为(s[0]....s[x-1],x,s[x+1]...s[n-1])

t = torch.arange(24).reshape(2,3,4)  #大小 = {2,3,4}
index = torch.tensor([1,2])   #大小 (2,)
print(t.index_select(1,index))  #大小 = {2,2,4}
tensor([[[ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[16, 17, 18, 19],
         [20, 21, 22, 23]]])

 

要对张量的某个维度进行选取 ———— 也可以用  [] ———— 需要给出在所有维度上的指标。在每个维度上,可以选取1个指标、多个指标【包括全部指标】。

t = torch.arange(12)
print(t)
print(t[3])  #选取1个元素,大小(),值为3
print(t[-5])  #选取1个元素,大小(),值为7
print(t[3:6])   #选取连续元素,大小(3,)
print(t[:6])  #选取连续元素,大小(6,)
print(t[3:])  #选取连续元素,大小(9,)
print(t[-5:])  #选取连续元素,大小(5,)
print(t[3:6:2])  #选取不连续元素,大小(2,)
print(t[3::2])  #选取不连续元素,大小(5,)
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
tensor(3)
tensor(7)
tensor([3, 4, 5])
tensor([0, 1, 2, 3, 4, 5])
tensor([ 3,  4,  5,  6,  7,  8,  9, 10, 11])
tensor([ 7,  8,  9, 10, 11])
tensor([3, 5])
tensor([ 3,  5,  7,  9, 11])

对于有多个维度的情况,可以用英文"," 分割各个维度的指标

t = torch.arange(12).reshape(3,4)
print(t)
print(t[2:,-2])  #大小(1,)  值为[10,]
print(t[0,:])  #大小(4,)  值为[0,1,2,3]
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([10])
tensor([0, 1, 2, 3])

 masked_select() 

接受一个张量参数 mask,它的大小必须调和 用masked_selected()  方法的类实例相同,并且元素类型必须为 torch.int8

张量mask 里面的元素非0即1,表示是否要选择对应元素

masked_selected()  将张量mask 选定的那些值以一个一维张量的形式返回,一维张量的元素个数就是张量mask 中1的个数

t = torch.arange(12).reshape(3,4)
mask = torch.tensor([[1,0,0,1],[0,1,1,0],[0,0,1,0]],dtype=torch.uint8)
t.masked_select(mask)   #大小(5,)
tensor([ 0,  3,  5,  6, 10])

 

take()

不再考虑张量的具体大小,而只考虑张量的元素总个数

take() 函数将张量个元素按照唯一的指标进行索引,相当于对经过 reshape(-1) 操作后的张量进行索引

t = torch.arange(12).reshape(3,4)
print(t)
indices = torch.tensor([2,5,6])
print(indices)
print(t.take(indices))  #大小(3,)  值为[2,5,6]

 

tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([2, 5, 6])
tensor([2, 5, 6])

 

 

(4)张量的扩展和拼接

repeat() 

可以将张量的内容进行重复,使张量的大小变大

(s[0],s[1]....s[n-1]) —— repeat() ———> (s[0]r[0],s[1]r[1]....s[n-1]r[n-1])

t12 = torch.tensor([[5.,-9.],])
print('t12={}'.format(t12))   #大小(1,2)
t34 = t12.repeat(3,2)
print(t34)    #大小(3,4)    1*3 = 3 2*2 =4
t12=tensor([[ 5., -9.]])
tensor([[ 5., -9.,  5., -9.],
        [ 5., -9.,  5., -9.],
        [ 5., -9.,  5., -9.]])

 

torch.cat() 

 

 

tp = torch.arange(12).reshape(3,4)
tn = -tp
tc0 = torch.cat([tp,tn],0)
print(tc0)
tc1 = torch.cat([tp,tp,tn,tn],1)
print(tc1)
tensor([[  0,   1,   2,   3],
        [  4,   5,   6,   7],
        [  8,   9,  10,  11],
        [  0,  -1,  -2,  -3],
        [ -4,  -5,  -6,  -7],
        [ -8,  -9, -10, -11]])
tensor([[  0,   1,   2,   3,   0,   1,   2,   3,   0,  -1,  -2,  -3,   0,  -1,
          -2,  -3],
        [  4,   5,   6,   7,   4,   5,   6,   7,  -4,  -5,  -6,  -7,  -4,  -5,
          -6,  -7],
        [  8,   9,  10,  11,   8,   9,  10,  11,  -8,  -9, -10, -11,  -8,  -9,
         -10, -11]])

torch.stack()

同样有 张量列表(或元组)和 维度两个参数

stack() 函数要求输入张量的大小完全相同,得到的张量的维度会比输入的张量的大小多1,并且多出的那个维度就是拼接的维度

tp = torch.arange(12).reshape(3,4)
tn = -tp
ts0 = torch.stack([tn,tp],0)
print(ts0)
ts1 = torch.stack([tp,tp,tn,tn],1)
print(ts1)
tensor([[[  0,  -1,  -2,  -3],
         [ -4,  -5,  -6,  -7],
         [ -8,  -9, -10, -11]],

        [[  0,   1,   2,   3],
         [  4,   5,   6,   7],
         [  8,   9,  10,  11]]])
tensor([[[  0,   1,   2,   3],
         [  0,   1,   2,   3],
         [  0,  -1,  -2,  -3],
         [  0,  -1,  -2,  -3]],

        [[  4,   5,   6,   7],
         [  4,   5,   6,   7],
         [ -4,  -5,  -6,  -7],
         [ -4,  -5,  -6,  -7]],

        [[  8,   9,  10,  11],
         [  8,   9,  10,  11],
         [ -8,  -9, -10, -11],
         [ -8,  -9, -10, -11]]])

 

posted @ 2019-09-08 15:15  远征i  阅读(352)  评论(0编辑  收藏  举报