Getting the error "AttributeError: 'NoneType' object has no attribute 'shape'" when implementing Atari Breakout

Getting the error "AttributeError: 'NoneType' object has no attribute 'shape'" when implementing Atari Breakout

我已经编写了解决 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'

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

错误告诉你问题所在。您正在尝试在 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