Python 将Pyrotch浮点模型转换为双精度
我想从健身房解cartpole。结果表明,状态是双浮点精度的,而pytorch默认情况下创建的模型是单浮点精度的Python 将Pyrotch浮点模型转换为双精度,python,pytorch,openai-gym,Python,Pytorch,Openai Gym,我想从健身房解cartpole。结果表明,状态是双浮点精度的,而pytorch默认情况下创建的模型是单浮点精度的 class QNetworkMLP(Module): def __init__(self,state_dim,num_actions): super(QNetworkMLP,self).__init__() self.l1 = Linear(state_dim,64) self.l2 = Linear(64,64)
class QNetworkMLP(Module):
def __init__(self,state_dim,num_actions):
super(QNetworkMLP,self).__init__()
self.l1 = Linear(state_dim,64)
self.l2 = Linear(64,64)
self.l3 = Linear(64,128)
self.l4 = Linear(128,num_actions)
self.relu = ReLU()
self.lrelu = LeakyReLU()
def forward(self,x) :
x = self.lrelu(self.l1(x))
x = self.lrelu(self.l2(x))
x = self.lrelu(self.l3(x))
x = self.l4(x)
return x
我试着通过
model = QNetworkMLP(4,2).double()
但它仍然不起作用,我得到了同样的错误
File ".\agent.py", line 117, in update_online_network
predicted_Qval = self.online_network(states_batch).gather(1,actions_batch)
File "C:\Users\27abh\anaconda3\envs\gym\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\27abh\Desktop\OpenAI Gym\Cartpole\agent_model.py", line 16, in forward
x = self.lrelu(self.l1(x))
File "C:\Users\27abh\anaconda3\envs\gym\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\27abh\anaconda3\envs\gym\lib\site-packages\torch\nn\modules\linear.py", line 91, in forward
return F.linear(input, self.weight, self.bias)
File "C:\Users\27abh\anaconda3\envs\gym\lib\site-packages\torch\nn\functional.py", line 1674, in linear
ret = torch.addmm(bias, input, weight.t())
RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'mat1' in call to _th_addmm
您可以在初始化模型后尝试以下操作:
model.to(torch.double)
不起作用,相同的错误您是否尝试过将输入的
x
转换为相应的数据类型,而不是模型?