使用nn编写脱落层.Sequential()方法+Pytorch



我正试图使用nn为我的神经网络创建一个Dropout Layer。Sequential((如下:

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:优化器得到一个空的参数列表

首先,我认为这是一个小的拼写错误:声明model = nn.Sequential(...),然后使用modelDp.parameters()。我想你只是犯了一个小的复制粘贴错误,这些实际上是一样的。

产生此错误是因为模型中没有任何层具有可训练的参数,即将受梯度反向传播步骤影响的参数。事实上;网络";什么都学不到。

为了消除错误并获得一个实际工作的神经网络,你需要包括学习层,根据你之前报告的错误,这些学习层是线性层。这将类似于:

model = nn.Sequential(nn.Linear(784, 10), Flatten(), DropoutLayer(0.7), nn.LogSoftMax(dim=-1))

现在再补充几句话:

  • 您可能需要使用pytorch随机张量而不是Numpy的。当您最终想要在GPU上移动网络时,处理device会更容易。

  • 一旦您在eval模式下尝试,此代码就会产生另一个错误,因为forward方法中的第二个条件分支没有返回任何内容。您可能需要将指令替换为return input * self.p

相关内容

最新更新