如何解决ValueError:优化器在pytorch中得到一个空的参数列表
我想在PyTorch中实现卡尔曼滤波器。我建立了以下模型:
protected void Application_AuthenticateRequest(Object sender,EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if(authCookie != null)
{
//Extract the forms authentication cookie
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
// If caching roles in userData field then extract
string[] roles = authTicket.UserData.Split(new char[]{'|'});
// Create the IIdentity instance
IIdentity id = new FormsIdentity( authTicket );
// Create the IPrinciple instance
IPrincipal principal = new GenericPrincipal(id,roles);
// Set the context user
Context.User = principal;
}
}
它给了我import torch
from torch import matmul,mm,nn
class DepthV1Acceleration(torch.nn.Module):
def __init__(self,prediction_steps):
super(DepthV1Acceleration,self).__init__()
t = 1 / 30
self.initial_P = torch.tensor([[0,0],[0,1000,1000]])
self.P = self.initial_P.clone()
self.F = torch.tensor([[1,t,0.5 * t ** 2],1,t],0.9]])
self.depth_acceleration = torch.tensor([0.9],requires_grad=True)
self.F[2,2] = self.depth_acceleration
self.H = torch.tensor([[1,0]])
self.R = torch.tensor([[0.01]],requires_grad=True)
self.prediction_steps = prediction_steps
def forward(self,measurements):
output = torch.zeros(measurements[0],measurements[1] - self.prediction_steps,measurements[2])
for i in range(measurements.shape[0]):
self.P = self.initial_P.clone()
x = measurements[i,:]
for j in range(measurements.shape[1],-1 * self.prediction_steps):
z = measurements[i,j,:]
y = z - mm(self.H,x)
S = mm(mm(self.H,self.P),torch.transpose(self.H,1)) + self.R
K = mm(mm(self.P,1)) @ torch.inverse(S))
x = x + mm(K,y)
self.P = mm((torch.eye(10) - mm(K,self.H)),self.P)
# prediction
output_element = x.clone()
x = mm(self.F,x)
self.P = mm(mm(self.F,torch.transpose(self.F,1))
for _ in range(self.prediction_steps):
output_element = mm(self.F,output_element)
output[i,:] = output_element
return output
。我使用以下代码:
ValueError: optimizer got an empty parameter list
我检查了其他具有相同错误的问题,但没有发现类似我的情况的
。解决方法
您应该设置一个字段来容纳pytorch图层(继承nn.Module
的图层),以便parameters()
找到一组参数。张量不足;对于线性变换,您可以仅使用Linear
层。或者,您可以覆盖parameters()或named_parameters()。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。