pyTorch等效于SciPy.minmise

问题描述

解决最小似然问题以学习逆强化学习模型的奖励函数。我已经在NumPy中构建了该函数,并使用scipy.minimise设法通过将似然函数返回的梯度有效地最小化了似然函数

但是...

我现在已经将我的实现集成到pyTorch和scipy.minimise并不能很好地工作。它始终以ABnorMAL_TERMINATION_IN_LNSRCH错误终止。

我意识到这里有torch.optim库,我尝试使用torch.optim.lbfgs()方法,但是它似乎是为神经网络构建的,我不知道如何配置max / min可能性问题。

有人知道使用scipy.minimise配置pyTorch.optim来匹配这种可能性最小化吗?

#set likelihood variables
lh = Likelihood()
lh.set_variables_for_likehood(mdp_data,N,T,example_samples)

#minimise likelihood with analytically computed gradient
guess = torch.randn(mdp_params['n']**2,1)
res = minimize(lh.negated_likelihood,guess,jac=True,method="L-BFGS-B",options={'disp': True})

为了记录,我的似然函数是:

def negated_likelihood(self,r):

        if(torch.is_tensor(r) == False):
            r = torch.tensor(r)
            
        #Reshape R to expected
        if(r.shape != (4,5)):
            r = torch.reshape(r,(4,1))
            r = r.repeat((1,5))
            
        #Solve MDP with current reward
        v,q,logp,p = linearvalueiteration(self.mdp_data,r)

        #Calculate likelihood from logp
        likelihood = sum(sum(logP*self.mu_sa))
        likelihood = likelihood.item() #get scalar value

        #Compute state visitation count D
        D = linearmdpfrequency(self.mdp_data,p,self.initD)

        #Compute gradient.
        dr = self.muE - torch.matmul(torch.t(self.F),D)
        dr = -dr #Invert for descent.
        dr = dr.detach().cpu().numpy() #convert to array
        
        return -(likelihood),dr

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)