【发布时间】:2021-01-12 06:09:06
【问题描述】:
我正在尝试使用 nn.Sequential() 为我的神经网络创建一个 Dropout 层,如下所示:
class DropoutLayer(nn.Module):
def __init__(self, p):
super().__init__()
self.p = p
def forward(self, input):
if self.training:
u1 = (np.random.rand(*input.shape)<self.p)
u1 *= input
return u1
else:
input *= self.p
model = nn.Sequential(Flatten(),DropoutLayer(p = 0.7),nn.LogSoftmax(dim = -1))
opt = torch.optim.Adam(modelDp.parameters(), lr=0.005)
train(modelDp, opt, 5)
但我收到此错误: ValueError: 优化器得到一个空的参数列表
【问题讨论】:
-
此代码中没有任何内容会产生此错误。 784x10 的形状让我觉得你正在使用线性层的 MNIST,我相信这个层失败了。您是否使用 128 的批量大小并使用
view出错?除此之外,代码与此代码非常相似:stackoverflow.com/questions/64032525/…。请问这个代码是从哪里来的? -
这是深度学习的功课。是的,我正在使用 128 的批量大小并使用 Flatten()
class Flatten(nn.Module): def forward(self, x): return x.view(x.size()[0], -1)
标签: deep-learning neural-network pytorch