pytorch的python API略读--SequentialModuleListModuleDict

pytorch的python API略读--SequentialModuleListModuleDict,第1张

作者:机器视觉全栈er
网站:cvtutorials.com

torch.nn.Sequential:序列容器,顾名思义,就是将构造函数中的子模块会按照你定义的序列顺序被添加到模块中。这里有个注意的小点,要注意区分Sequential和torch.nn.ModuleList,后者就是一个简单的列表,里面的元素是模块,但是模块之间是孤立的,前者则是互相连通的模块。还是以上面的网络模块为例,使用Sequential可以实现快速搭建:

import torch.nn as nn
import torch

class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer = nn.Sequential(
            nn.Conv2d(1, 3, 3),
            nn.ReLU(),
            nn.Conv2d(3, 3, 3),
            nn.ReLU()
            )

    def forward(self, x):
        x = self.layer(x)
        return x

my_module = MyModule()

cvtutorials = torch.randn(3, 1, 5, 5)
print("=====================================")
print(cvtutorials)
print("=====================================")
print(my_module(cvtutorials))

torch.nn.ModuleList():顾名思义,是多个Module组成的列表,还是以前面的网络模型举例:

import torch.nn as nn
import torch

class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.layers = nn.ModuleList([
            nn.Conv2d(1, 3, 3),
            nn.ReLU(),
            nn.Conv2d(3, 3, 3),
            nn.ReLU()])
    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x
my_module = MyModule()
cvtutorials = torch.randn(3, 1, 5, 5)
print("=====================================")
print(cvtutorials)
print("=====================================")
my_module(cvtutorials)

torch.nn.ModuleDict():顾名思义,是多个Module组成的字典,还是以前面的网络模型举例(注意这里前向推理的时候,只选择了conv1和activation1):

import torch.nn as nn
import torch

class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.convs = nn.ModuleDict({
            "conv1": nn.Conv2d(1, 3, 3),
            "conv2": nn.Conv2d(3, 3, 3)
        })
        self.activations = nn.ModuleDict({
            "activation1": nn.ReLU(),
            "activation2": nn.ReLU()
        })

    def forward(self, x, conv, activation):
        x = self.convs[conv](x)
        x = self.activations[activation](x)
        return x

my_module = MyModule()
cvtutorials = torch.randn(3, 1, 5, 5)
print("=====================================")
print(cvtutorials)
print("=====================================")
my_module(cvtutorials, "conv1", "activation1")

现在我们来总结下Sequential, ModuleList, ModuleDict三者常见的应用场景:

www.cvtutorials.comSequentialModuleListModuleDict
特点顺序性迭代性索引性
应用场景网络block搭建大量重复网络结构可选择网络模块

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/868513.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存