Python 如何在pytorch神经网络中为层创建循环变量名

Python 如何在pytorch神经网络中为层创建循环变量名,python,neural-network,pytorch,torch,Python,Neural Network,Pytorch,Torch,我正在PyTorch中实现一个简单的前馈神经网络。然而,我想知道是否有更好的方法为网络添加灵活的层?也许在循环中给它们命名,但我听说那是不可能的 目前我是这样做的 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,

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

目前我是这样做的

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

您可以将图层放入容器中:

导入火炬
导入torch.nn作为nn
导入torch.nn.功能为F
类别网络(nn.模块):
定义初始(自身、输入尺寸、输出尺寸、隐藏尺寸):
超级(网络,自我)。\uuuu初始化
self.input_dim=输入_dim
self.output\u dim=output\u dim
self.hidden\u dim=隐藏的\u dim
当前尺寸=输入尺寸
self.layers=nn.ModuleList()
对于隐藏的hdim\u dim:
self.layers.append(nn.Linear(当前尺寸,hdim))
当前尺寸=hdim
self.layers.append(nn.Linear(当前尺寸,输出尺寸))
def前进(自身,x):
对于self.layers[:-1]中的层:
x=F.relu(层(x))
out=F.softmax(自层[-1](x))
返回

这对于层非常重要,而不仅仅是一个简单的python列表。请查看以了解原因。

为什么不使用列表?如果你开始在变量名中添加数字,你就做错了。