当Pytorch的mul()函数与numpy结合使用时,为什么会看到TypeError?

时间:2019-02-17 00:09:00

标签: pytorch reinforcement-learning

我在终端中遇到以下错误:

Traceback (most recent call last):
  File "deep_Q_learner.py", line 289, in <module>
    agent.replay_experience()
  File "deep_Q_learner.py", line 170, in replay_experience
    self.learn_from_batch_experience(experience_batch)
  File "deep_Q_learner.py", line 151, in learn_from_batch_experience
    self.Q_target(next_obs_batch).max(1)[0].data
TypeError: mul(): argument 'other' (position 1) must be Tensor, not numpy.ndarray

该代码的链接为: https://github.com/PacktPublishing/Hands-On-Intelligent-Agents-with-OpenAI-Gym/blob/master/ch6/deep_Q_learner.py

仅当self.DQN = SLP时才看到错误(请参阅第76行)

是否已解决此问题?我在这里想念东西吗?

1 个答案:

答案 0 :(得分:0)

如错误所示,您应该传递一个torch.tensor对象,其中传递一个numpy数组对象。通过以下方式将您的numpy数组转换为torch.tenosr:

new_torch_tensor = torch.tensor(numpy_array)