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

问题描述 投票:0回答:1

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

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个回答
0
投票

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

new_torch_tensor = torch.tensor(numpy_array)
© www.soinside.com 2019 - 2024. All rights reserved.