8有免费建网站,网站设计视频,淘宝客网站一定要备案吗,wordpress cpu 100%三 模型创建与nn.Module
3.1 nn.Module 模型构建两要素#xff1a;
构建子模块——__init()__拼接子模块——forward#xff08;#xff09; 一个module可以有多个module#xff1b;
一个module相当于一个运算#xff0c;都必须实现forward函数#xff1b;
每一个mod…三 模型创建与nn.Module
3.1 nn.Module 模型构建两要素
构建子模块——__init()__拼接子模块——forward 一个module可以有多个module
一个module相当于一个运算都必须实现forward函数
每一个module有8个字典管理属性。
self._parameters OrderedDict()
self._buffers OrderedDict()
self._backward_hooks OrderedDict()
self._forward_hooks OrderedDict()
self._forward_pre_hooks OrderedDict()
self._state_dict_hooks OrderedDict()
self._load_state_dict_pre_hooks OrderedDict()
self._modules OrderedDict()
3.2 网络容器 nn.Sequential()
是nn.Module()的一个容器用于按照顺序包装一组网络层
顺序性网络层之间严格按照顺序构建
自带forward
各网络层之间严格按顺序执行常用于block构建
class LeNetSequential(nn.Module): def __init__(self, classes): super(LeNetSequential, self).__init__() self.features nn.Sequential( nn.Conv2d(3, 6, 5), nn.ReLU(), nn.MaxPool2d(kernel_size2, stride2), nn.Conv2d(6, 16, 5), nn.ReLU(), nn.MaxPool2d(kernel_size2, stride2),) self.classifier nn.Sequential( nn.Linear(16*5*5, 120), nn.ReLU(), nn.Linear(120, 84), nn.ReLU(), nn.Linear(84, classes),) def forward(self, x): x self.features(x) x x.view(x.size()[0], -1) x self.classifier(x) return x
nn.ModuleList()
是nn.Module的容器用于包装网络层以迭代方式调用网络层。
主要方法
append()在ModuleList后面添加网络层
extend:拼接两个ModuleList.
Insert():指定在ModuleList中插入网络层。
nn.ModuleList迭代性常用于大量重复网构建通过for循环实现重复构建
class ModuleList(nn.Module): def __init__(self): super(ModuleList, self).__init__() self.linears nn.ModuleList([nn.Linear(10, 10) for i in range(20)]) def forward(self, x): for i, linear in enumerate(self.linears): x linear(x) return x
nn.ModuleDict()
以索引方式调用网络层
主要方法
• clear()清空ModuleDict
• items()返回可迭代的键值对(key-value pairs)
• keys()返回字典的键(key)
• values()返回字典的值(value)
• pop()返回一对键值并从字典中删除
n.ModuleDict索引性常用于可选择的网络层
class ModuleDict(nn.Module): def __init__(self): super(ModuleDict, self).__init__() self.choices nn.ModuleDict({ conv: nn.Conv2d(10, 10, 3), pool: nn.MaxPool2d(3) }) self.activations nn.ModuleDict({ relu: nn.ReLU(), prelu: nn.PReLU() }) def forward(self, x, choice, act): x self.choices[choice](x) x self.activations[act](x) return x
3.3卷积层
nn.ConV2d()
nn.Conv2d(in_channels, out_channels,kernel_size, stride1,padding0, dilation1, groups1, biasTrue, padding_modezeros)
in_channels输入通道数比如RGB图像是3而后续的网络层的输入通道数为前一卷积层的输出通道数
out_channels输出通道数等价于卷积核个数
kernel_size卷积核尺寸
stride步
padding填充个数
dilation空洞卷积大小
groups分组卷积设置
bias偏置 conv_layer nn.Conv2d(3, 1, 3) # input:(i, o, size) weights:(o, i , h, w) nn.init.xavier_normal_(conv_layer.weight.data) # calculation img_conv conv_layer(img_tensor)
这里使用 input*channel 为 3output_channel 为 1 卷积核大小为 3×3 的卷积核nn.Conv2d(3, 1, 3)使用nn.init.xavier_normal*()方法初始化网络的权值。
我们通过conv_layer.weight.shape查看卷积核的 shape 是(1, 3, 3, 3)对应是(output_channel, input_channel, kernel_size, kernel_size)。所以第一个维度对应的是卷积核的个数每个卷积核都是(3,3,3)。虽然每个卷积核都是 3 维的执行的却是 2 维卷积。
转置卷积nn.ConvTranspose2d
转置卷积又称为反卷积(Deconvolution)和部分跨越卷积(Fractionally-stridedConvolution) 用于对图像进行上采样(UpSample)
为什么称为转置卷积
假设图像尺寸为4*4卷积核为3*3padding0stride1
正常卷积 转置卷积
假设图像尺寸为2*2卷积核为3*3padding0stride1 nn.ConvTranspose2d(in_channels, out_channels,
kernel_size,
stride1,
padding0,
output_padding0,
groups1,
biasTrue,
dilation1, padding_modezeros)
输出尺寸计算 # flag 1
flag 0
if flag: conv_layer nn.ConvTranspose2d(3, 1, 3, stride2) # input:(i, o, size) nn.init.xavier_normal_(conv_layer.weight.data) # calculation img_conv conv_layer(img_tensor)
print(卷积前尺寸:{}\n卷积后尺寸:{}.format(img_tensor.shape, img_conv.shape))
img_conv transform_invert(img_conv[0, 0:1, ...], img_transform)
img_raw transform_invert(img_tensor.squeeze(), img_transform)
plt.subplot(122).imshow(img_conv, cmapgray)
plt.subplot(121).imshow(img_raw)
plt.show() 3.4池化层nn.MaxPool2d nn.AvgPool2d
池化运算对信号进行 “收集”并 “总结”类似水池收集水资源因而
得名池化层
“收集”多变少
“总结”最大值/平均值
nn.MaxPool2d
nn.MaxPool2d(kernel_size, strideNone,
padding0, dilation1,
return_indicesFalse,
ceil_modeFalse)
主要参数
• kernel_size池化核尺寸
• stride步长
• padding 填充个数
• dilation池化核间隔大小
• ceil_mode尺寸向上取整
• return_indices记录池化像素索引
# flag 1
flag 0
if flag: maxpool_layer nn.MaxPool2d((2, 2), stride(2, 2)) # input:(i, o, size) weights:(o, i , h, w) img_pool maxpool_layer(img_tensor)
nn.AvgPool2d
nn.AvgPool2d(kernel_size,
strideNone,
padding0,
ceil_modeFalse,
count_include_padTrue,
divisor_overrideNone)
主要参数
• kernel_size池化核尺寸
• stride步长
• padding 填充个数
• ceil_mode尺寸向上取整
• count_include_pad填充值用于计算
• divisor_override 除法因子 avgpoollayer nn.AvgPool2d((2, 2), stride(2, 2)) # input:(i, o, size) weights:(o, i , h, w) img_pool avgpoollayer(img_tensor) img_tensor torch.ones((1, 1, 4, 4)) avgpool_layer nn.AvgPool2d((2, 2), stride(2, 2), divisor_override3) img_pool avgpool_layer(img_tensor) print(raw_img:\n{}\npooling_img:\n{}.format(img_tensor, img_pool)) nn.MaxUnpool2d
功能对二维信号图像进行最大值池化
上采样
主要参数
• kernel_size池化核尺寸
• stride步长
• padding 填充个数 # pooling img_tensor torch.randint(high5, size(1, 1, 4, 4), dtypetorch.float) maxpool_layer nn.MaxPool2d((2, 2), stride(2, 2), return_indicesTrue) img_pool, indices maxpool_layer(img_tensor) # unpooling img_reconstruct torch.randn_like(img_pool, dtypetorch.float) maxunpool_layer nn.MaxUnpool2d((2, 2), stride(2, 2)) img_unpool maxunpool_layer(img_reconstruct, indices) print(raw_img:\n{}\nimg_pool:\n{}.format(img_tensor, img_pool)) print(img_reconstruct:\n{}\nimg_unpool:\n{}.format(img_reconstruct, img_unpool)) 3.5线性层
nn.Linear(in_features, out_features, biasTrue)
功能对一维信号向量进行线性组合
主要参数
• in_features输入结点数
• out_features输出结点数
• bias 是否需要偏置
计算公式y s inputs torch.tensor([[1., 2, 3]]) linear_layer nn.Linear(3, 4) linear_layer.weight.data torch.tensor([[1., 1., 1.], [2., 2., 2.], [3., 3., 3.], [4., 4., 4.]]) linear_layer.bias.data.fill_(0.5) output linear_layer(inputs) print(inputs, inputs.shape) print(linear_layer.weight.data, linear_layer.weight.data.shape) print(output, output.shape) 3.6 激活函数层
nn.Sigmoid
nn.tanh:
nn.ReLU
nn.LeakyReLU
negative_slope: 负半轴斜率
nn.PReLU
init: 可学习斜率
nn.RReLU
lower: 均匀分布下限
upper:均匀分布上限 参考资料
深度之眼课程