torch.nn.Module所有方法总结及其使用举例

以下是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
>>>
>>> 
  • 11
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值