我定义了以下神经网络:
class Q_function(nn.Module):
def __init__(self, input_size=3, hidden_size=5, num_layers=3, learning_rate=0.01):
super(Q_function, self).__init__()
self.input_size = input_size
self.layers = []
for i in num_layers:
self.layers.append(nn.Linear(input_size, hidden_size))
self.append(nn.ReLU())
self.layers.append(nn.Linear(hidden_size,1))
def forward(self, x):
out = self.layers[0](x)
for lay in range(1,len(self.layers)):
out = self.layers[lay](out)
return out
当我跑步时:
net = Q_function()
list(net.parameters())
我得到的输出是一个空列表
[]
。有人可以解释为什么网络没有参数吗?如何注册参数?如何解决这个问题?
Pytorch 通过特定的构造函数类来跟踪参数。它无法查看任意列表。
要跟踪模块列表,您需要将其包装在 nn.Sequential 或 nn.ModuleList
中class Q_function(nn.Module):
def __init__(self, input_size=3, hidden_size=5, num_layers=3, learning_rate=0.01):
super(Q_function, self).__init__()
self.input_size = input_size
self.layers = []
for i in num_layers:
self.layers.append(nn.Linear(input_size, hidden_size))
self.append(nn.ReLU())
self.layers.append(nn.Linear(hidden_size,1))
self.layers = nn.ModuleList(self.layers)
def forward(self, x):
out = self.layers[0](x)
for lay in range(1,len(self.layers)):
out = self.layers[lay](out)
return out
也就是说,您的模型代码中也存在许多错误。您可能想要这样的东西:
class Q_function(nn.Module):
def __init__(self, input_size=3, hidden_size=5, num_layers=3, learning_rate=0.01):
super(Q_function, self).__init__()
self.input_size = input_size
self.layers = [nn.Linear(input_size, hidden_size), nn.ReLU()]
for i in range(num_layers-1):
self.layers.append(nn.Linear(hidden_size, hidden_size))
self.layers.append(nn.ReLU())
self.layers.append(nn.Linear(hidden_size,1))
self.layers = nn.Sequential(*self.layers)
def forward(self, x):
x = self.layers(x)
return x