以下是PyTorch官方文档的链接地址:
参考链接: class torch.nn.Module
以下是根据官方文档上的说明,对该Module类内的成员方法和成员属性总结整理的资料:
成员方法导航链接: torch.nn.Module.add_module(name, module)
成员方法导航链接: torch.nn.Module.apply(fn)
成员方法导航链接: torch.nn.Module.buffers(recurse=True)
成员方法导航链接: torch.nn.Module.children()
成员方法导航链接: torch.nn.Module.cpu()
成员方法导航链接: torch.nn.Module.cuda(device=None)
成员方法导航链接: torch.nn.Module.double()
成员属性导航链接: torch.nn.Module.dump_patches
成员方法导航链接: torch.nn.Module.eval()
成员方法导航链接: torch.nn.Module.extra_repr()
成员方法导航链接: torch.nn.Module.float()
成员方法导航链接: torch.nn.Module.forward(*input)
成员方法导航链接: torch.nn.Module.half()
成员方法导航链接: torch.nn.Module.load_state_dict(state_dict, strict=True)
成员方法导航链接: torch.nn.Module.modules()
成员方法导航链接: torch.nn.Module.named_buffers(prefix=‘‘, recurse=True)
成员方法导航链接: torch.nn.Module.named_children()
成员方法导航链接: torch.nn.Module.named_modules(memo=None, prefix=‘‘)
成员方法导航链接: torch.nn.Module.named_parameters(prefix=‘‘, recurse=True)
成员方法导航链接: torch.nn.Module.parameters(recurse=True)
成员方法导航链接: torch.nn.Module.parameters()和torch.nn.Module.state_dict()的使用举例
成员方法导航链接: register_backward_hook() register_forward_hook() register_forward_pre_hook()
成员方法导航链接: torch.nn.Module.register_backward_hook(hook)
成员方法导航链接: torch.nn.Module.register_buffer(name, tensor)
成员方法导航链接: torch.nn.Module.register_forward_hook(hook)
成员方法导航链接: torch.nn.Module.register_forward_pre_hook(hook)
成员方法导航链接: torch.nn.Module.register_parameter(name, param)方法使用简介
成员方法导航链接: torch.nn.Module.requires_grad_(requires_grad=True)
成员方法导航链接: torch.nn.Module.state_dict(destination=None, prefix=’’, keep_vars=False)
成员方法导航链接: torch.nn.Module.to(*args, **kwargs)使用举例
成员方法导航链接: torch.nn.Module.train(mode=True)
成员方法导航链接: torch.nn.Module.type(dst_type)
成员方法导航链接: torch.nn.Module.zero_grad()的使用
链接: PyTorch神经网络模型从CPU或GPU中加载和保存
链接: PyTorch训练模型时,给优化器传入多余参数,这些多余的参数并不参与前向传播,因而也无法在反向传播中训练更新
链接: 讨论PyTorch中模型加载时参数不一致的情况
链接: PyTorch模型的保存与加载简单总结
原文以及翻译:
Containers 容器
Module 模块
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))
Submodules assigned in this way will be registered, and will
have their parameters converted too when you call to(), etc.
以这种方式赋值给属性的子模块将会被自动地登记注册,而且当你调用to()方法
的时候,这些子模块的参数也会被正确地相应转换.
torch.nn.Module是所有神经网络模块地基类,代码展示:
(ssd4pytorch1_2_0) PS C:\Users\chenxuqi\Desktop\News4cxq\test4cxq> python
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.nn.Module
<class 'torch.nn.modules.module.Module'>
>>> torch.nn.Sequential
<class 'torch.nn.modules.container.Sequential'>
>>> torch.nn.Conv2d
<class 'torch.nn.modules.conv.Conv2d'>
>>> torch.nn.MaxPool2d
<class 'torch.nn.modules.pooling.MaxPool2d'>
>>> torch.nn.ReLU
<class 'torch.nn.modules.activation.ReLU'>
>>> torch.nn.BatchNorm2d
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>
>>> torch.nn.Linear
<class 'torch.nn.modules.linear.Linear'>
>>> torch.nn.Dropout
<class 'torch.nn.modules.dropout.Dropout'>
>>> type(torch.nn.Module)
<class 'type'>
>>> type(torch.nn.Sequential)
<class 'type'>
>>> type(int)
<class 'type'>
>>> issubclass(torch.nn.Module,torch.nn.Module)
True
>>> issubclass(torch.nn.Sequential,torch.nn.Module)
True
>>> int
<class 'int'>
>>> issubclass(torch.nn.Conv2d,torch.nn.Module)
True
>>> issubclass(torch.nn.MaxPool2d,torch.nn.Module)
True
>>> issubclass(torch.nn.ReLU,torch.nn.Module)
True
>>> issubclass(torch.nn.BatchNorm2d,torch.nn.Module)
True
>>> issubclass(torch.nn.Linear,torch.nn.Module)
True
>>> issubclass(torch.nn.Dropout,torch.nn.Module)
True
>>> issubclass(torch.nn.Dropout,torch.nn.Module)
True
>>> issubclass(torch.nn.Module,torch.nn.Dropout)
False
>>>
>>>