当前位置:   article > 正文

【Pytorch】常用函数功能介绍和注意事项_torch.max(out,1)

torch.max(out,1)

【持续更新中…】

数据预处理

Variable

from torch.autograd import Variable 
  • 1

作用:自动微分变量,用于构建计算图

网络层定义

torch.nn.BatchNorm2d()

设尺寸为N*C*H*W,其中N代表batchsize,C表示通道数(例如RGB三通道),H,W分别表示feature map的宽高。

torch.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True, device=None, dtype=None)
  • 1
  • num_features:通道数,例如RGB为3
  • eps:一个加至分母的参数,为提高计算稳定性
  • momentum:运行中调整均值、方差的估计参数
  • affine:当设为true时,给定可以学习的系数矩阵\gammaγ和 \beta

torch.nn.Linear()

torch.nn.Linear(in_features: int, out_features: int, bias: bool = True, device=None, dtype=None)
  • 1
  • in_features:输入的二维张量的大小
  • out_features:输出的二维张量的大小

torch.nn.Sequential()

 self.conv4 = torch.nn.Sequential(
            torch.nn.Conv2d(64, 64, 2, 2, 0),  
            torch.nn.BatchNorm2d(64), 
            torch.nn.ReLU()#ReLU激活函数) 
        self.mlp1 = torch.nn.Linear(2 * 2 * 64, 100)#torch.nn.Linear定义全连接层,conv4为2*2*64
        self.mlp2 = torch.nn.Linear(100, 10) 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用损失函数和优化器的步骤

  1. 获取损失:loss=loss_fuction(out,batch_y)
  2. 清空上一步残余更新参数:opt.zero_grad()
  3. 误差反向传播:loss.backward()
  4. 将参数更新值施加到net的parmeter上:opt.step()

模型相关参数配置(使用argparse.ArgumentParser)

简介

argparse是一个Python模块:命令行选项、参数和子命令解析器。

使用方法

  1. 创建解析器
parser = argparse.ArgumentParser(description='Process some integers.')
  • 1
  1. 添加参数
parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
  • 1

例如在神经网络训练过程中,我们需要定义训练的初始学习率,并设置默认值为0.00001,可通过以下代码实现:

parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=1e-5,
                        help='Learning rate', dest='lr')
  • 1
  • 2
  1. 解析参数
args = parser.parse_args()
  • 1

模型输出

torch.max(out,1)[1]
output = torch.max(input, dim)

  • input是softmax函数输出的一个tensor
  • dim是max函数索引的维度0/1,0是每列最大值,1是每行最大值

函数会返回两个tensor,第一个tensor是每行的最大值;第二个tensor是每行最大值的索引。
(PS:第一个tensor value是不需要的,我们仅提取第二个tensor并将数据转换为array格式
torch.max(out,1)[1].numpy()

网络参数查看

model_dict=torch.load('D:\JetBrains\pythonProject\CMRI\CNN\CNN_Log')
parameters=list(model_dict.named_parameters())
  • 1
  • 2

补充知识点

downsampling(向下采样) & upsampling

down-sampling通过舍弃一些元素,实现图像的缩放

在CNN中,汇合层(Pooling layer)通过max poolingaverage pooling等操作,使汇合后结果中一个元素对应于原输入数据的一个子区域,因此汇合操作实际上就是一种”降采样“操作

up-sampling 可实现图像的放大或分辨率的优化等

常用方法:

  • Bilinear(双线性插值法):只需要设置好固定的参数值即可,设置的参数就是中心值需要乘以的系数。
  • Deconvolution(反卷积):参考https://github.com/vdumoulin/conv_arithmetic
  • Unpooling(反池化):在反池化过程中,将一个元素根据kernel进行放大,根据之前的坐标将元素填写进去,其他位置补0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/691132
推荐阅读
相关标签
  

闽ICP备14008679号