[英]Error: optimizer got an empty parameter list. How can i modify my code?
这是代码:
class MLP(nn.Module):
def __ini__(self):
super (MLP, self). __init__()
self.model=nn.nn.Sequential(
nn.Linear(784, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 10),
nn.LeakyReLU(inplace=True),
)
def forward(self,x):
x=self.model(x)
return
device= torch.device('cuda:0')
net = MLP().to(device)
运行这些代码时
optimizer = optim.SGD(net.parameters(),lr=learning_rate)
我明白了
ValueError:优化器得到一个空的参数列表”
我试图模仿这个笔记本。
您的代码有几个问题。 目前您的代码相当于:
class MLP(nn.Module):
def forward(self, x):
pass
以下是问题:
您的初始化程序名为__init__
。
forward
应该返回一个值,这里是x
。
将nn.nn.Sequential
替换为nn.Sequential
。
class MLP(nn.Module):
def __init__(self):
super (MLP, self).__init__()
self.model = nn.Sequential(
nn.Linear(784, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 200),
nn.LeakyReLU(inplace=True),
nn.Linear(200, 10),
nn.LeakyReLU(inplace=True))
def forward(self,x):
x = self.model(x)
return x
device= torch.device('cuda:0')
net = MLP().to(device)
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.