python - 如何在循环中为 pytorch 神经网络中的层创建变量名称

标签 python neural-network pytorch torch

我正在 PyTorch 中实现一个简单的前馈神经 newtork。但是我想知道是否有更好的方法来向网络添加灵活的层数?也许通过在循环中命名它们,但我听说那是不可能的?

目前我正在这样做

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

class Net(nn.Module):

    def __init__(self, input_dim, output_dim, hidden_dim):
        super(Net, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.hidden_dim = hidden_dim
        self.layer_dim = len(hidden_dim)
        self.fc1 = nn.Linear(self.input_dim, self.hidden_dim[0])
        i = 1
        if self.layer_dim > i:
            self.fc2 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc3 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc4 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc5 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc6 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc7 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        if self.layer_dim > i:
            self.fc8 = nn.Linear(self.hidden_dim[i-1], self.hidden_dim[i])
            i += 1
        self.fcn = nn.Linear(self.hidden_dim[-1], self.output_dim)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.relu(self.fc1(x))
        i = 1
        if self.layer_dim > i:
            x = F.relu(self.fc2(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc3(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc4(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc5(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc6(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc7(x))
            i += 1
        if self.layer_dim > i:
            x = F.relu(self.fc8(x))
            i += 1
        x = F.softmax(self.fcn(x))
        return x

最佳答案

您可以将图层放在 ModuleList 容器中:

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

class Net(nn.Module):

    def __init__(self, input_dim, output_dim, hidden_dim):
        super(Net, self).__init__()
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.hidden_dim = hidden_dim
        current_dim = input_dim
        self.layers = nn.ModuleList()
        for hdim in hidden_dim:
            self.layers.append(nn.Linear(current_dim, hdim))
            current_dim = hdim
        self.layers.append(nn.Linear(current_dim, output_dim))

    def forward(self, x):
        for layer in self.layers[:-1]:
            x = F.relu(layer(x))
        out = F.softmax(self.layers[-1](x))
        return out    

对层使用 pytorch Containers 非常重要,而不仅仅是简单的 Python 列表。请参阅 this answer 以了解原因。

关于python - 如何在循环中为 pytorch 神经网络中的层创建变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097924/

相关文章:

python - 运行时错误 : one of the variables needed for gradient computation has been modified by an inplace operation?

python - 获取 Qt 中的输出 : 'QProcess::start()' and 'QProcess:readAllStandardOutPut()'

java - 用 java 编写这个 python 结构

matlab - 在matlab中批量读取wav文件为神经网络训练集创建矩阵

python - 训练卷积神经网络时准确率突然下降 50%

java - 性能 Encog 与 Deeplearning4J

python - 正确的数据加载器设置可训练 fastrcnn-resnet50 以使用 pytorch 进行对象检测

python - 如何重构 cnn 层的输出张量以供简单的 pytorch 模型中的线性层使用

python - 以交互式 ssh 模式在进程内调用多个命令

python - 字符串的按位运算Python3.7