如何在循环中为pytorch神经网络中的层创建变量名
Posted
技术标签:
【中文标题】如何在循环中为pytorch神经网络中的层创建变量名【英文标题】:How to create variable names in loop for layers in pytorch neural network 【发布时间】:2020-01-25 14:50:20 【问题描述】:我正在 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
【问题讨论】:
为什么不使用列表?如果您开始在变量名中添加数字,您就做错了。 【参考方案1】:您可以将您的图层放入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 了解原因。
【讨论】:
以上是关于如何在循环中为pytorch神经网络中的层创建变量名的主要内容,如果未能解决你的问题,请参考以下文章
如何在 django 模板中为循环创建唯一的 javascript 变量名?