我正在 YouTube 上关注此指南 Python + PyTorch + Pygame 强化学习 – 训练 AI 玩贪吃蛇 并收到以下错误。
我已将代码上传到 GitHub:这里
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
您必须将以下代码摘录中的
state_new = agent.get_state
更改为 state_new = agent.get_state()
(链接到 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()
如果没有括号,你给出的是方法而不是方法的结果。