【发布时间】:2021-06-07 15:15:58
【问题描述】:
我正在尝试解决 Gym 的购物车问题。事实证明,这些状态是双浮点精度的,而 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)
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
【问题讨论】:
-
您是否尝试将 input
x转换为适当的 dtype,而不是模型? -
谢谢它的工作。结果在转换为 Torch 张量后,我不知道我的输入本身被转换为 float 而不是将其数据类型保持为 double
标签: python pytorch openai-gym