美文网首页
PyTorch神经网络层拆解

PyTorch神经网络层拆解

作者: LabVIEW_Python | 来源:发表于2021-11-07 14:56 被阅读0次

本文将拆解常见的PyTorch神经网络层,从开发者的角度来看,这些神经网络层都是一个一个的函数,完成对数据的处理。

第一:CLASS torch.nn.Flatten(start_dim=1, end_dim=- 1) ,将多维的输入一维化,常用在从卷积层到全连接层的过渡。需要注意的是,Flatten()的默认值start_dim=1,即默认数据数据的格式是[N,C,H,W]第0维度为Batch Size,不参与Flatten。后面的CHW全部展平为一维。

Flatten()常用于卷积层与全连接层的过度
范例程序:
import torch
import torch.nn as nn 

flatten = nn.Flatten()
input = torch.rand(64, 1, 28, 28)
output = flatten(input)
print(f"output's Shape:{output.size()}")

output's Shape:torch.Size([64, 784])

第二CLASS torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None) ,Linear又叫全连接层,TensorFlow里面叫Dense,主要用于分类。
Linear类有两个属性:

  • Linear.weight (torch.Tensor) – 可训练的权重
  • Linear.bias – 可训练的偏置
    范例程序:
import torch
import torch.nn as nn 

flatten = nn.Flatten()
input = torch.rand(64, 1, 28, 28)
flatten_output = flatten(input)
print(f"flatten_output's Shape:{flatten_output.size()}")

fc = nn.Linear(in_features=28*28, out_features=10)
fc_output = fc(flatten_output)
print(f"fc_output's Shape:{fc_output.size()}")
print(f"fc's weight:{fc.weight};fc's bias:{fc.bias}")

flatten_output's Shape:torch.Size([64, 784])
fc_output's Shape:torch.Size([64, 10])
fc's weight:Parameter containing:
tensor([[ 0.0325, -0.0351, 0.0015, ..., -0.0062, -0.0098, 0.0303],
[ 0.0227, -0.0080, 0.0173, ..., 0.0158, -0.0036, 0.0128],
[ 0.0179, 0.0329, -0.0287, ..., -0.0002, -0.0090, 0.0187],
...,
[-0.0068, -0.0257, 0.0051, ..., 0.0014, 0.0097, -0.0065],
[ 0.0120, 0.0093, 0.0217, ..., -0.0130, -0.0346, 0.0356],
[-0.0169, 0.0087, -0.0047, ..., -0.0003, 0.0157, -0.0295]],
requires_grad=True);fc's bias:Parameter containing:
tensor([ 0.0340, -0.0079, -0.0193, -0.0159, 0.0028, 0.0116, -0.0062, -0.0316,
-0.0305, 0.0039], requires_grad=True)

第三,CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None),卷积层,常用于提取图像特征,CNN+RELU+MaxPooling已经成为一种常见的特征提取操作了。
需要注意的是:CNN要求数据输入格式为:[N, Cin, Hin, Wout],Cin是输入数据Tensor的通道数量,输出为[N, Cout, Hout, Wout],Cout为本CNN层的卷积个数。Hout和Wout计算公式如下所示:

CNN层要求的数据输入与输出格式

范例程序:

import torch
import torch.nn as nn 

input = torch.rand(64, 3, 28, 28)
cnn1  = nn.Conv2d(3,5,3)               # 28->26
maxpooling = nn.MaxPool2d(2,2)         # 26->13
relu = nn.ReLU()
cnn2 = nn.Conv2d(5,10,3)               # 13->11

print(f"input'shape:{input.shape}")
output_cnn1 = cnn1(input)
print(f"output_cnn1'shape:{output_cnn1.shape}")
output_relu1 = relu(output_cnn1)
print(f"output_relu1'shape:{output_relu1.shape}")
output_maxpooling1 = maxpooling(output_relu1)
print(f"output_maxpooling1'shape:{output_maxpooling1.shape}")
output_cnn2 = cnn2(output_maxpooling1)
print(f"output_cnn2'shape:{output_cnn2.shape}")

input'shape:torch.Size([64, 3, 28, 28])
output_cnn1'shape:torch.Size([64, 5, 26, 26])
output_relu1'shape:torch.Size([64, 5, 26, 26])
output_maxpooling1'shape:torch.Size([64, 5, 13, 13])
output_cnn2'shape:torch.Size([64, 10, 11, 11])

总结:

  • PyTorch的每个神经网络计算层都是CLASS torch.nn.Module的子类,都具有forward()方法实现前向计算,使用to()方法把该层的可训练参数指定到CPU或GPU上

    torch.nn.Module基类
  • 认真阅读API手册可以更加深入的理解PyTorch的神经网络层的实现

相关文章

网友评论

      本文标题:PyTorch神经网络层拆解

      本文链接:https://www.haomeiwen.com/subject/cvxezltx.html