PyTorch - torch.nn.Sequential

PyTorch - torch.nn.Sequential

flyfish

官网的示例

# Example of using Sequential
model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )

# Example of using Sequential with OrderedDict
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

运行起来

示例代码1

import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        
        self.model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )



output=Net()
print(output) 

# =============================================================================
# Net(
#   (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()
#   )
# )
# =============================================================================

示例代码2

# 每一层都有名字
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        
        self.model = nn.Sequential(OrderedDict([
                  ('conv1', nn.Conv2d(1,20,5)),
                  ('relu1', nn.ReLU()),
                  ('conv2', nn.Conv2d(20,64,5)),
                  ('relu2', nn.ReLU())
                ]))



output=Net()
print(output) 


# =============================================================================
# Net(
#   (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()
#   )
# )
# =============================================================================

示例代码3

import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import OrderedDict

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.model=nn.Sequential()
        self.model.add_module('conv1', nn.Conv2d(1,20,5))
        self.model.add_module('relu1', nn.ReLU())
        self.model.add_module('conv2', nn.Conv2d(20,64,5))
        self.model.add_module('relu2', nn.ReLU())


output=Net()
print(output) 


# =============================================================================
# Net(
#   (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 `torch.nn` 高级用法 #### 自定义层实现 除了内置的标准神经网络组件外,PyTorch允许开发者创建自定义层来满足特定需求。通过继承`nn.Module`类并重写其中的方法可以轻松构建新的功能模块。 ```python import torch from torch import nn class CustomLayer(nn.Module): def __init__(self, input_features, output_features): super(CustomLayer, self).__init__() self.linear = nn.Linear(input_features, output_features) def forward(self, x): return torch.relu(self.linear(x)) ``` 此代码片段展示了如何定义一个新的线性变换加ReLU激活函数组合而成的简单定制化层[^1]。 #### 动态计算图支持 得益于PyTorch动态计算图机制,在训练过程中可以根据输入数据调整模型结构而无需重新编译整个程序。这使得实验更加灵活高效。 对于复杂的条件逻辑处理场景尤为有用: ```python def dynamic_forward(x): if sum(x).item() >= 0: branch_a = nn.Sequential( nn.Conv2d(3, 64, kernel_size=7), nn.ReLU(), nn.MaxPool2d(kernel_size=2) ) out = branch_a(x) else: branch_b = nn.Sequential( nn.Conv2d(3, 128, kernel_size=5), nn.ReLU(), nn.AvgPool2d(kernel_size=2) ) out = branch_b(x) return out ``` 上述例子中根据输入特征总和决定采用不同卷积分支路径。 #### 参数共享技巧 有时希望某些部分权重在整个网络内被多个地方共同使用,这时可以通过直接赋值方式实现在不同位置间共享参数。 下面的例子说明了两个全连接层之间共享相同的权值矩阵W: ```python shared_linear = nn.Linear(in_features=100, out_features=50) model_with_shared_params = nn.Sequential( shared_linear, nn.ReLU(), shared_linear, # Reuse the same layer instance here. nn.Sigmoid() ) ``` 这种做法有助于减少内存占用以及加速收敛过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二分掌柜的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值