pytorch 中的重要模块化接口nn.Module的使用
作者:今夜无风 时间:2023-10-28 10:10:06
torch.nn 是专门为神经网络设计的模块化接口,nn构建于autgrad之上,可以用来定义和运行神经网络
nn.Module 是nn中重要的类,包含网络各层的定义,以及forward方法
查看源码
初始化部分:
def __init__(self):
self._backend = thnn_backend
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()
self.training = True
属性解释:
_parameters:字典,保存用户直接设置的 Parameter
_modules:子 module,即子类构造函数中的内容
_buffers:缓存
_backward_hooks与_forward_hooks:钩子技术,用来提取中间变量
training:判断值来决定前向传播策略
方法定义:
def forward(self, *input):
raise NotImplementedError
没有实际内容,用于被子类的 forward() 方法覆盖
且 forward 方法在 __call__ 方法中被调用:
def __call__(self, *input, **kwargs):
for hook in self._forward_pre_hooks.values():
hook(self, input)
if torch._C._get_tracing_state():
result = self._slow_forward(*input, **kwargs)
else:
result = self.forward(*input, **kwargs)
...
...
对于自己定义的网络,需要注意以下几点:
1)需要继承nn.Module类,并实现forward方法,只要在nn.Module的子类中定义forward方法,backward函数就会被自动实现(利用autograd机制)
2)一般把网络中可学习参数的层放在构造函数中__init__(),没有可学习参数的层如Relu层可以放在构造函数中,也可以不放在构造函数中(在forward函数中使用nn.Functional)
3)在forward中可以使用任何Variable支持的函数,在整个pytorch构建的图中,是Variable在流动,也可以使用for,print,log等
4)基于nn.Module构建的模型中,只支持mini-batch的Variable的输入方式,如,N*C*H*W
代码示例:
class LeNet(nn.Module):
def __init__(self):
# nn.Module的子类函数必须在构造函数中执行父类的构造函数
super(LeNet, self).__init__() # 等价与nn.Module.__init__()
# nn.Conv2d返回的是一个Conv2d class的一个对象,该类中包含forward函数的实现
# 当调用self.conv1(input)的时候,就会调用该类的forward函数
self.conv1 = nn.Conv2d(1, 6, (5, 5)) # output (N, C_{out}, H_{out}, W_{out})`
self.conv2 = nn.Conv2d(6, 16, (5, 5))
self.fc1 = nn.Linear(256, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# F.max_pool2d的返回值是一个Variable, input:(10,1,28,28) ouput:(10, 6, 12, 12)
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# input:(10, 6, 12, 12) output:(10,6,4,4)
x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
# 固定样本个数,将其他维度的数据平铺,无论你是几通道,最终都会变成参数, output:(10, 256)
x = x.view(x.size()[0], -1)
# 全连接
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
# 返回值也是一个Variable对象
return x
def output_name_and_params(net):
for name, parameters in net.named_parameters():
print('name: {}, param: {}'.format(name, parameters))
if __name__ == '__main__':
net = LeNet()
print('net: {}'.format(net))
params = net.parameters() # generator object
print('params: {}'.format(params))
output_name_and_params(net)
input_image = torch.FloatTensor(10, 1, 28, 28)
# 和tensorflow不一样,pytorch中模型的输入是一个Variable,而且是Variable在图中流动,不是Tensor。
# 这可以从forward中每一步的执行结果可以看出
input_image = Variable(input_image)
output = net(input_image)
print('output: {}'.format(output))
print('output.size: {}'.format(output.size()))
来源:https://www.cnblogs.com/demo-deng/p/10621904.html
标签:pytorch,nn.Module
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue axios请求拦截实例代码
2024-05-13 09:14:17
mysql 5.7.14 下载安装配置方法图文教程
2024-01-21 00:12:58
![](https://img.aspxhome.com/file/2023/0/106590_0s.jpg)
pandas返回缺失值位置的方法实例教程
2022-11-13 01:51:31
![](https://img.aspxhome.com/file/2023/7/69607_0s.png)
超好玩的"隔空操物"通过Python MediaPipe库实现
2023-06-04 23:21:09
![](https://img.aspxhome.com/file/2023/7/114747_0s.gif)
自定义Django Form中choicefield下拉菜单选取数据库内容实例
2024-01-25 09:02:02
利用Pytorch实现获取特征图的方法详解
2023-09-11 16:16:02
Python列表排序方法reverse、sort、sorted详解
2022-01-06 22:16:39
在ASP.NET 2.0中操作数据之十一:基于数据的自定义格式化
2023-07-14 19:53:21
![](https://img.aspxhome.com/file/2023/7/75547_0s.png)
thinkphp学习笔记之多表查询
2023-11-15 02:57:15
Python try-except-else-finally的具体使用
2022-10-18 14:31:45
![](https://img.aspxhome.com/file/2023/3/115643_0s.png)
Python+Pygame实现怀旧游戏飞机大战
2023-09-27 03:36:52
Python数据可视化之环形图
2022-03-26 04:24:12
![](https://img.aspxhome.com/file/2023/5/76905_0s.png)
MySQL中由load data语句引起死锁的解决案例
2024-01-19 19:37:14
![](https://img.aspxhome.com/file/2023/6/121586_0s.png)
Vue的生命周期一起来看看
2024-05-02 17:03:21
![](https://img.aspxhome.com/file/2023/4/130074_0s.jpg)
Python利用matplotlib做图中图及次坐标轴的实例
2022-01-06 18:14:56
![](https://img.aspxhome.com/file/2023/8/104898_0s.jpg)
让设计散发文化韵味
2009-03-22 15:01:00
![](https://img.aspxhome.com/file/UploadPic/20093/22/e69687e58c96e6a8a1e59e8b-85s.jpg)
几个常用的js小函数
2007-09-19 12:59:00
解决python 使用openpyxl读写大文件的坑
2021-06-20 17:03:24
![](https://img.aspxhome.com/file/2023/2/95822_0s.jpg)
python 字典访问的三种方法小结
2022-03-25 06:07:26
解决运行出现'dict' object has no attribute 'has_key'问题
2021-06-14 06:58:48