赞
踩
原始定义方式与 nn.Sequential 两种定义方式实例:
可以看到使用torch.nn.Sequential()搭建神经网络模型非常的方便,少写很多的code
- import torch
- import torch.nn as nn
-
-
- # -------------------------方式一:传统网络定义方式--------------------------------
- class Net(nn.Module):
- def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
- super(Net, self).__init__()
-
- self.linear1 = nn.Linear(in_dim, n_hidden_1)
- self.Relu1 = nn.ReLU(True)
- self.linear2 = nn.Linear(n_hidden_1, n_hidden_2)
- self.Relu2 = nn.ReLU(True)
- self.linear3 = nn.Linear(n_hidden_2, out_dim)
-
- def forward(self, x):
-
- x = self.linear1(x)
- x = self.Relu1(x)
- x = self.linear2(x)
- x = self.Relu2(x)
- x = self.linear3(x)
-
- return x
-
- # -------------------------方式二:使用nn.Sequential定义网络------------------------
- class Net(nn.Module):
- def __init__(self, in_dim, n_hidden_1, n_hidden_2, out_dim):
- super(Net, self).__init__()
-
- self.layer = nn.Sequential(
- nn.Linear(in_dim, n_hidden_1), # (18,15)
- nn.ReLU(True),
- nn.Linear(n_hidden_1, n_hidden_2), # (15,10)
- nn.ReLU(True),
- nn.Linear(n_hidden_2, out_dim) # (10,1)
- )
-
- def forward(self, x):
- x = self.layer(x)
- return x
-
-
-
- # instantiation
- net = Net(18, 15, 10, 1)
-
- # create random input to model
- input = torch.randn(30, 18)
-
- # output the predicted value
- predict = net(input)
-
- print(predict.size())
- print(net)
torch.nn.Sequential
是一个Sequential
容器,模块将按照构造函数中传递的顺序添加到模块中。通俗的话说,就是根据自己的需求,把不同的函数组合成一个(小的)模块使用或者把组合的模块添加到自己的网络中。
- import torch.nn as nn
-
- model = nn.Sequential()
-
- model.add_module("conv1", nn.Conv2d(1, 20, 5))
- model.add_module('relu1', nn.ReLU())
- model.add_module('conv2', nn.Conv2d(20, 64, 5))
- model.add_module('relu2', nn.ReLU())
-
- # 输出
- Sequential(
- (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
- (relu1): ReLU()
- (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
- (relu2): ReLU()
- )
注意!!!nn.module也有add_module()对象
- # 被添加的module可以通过 name 属性来获取。
-
- import torch.nn as nn
- class Model(nn.Module):
- def __init__(self):
- super(Model, self).__init__()
- self.add_module("conv", nn.Conv2d(10, 20, 4))
- # self.conv = nn.Conv2d(10, 20, 4) 和上面这个增加module的方式等价
- model = Model()
- print(model.conv) # 通过name属性访问添加的子模块
- print(model)
-
- # 输出:注意子模块的命名方式
- Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
- Model(
- (conv): Conv2d(10, 20, kernel_size=(4, 4), stride=(1, 1))
- )
- import torch.nn as nn
-
- model = nn.Sequential(
- nn.Conv2d(1,20,5),
- nn.ReLU(),
- nn.Conv2d(20,64,5),
- nn.ReLU()
- )
- print(model)
-
- # 输出:注意命名方式
- Sequential(
- (0): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
- (1): ReLU()
- (2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
- (3): ReLU()
- )
-
- import collections
- import torch.nn as nn
-
- model = nn.Sequential(collections.OrderedDict([('conv1', nn.Conv2d(1, 20, 5)), ('relu1', nn.ReLU()),
- ('conv2', nn.Conv2d(20, 64, 5)),
- ('relu2', nn.ReLU())
- ]))
- print(model)
-
- # 输出:注意子模块命名方式
- Sequential(
- (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))
- (relu1): ReLU()
- (conv2): Conv2d(20, 64, kernel_size=(5, 5), stride=(1, 1))
- (relu2): ReLU()
- )
Pytorch系列1: torch.nn.Sequential()讲解_xddwz的博客-CSDN博客_torch.nn.sequential
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。