Search code examples
pythontensorflowpytorchtypeerror

TypeError: must be real number, not method


I am following this guide on YouTube Python + PyTorch + Pygame Reinforcement Learning – Train an AI to Play Snake and getting the error below.

I have uploaded the code to GitHub : Here

pygame 2.5.2 (SDL 2.28.3, Python 3.11.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "/Users/dhruvyadav/Desktop/Projects/AI-Snake-Game/agent.py", line 142, in <module>
    train()
  File "/Users/dhruvyadav/Desktop/Projects/AI-Snake-Game/agent.py", line 120, in train
    agent.train_short_memory(state_old, final_move, reward, state_new, done)
  File "/Users/dhruvyadav/Desktop/Projects/AI-Snake-Game/agent.py", line 84, in train_short_memory
    self.trainer.train_step(state, action, reward, next_state, done)
  File "/Users/dhruvyadav/Desktop/Projects/AI-Snake-Game/model.py", line 34, in train_step
    next_state = torch.tensor(next_state, dtype=torch.float)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: must be real number, not method

Solution

  • You have to change state_new = agent.get_state to state_new = agent.get_state() in the following code extract (link to github).

        while True:
            # Get old state
            state_old = agent.get_state(game)
            
            # Get move based on current state
            final_move = agent.get_action(state_old)
    
            # Perform the move and get the new state
            reward, done, score = game.play_step(final_move)
            state_new = agent.get_state()
    

    Without the parenthesis, you give the method instead of the result of the method.