Search code examples
python-3.xreinforcement-learningdqn

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


I have made a code to solve Atari Breakout. I am facing a little problem, but I can't say what it is.

Here is the code

It is a problem with the replay memory.

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

The problem is located where are those lines. set_trace() is use to pop-up an interactive shell. From there, if I run for i in range(batch_size): print(batch[3][i].shape), I obtained this output

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'

How can I improve that code to avoid such error?


Solution

  • The error is telling you the problem. You're trying to call shape on None, so, in your code, some variable a is None and you are calling shape on it, i.e. a.shape. This is one of the most common errors in programming!

    In your for loop

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

    at some point, batch[3][i] is None, so you will have to figure out what batch[3] contains and why it is None.

    See the discussion here https://chat.stackexchange.com/transcript/message/54070403#54070403.