我正在建立一个神经网络模型,并试图允许任意数量的隐藏层。隐藏层的大小在列表中给出。例如hidden_size = [50,20]表示存在三个分别为大小50和20的隐藏层。
我的代码粘贴在下面。
class NeuralNet(nn.Module):
"""
neural network class, with nn api
"""
def __init__(self, input_size: int, hidden_size: List[int], output_size: int):
"""
initialization function
:param input_size: input data dimension
:param hidden_size: list of hidden layer sizes, arbitrary length
:param output_size: output data dimension
"""
super().__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1)
"""layers"""
self.input = nn.Linear(self.input_size, self.hidden_size[0])
for h in range(len(hidden_size) - 1):
exec(f"self.hidden{h} = nn.Linear(self.hidden_size[h], self.hidden_size[h+1])")
self.output = nn.Linear(hidden_size[-1], output_size)
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
forward propagation process, required by the nn.Module class
:param x: the input data
:return: the output from neural network
"""
x = self.input(x)
x = self.relu(x)
#x = self.hidden0(x)
#x = self.relu(x)
for h in range(len(self.hidden_size) - 1):
exec(f"x = self.hidden{h}(x)")
x = self.relu(x)
x = self.output(x)
x = self.softmax(x)
return x
我给出了100的输入大小,[50,20]的hidden_size和2的output_size。我们应该具有大小为100 * 50的输入层,大小为50 * 20的hidden0和大小为20的输出* 2。如预期的那样,在名为 init 的函数中,使用exec参数定义了名为hidden0的层,而没有发生任何错误。但是,该模型似乎在前进步骤中跳过了exec参数。我对此有两个怀疑的理由。第一个原因是错误表明torch.addmm中的尺寸不匹配,其中两个矩阵的尺寸分别为200 * 50和20 * 2,其中200为批处理尺寸。另一个原因是,如果我使用注释的代码而不是for循环,那么一切都会正常运行。
我想知道为什么会发生这种情况,以及使用for循环允许任意数量的隐藏层的可行解决方案。