【问题标题】:Why do I see TypeError when Pytorch's mul() function is used in combination with numpy?为什么当 Pytorch mul() 函数与 numpy 结合使用时,我会看到 TypeError?
【发布时间】:2019-07-10 18:04:04
【问题描述】:

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

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

这个问题有解决办法吗?我在这里遗漏了什么吗?

【问题讨论】:

    标签: pytorch reinforcement-learning


    【解决方案1】:

    正如错误提示的那样,您应该传入一个 torch.tensor 对象,在该对象中传入一个 numpy 数组对象。通过以下方式将您的 numpy 数组转换为 torch.tenosr:

    new_torch_tensor = torch.tensor(numpy_array)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 2021-04-25
      • 1970-01-01
      • 2019-10-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      相关资源
      最近更新 更多