You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PyTorch中为自动构建的神经网络添加Dropout层

解决PyTorch FCN自动添加Dropout层的问题

你的问题出在构建ModuleList的语法和逻辑上——你之前的写法只是在整个Linear层列表末尾硬加了一个Dropout,没有实现每个隐藏层后自动添加的效果。下面是正确的修改方案:

修改后的完整代码

import torch
import torch.nn as nn
import numpy as np

class FCN(nn.Module):
    ##Neural Network
    def __init__(self, layers, dropout_p=0.5):
        super().__init__() #call __init__ from parent class 
        self.activation = nn.Tanh()
        self.loss_function = nn.MSELoss(reduction='mean')
        self.iter = 0
        
        # 初始化包含Linear和Dropout的层列表
        self.layers = nn.ModuleList()
        for i in range(len(layers)-1):
            # 添加Linear层
            self.layers.append(nn.Linear(layers[i], layers[i+1]))
            # 除了最后一个Linear层,后面都加Dropout
            if i < len(layers)-2:
                self.layers.append(nn.Dropout(p=dropout_p))
        
        # Xavier Normal Initialization - 只初始化Linear层的参数
        for layer in self.layers:
            if isinstance(layer, nn.Linear):
                nn.init.xavier_normal_(layer.weight.data, gain=1.0)            
                nn.init.zeros_(layer.bias.data)   
    
    # forward pass
    def forward(self, x):
        if not torch.is_tensor(x):         
            x = torch.from_numpy(x)                
        a = x.float()
        # 遍历所有层,依次前向传播
        for layer in self.layers:
            a = layer(a)
            # 注意:Dropout层不需要手动加激活,激活只在Linear之后(除了最后一层)
            if isinstance(layer, nn.Linear) and layer != self.layers[-1]:
                a = self.activation(a)
        return a

实例化代码(和之前一致)

layers = np.array([2, 50, 50, 1])
model = FCN(layers)

关键修改点说明

  1. 层结构构建逻辑:

    • 不再用单一的列表推导式,而是循环创建每个Linear层,并且在非最后一个Linear层后添加Dropout,确保每个隐藏层后都有Dropout
    • 把原来的self.linears改名为self.layers,更准确地表示它包含所有类型的层
  2. 参数初始化:

    • 遍历层列表时,只对nn.Linear类型的层做初始化,跳过Dropout层(Dropout没有可训练参数)
  3. 前向传播逻辑:

    • 遍历所有层依次计算,遇到Linear层(且不是最后一层)时,再添加激活函数,这样能正确适配Linear+Dropout+激活的顺序
  4. 额外优化:

    • 把Dropout的概率作为参数传入__init__,方便灵活调整

注意事项

  • 使用Dropout时,训练阶段要调用model.train(),评估阶段调用model.eval()——PyTorch会自动在eval模式下关闭Dropout的随机失活效果
  • 如果你希望激活函数在Dropout之后(而不是之前),只需要调整forward里的顺序:先过Linear,再激活,再Dropout,对应修改forward的逻辑即可

内容的提问来源于stack exchange,提问作者Link_tester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:11:05