pytorch学习笔记(一)

一、索引与切片

1.index_select:

给定如下一个a:

a = np.arange(2*2*2*2)
a = torch.from_numpy(a)
a = torch.reshape(a, [2,2,2,2])
a

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

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


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

             [[12, 13],
              [14, 15]]]], dtype=torch.int32)
# 对a的第0个维度进行索引,选择第0个维度索引为1的数据
b = a.index_select(0, torch.tensor([1]))
b

out:tensor([[[[ 8,  9],
              [10, 11]],

             [[12, 13],
              [14, 15]]]], dtype=torch.int32)

2.masked_select

给定如下x:

x = torch.rand(3, 4)
x

out:tensor([[0.4960, 0.0145, 0.9789, 0.6595],
        	[0.2666, 0.2241, 0.1380, 0.3301],
        	[0.9281, 0.8387, 0.1143, 0.2890]])
# 筛选出x中大于0.5的元素,得到掩码mask
mask = x.ge(0.5)
mask

out:tensor([[0, 0, 1, 1],
       	 	[0, 0, 0, 0],
       	 	[1, 1, 0, 0]], dtype=torch.uint8)
# 根据mask从x中取出元素
b = torch.masked_select(x, mask)
b

out:tensor([0.9789, 0.6595, 0.9281, 0.8387])
3. select from flatten index
# 建立一个2 * 3的tensor
src = torch.tensor([[1,2,3], [4,5,6]])
src

out:tensor([[1, 2, 3],
        	[4, 5, 6]])
# 将2 * 3的tensor flatten之后选出其中的第0, 2, 4个元素
b = torch.take(src, torch.tensor([0, 2, 4]))
b

out:tensor([1, 3, 5])

二、维度变换

1. view(reshape)
# 假如a表示 batch * C * W * H
a = torch.rand(32, 3, 28, 28)
a.shape

out: torch.Size([32, 3, 28, 28])
# 将a打平,即将每张图片的CWH通道合并在一起
b = a.view(32, 3 * 28 * 28)
b.shape

out:torch.Size([32, 2352])
# 将打平的数据恢复到原来的维度
print(torch.equal(b.view(32, 3, 28, 28), a))

out: True
2.unsqueeze
# 在第0维前面插入一个维度
In [5]: a.unsqueeze(0).shape
Out[5]: torch.Size([1, 32, 3, 28, 28])
# 在最后一维后面插入一个维度
In [6]: a.unsqueeze(-1).shape
Out[6]: torch.Size([32, 3, 28, 28, 1])

# 给f添加一个bias,需要先将b的维度增加到与f一致(此操作并未完成。。。)
In [7]: f = torch.rand(4, 32, 14, 14)
In [8]: b = torch.rand(32)
In [9]: b = b.unsqueeze(1).unsqueeze(2).unsqueeze(0)
In [10]: b.shape
Out[10]: torch.Size([1, 32, 1, 1])

3.squeeze

# 此处将b中通道数为0的维度全部“挤压”
In [11]: b.squeeze().shape
Out[11]: torch.Size([32])

# 然而通道数为32并无法挤压,但是也不会报错
In [12]: b.squeeze(1).shape
Out[12]: torch.Size([1, 32, 1, 1])
4.expand
# 继续之前未完成的操作,将b中各维度的通道数扩展到与f一致
In [13]: b.shape
Out[13]: torch.Size([1, 32, 1, 1])

In [14]: f.shape
Out[14]: torch.Size([4, 32, 14, 14])

In [15]: b.expand(4,32,14,14).shape
Out[15]: torch.Size([4, 32, 14, 14])

5.repeat

# 使用repeat操作也可以完成扩展
# repeat与expand的不同之处是,repeat会主动的对数据进行复制,占用了较多内存,而且执行速度慢
# repeat的参数的意义是拷贝的次数
In [16]: b.shape
Out[16]: torch.Size([1, 32, 1, 1])

In [17]: b.repeat(4, 1, 4, 4).shape
Out[17]: torch.Size([4, 32, 4, 4])

6.transpose

# a : BCWH
In [19]: a.shape
Out[19]: torch.Size([32, 3, 28, 28])
# BCWH -> BHWC
In [20]: a.transpose(1, 3).shape
Out[20]: torch.Size([32, 28, 28, 3])
# 将a通道交换并将其flatten 

a.transpose(1, 3).contiguous().view(32, 3*28*28).shape
Out[7]: torch.Size([32, 2352])

# 一顿操作,将BCWH->BHWC之后将其打平,再将其恢复到BHWC之后再变为BCWH
a2 = a.transpose(1, 3).contiguous().view(32, 3*28*28).view(32, 28, 28, 3).transpose(1,3)
a2.shape
Out[9]: torch.Size([32, 3, 28, 28])
    
a.shape
Out[10]: torch.Size([32, 3, 28, 28])
# 最后可以看到是能恢复到之前的样子,这样便于变化操作之后再恢复到原样    
print(torch.equal(a, a2))
Out[11]:True

7.permute

# 实际中其实是需要将BCWH转换为BWHC,如果用transpose需要两次操作,这里可以用permute方便实现
a.permute(0, 2, 3, 1).shape
Out[12]: torch.Size([32, 28, 28, 3])
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值