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()

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...