MODULE
CLASStorch.nn.Module(args, kwargs*)[SOURCE]层、模型的父类
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:
func
add_module(name, module)[SOURCE](向当前的模块中添加子模块,层层嵌套通过
.
访问)apply(fn)[SOURCE](以函数作为参数,可以递归的应用到子模块中)
Apply
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).
Parameters
fn (
Module
-> None) – function to be applied to each submoduleReturns
self
Return type
Example:
bfloat16()[SOURCE]
- Casts all floating point parameters and buffers to
bfloat16
datatype.(将模块的所有浮点类型参数或者buffers全部转换为bfloat16
)buffers(recurse=True)[SOURCE]
- Return an iterator over module buffers.(返回该模块包含buffer的一个迭代器,buffers与parameter同级,parameter参与到梯度下降训练&前向运算,buffers统计量)
**children()[
](https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module.children)**
- Return an iterator over immediate children modules.
**cpu()[
](https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module.cpu)**
- Move all model parameters and buffers to the CPU.
cuda(device=None)[
](https://pytorch.org/docs/stable/_modules/torch/nn/modules/module.html#Module.cuda)
Move all model parameters and buffers to the GPU.
This also makes associated parameters and buffers different objects. So it should be called before constructing optimizer if the module will live on GPU while being optimized.
eval()[SOURCE]
Set the module in evaluation mode.
This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g.
Dropout
,BatchNorm
, etc.This is equivalent with
self.train(False)
.See Locally disabling gradient computation for a comparison between .eval() and several similar mechanisms that may be confused with it.
- get_parameter(target)[SOURCE](根据字符串,得到当前模型里的参数)
load_state_dict(state_dict, strict=True, assign=False)[SOURCE]
Copy parameters and buffers from
state_dict
into this module and its descendants.If
strict
isTrue
, then the keys ofstate_dict
must exactly match the keys returned by this module’sstate_dict()
function.requires_grad_(requires_grad=True)[SOURCE](对模型是否需要梯度更新进行设置)
MODULE SOURCE CODE
register_buffer
register_parameter
parameter
CLASStorch.nn.parameter.Parameter(data=None, requires_grad=True)[SOURCE](data传入的张量;*requires_grad参数是否需要梯度运算。模型内部如果添加参数,要写成Parameter类型而不是Tensor类型)
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. inparameters()
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 asParameter
, these temporaries would get registered too.Parameters
- data (Tensor) – parameter tensor.
- requires_grad (bool, optional) – if the parameter requires gradient. Note that the torch.no_grad() context does NOT affect the default behavior of Parameter creation–the Parameter will still have requires_grad=True in
no_grad
mode. See Locally disabling gradient computation for more details. Default: True
实例
python - Correct way to register a parameter for model in Pytorch - Stack Overflow
还不快抢沙发