【问题标题】:Getting the error "AttributeError: 'NoneType' object has no attribute 'shape'" when implementing Atari Breakout实现 Atari Breakout 时出现错误“AttributeError: 'NoneType' object has no attribute 'shape'”
【发布时间】:2020-07-25 10:37:07
【问题描述】:

我编写了一个代码来解决 Atari Breakout。我面临一个小问题,但我不能说它是什么。

这里是code

这是回放内存的问题。

try:
    next_states = torch.tensor(batch[3], dtype=torch.float32) 
except:
    import ipdb; ipdb.set_trace()

问题出在这些行在哪里。 set_trace() 用于弹出交互式 shell。从那里,如果我运行for i in range(batch_size): print(batch[3][i].shape),我会得到这个输出

ipdb> for i in range(32): print(batch[3][i].shape)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
(4, 84, 84)
*** AttributeError: 'NoneType' object has no attribute 'shape'

如何改进该代码以避免此类错误?

【问题讨论】:

  • 这个问题更适合Stack Overflow(因为这只是一个编程问题)。我会将它迁移到 SO 并在那里提供正式的答案。

标签: python-3.x reinforcement-learning dqn


【解决方案1】:

错误告诉您问题所在。您正在尝试在 None 上调用 shape,因此,在您的代码中,某个变量 aNone,而您正在调用 shape,即 a.shape。这是编程中最常见的错误之一!

在您的 for 循环中

for i in range(32): 
    print(batch[3][i].shape)

在某些时候,batch[3][i]None,所以你必须弄清楚batch[3] 包含什么以及为什么它是None

在这里查看讨论https://chat.stackexchange.com/transcript/message/54070403#54070403

【讨论】:

  • @jgauth 我稍后会尝试查看您的代码,但很明显batch[3] 中的问题。你在某个时候用None 填充它。
  • @jgauth 我建议你开始打印迭代号i。也许batch[3] 包含少于 32 个数字。
  • @jgauth 为什么你认为问题出在ptan。如果稍后我有一点时间,我会尝试更仔细地查看您的代码,但现在我不能。也许尝试使用调试器。
  • @jgauth 我需要执行哪些确切的代码才能得到您的确切错误?
猜你喜欢
  • 1970-01-01
  • 2022-11-13
  • 2023-01-16
  • 2023-02-18
  • 1970-01-01
  • 2022-12-15
  • 1970-01-01
相关资源
最近更新 更多