torch.nn模块
本文就根据官网文档整理一下torch.nn模块
TORCH.NN非常重要,也是以后用pytorch搭建神经网络的关键模块。所以值得认真学习。
TORCH.NN
第一个关键类:
CLASS torch.nn.Parameter
A kind of Tensor that is to be considered a module parameter.
Parameters are
Tensor
subclasses, that have a very special property when used withModule
s - when they’re assigned as Module attributes they are automatically added to the list of its parameters, and will appear e.g. in parameters() iterator. Assigning a Tensor doesn’t have such effect. This is because one might want to cache some temporary state, like last hidden state of the RNN, in the model. If there was no such class as Parameter, these temporaries would get registered too.
简单来说,就是Tensor类的特殊化实例,利用torch.nn.Parameter(data,required_grad)定义的变量可以直接作为网络模型的parameter
需要的参数有:
- data (Tensor) – parameter tensor.
- requires_grad (bool, optional) – Default: True
第二个关键类
CLASS torch.nn.Module
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
很关键很重要的搭建神经网络的父类
举例:
import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))
那么该方法都包括哪些方法呢:
add_module(name, module)
Adds a child module to the current module. The module can be
accessed as an attribute using the given name. parameter:
- name (string) – name of the child module. The child module can be accessed from this module using the given name
- module (Module) – child module to be added to the module.
作用:添加子模型
—————————————————————————————
apply(fn)
‘’‘
Parameters:fn (Module -> None) – function to be applied to each submodule
Return:self
Return type:Module
’‘’
Applies
fn
recursively to every submodule (as returned by.children()
) as well as self. Typical use includes initializing the parameters of a model (see also torch-nn-init).
举例:
>>> def init_weights(m):
>>> print(m)
>>> if type(m) == nn.Linear:
>>> m.weight.data.fill_(1.0)
>>> print(m.weight)
>>> net = nn.Sequential(nn.Linear(2, 2), nn.Linear(2, 2))
>>> net.apply(init_weights)
相当于对net内的每一个module都进行一次fn(init_weights)处理“
结果:
Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[ 1., 1.],
[ 1., 1.]])
Linear(in_features=2, out_features=2, bias=True)
Parameter containing:
tensor([[ 1., 1.],
[ 1., 1.]])
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
Sequential(
(0): Linear(in_features=2, out_features=2, bias=True)
(1): Linear(in_features=2, out_features=2, bias=True)
)
—————————————————————————————
上一篇: php数组拆分、组合
下一篇: 知识图谱构建