【持续更新中…】
数据预处理
Variable
from torch.autograd import Variable
作用:自动微分变量,用于构建计算图
网络层定义
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)
- 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)
- 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)
使用损失函数和优化器的步骤
- 获取损失:loss=loss_fuction(out,batch_y)
- 清空上一步残余更新参数:opt.zero_grad()
- 误差反向传播:loss.backward()
- 将参数更新值施加到net的parmeter上:opt.step()
模型相关参数配置(使用argparse.ArgumentParser)
简介
argparse是一个Python模块:命令行选项、参数和子命令解析器。
使用方法
- 创建解析器
parser = argparse.ArgumentParser(description='Process some integers.')
- 添加参数
parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
例如在神经网络训练过程中,我们需要定义训练的初始学习率,并设置默认值为0.00001,可通过以下代码实现:
parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=1e-5,
help='Learning rate', dest='lr')
- 解析参数
args = parser.parse_args()
模型输出
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())
补充知识点
downsampling(向下采样) & upsampling
down-sampling通过舍弃一些元素,实现图像的缩放
在CNN中,汇合层(Pooling layer)通过max pooling 或average pooling等操作,使汇合后结果中一个元素对应于原输入数据的一个子区域,因此汇合操作实际上就是一种”降采样“操作
up-sampling 可实现图像的放大或分辨率的优化等
常用方法:
- Bilinear(双线性插值法):只需要设置好固定的参数值即可,设置的参数就是中心值需要乘以的系数。
- Deconvolution(反卷积):参考https://github.com/vdumoulin/conv_arithmetic
- Unpooling(反池化):在反池化过程中,将一个元素根据kernel进行放大,根据之前的坐标将元素填写进去,其他位置补0