1、支持的数据格式:

batch_size,input_channel,input_h,input_w

2、随机生成一个4维张量,将其输入到模型内,验证模型的尺寸匹配

input_tensor = torch.rand(4, 4, 128, 128)随机生成一个4维张量,将其输入到模型内

3、修改预训练模型

https://blog.csdn.net/weixin_42118374/article/details/103761795

4、variable是torch的数据类型、

grad_fn记录了张量求导过程中的运算方法
张量的8个属性:data、dtype、shape、device等还有四个和梯度求导相关
张量的创建

5、张量拼接方法

torch.cat,torch.stack,#前者不拓展维度,后者拓展

张量拆分

torch.chunk,torch.split

张量索引

idx=torch.tensor([0,2],dtype=torch.long)# 张量中的idx数据类型必须是torch.long
torch.index_selcet(tensor,dim,index=idx)# 索引出的张量进行dim维度的拼接

6、张量中的torch.gather

https://blog.csdn.net/Apikaqiu/article/details/104253080

举例子:

import torch
b = torch.Tensor([[1,2,3],[4,5,6]])
print (b)
index_1 = torch.LongTensor([[0,1],[2,0]])
index_2 = torch.LongTensor([[0,1,1],[0,0,0]])
print (torch.gather(b, dim=1, index=index_1))  # 按行
print (torch.gather(b, dim=0, index=index_2))  # 按列

7、index的大小就是输出的大小

输出

tensor([[1., 2., 3.],
    [4., 5., 6.]])
tensor([[1., 2.],
    [6., 4.]])
tensor([[1., 5., 6.],
    [1., 2., 3.]])

一直理解不了index_2,看到上面那个博主的解释,终于理解了,
其实是按照index的尺寸得到的尺寸,dim也相当于索引,只是一个维度上的索引。按照dim这个索引得到的是行或者列的信息,按照index的数据得到另一个维度的索引
index_2的解释就是:
dim是0,
按列取,0是第一列第0个(1)
1是第二列第1个(5)
1是第三列第1个(6)
0是第一列第0个(1)
0是第二列第0个(2)
0是第三列第0个(3)
dim取得列是按顺序的,第几个是按照索引顺序,从0开始
验证一下

import torch
b = torch.Tensor([[3,9,6],[4,22,41],[1,0,3]])
index_1 = torch.LongTensor([[0,1,1],[0,0,0],[0,1,0]])
index_2 = torch.LongTensor([[0,1,1],[0,0,1],[1,0,2]])
print (torch.gather(b, dim=1, index=index_1))  # 按行
print (torch.gather(b, dim=0, index=index_2))  # 按列
[[3,9,9],[4,4,4],[1,0,1]]
[[3,22,41],[3,9,41],[4,9,3]]

7、代码解释

LambdaLayer(lambda x:
                            F.pad(x[:, :, ::2, ::2], (0, 0, 0, 0, planes//4, planes//4), "constant", 0))

第一个参数:x是个张量,取x0,1维全部,2,3维的奇数
第二个参数:两个一组,第一组是在倒数第一维pad,第二组是在倒数第二维pad,第三组是在倒数第三维pad
源码:

p1d = (1, 1) # pad last dim by 1 on each side
p2d = (1, 1, 2, 2)# pad last dim by (1, 1) and 2nd to last by (2, 2)
p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐