Python + PyTorch + Pygame Reinforcement Learning – Train an AI to Play Snake - playproduction.de

Python + PyTorch + Pygame Reinforcement Learning – Train an AI to Play Snake

freeCodeCamp.org
Views: 110005
Like: 3292
In this Python Reinforcement Learning course you will learn how to teach an AI to play Snake! We build everything from scratch using Pygame and PyTorch.

💻 Code:

✏️ Course developed by Python Engineer. Check out his YouTube channel:

🎨 Art by Rachel:

⭐️ Course Contents ⭐️
⌨️ (0:00:00) Part 1: Basics of Reinforcement Learning and Deep Q Learning
⌨️ (0:17:22) Part 2: Setup environment and implement snake game
⌨️ (0:40:07) Part 3: Implement agent to control game
⌨️ (1:10:59) Part 4: Create and train neural network

🎉 Thanks to our Champion and Sponsor supporters:
👾 Raymond Odero
👾 Agustín Kussrow
👾 aldo ferretti
👾 Otis Morgan
👾 DeezMaster

Learn to code for free and get a developer job:

Read hundreds of articles on programming:

55 Comments

  1. Thanks for freeCodeCamp for making this possible.

  2. __________________________________________________ says:

    omg patrick wow

  3. Go Elon, what you are doing is crucial to the world's best interest.

  4. I am loving these Python courses about AI. Can you guys create a Lua crash course?

  5. I'm a JavaScript Developer that knows nothing about python, but I must say I'm jelous 😭😭. This is really cool

  6. My snake became self aware, any ideas to stop it from taking over the world?

  7. Thanks to freeCodeCamp for introducing us to another great python content creator.

  8. I’ve always wanted to do this, thanks alot for the tutorial

  9. Could you please teach us unreal engine 5 blueprint beginners tutorial

  10. Sweet jesus… I haqd to go to the end, because I can't deal with this poor guy's voice. Then I see that if this is the best AI can do – we're going to keep our jobs for a long time.

  11. Can we speed up the game soo fast so ai can play faster and learn faster. Basically it can finish 100 games in a minute or two.

  12. This is so good! I have been playing this snake game everywhere, also on Discord error page, on computer app games, many more! Now you can even program bots to play the snake game

  13. There will be limited jobs by 2050 therefore start restoring nature also this is not about the availability of jobs, whatever you do; be responsible about nature, mainly water;  otherwise i fear nobody could help even if he/she has trillions, stop wandering here & there, coz the happiness which we are experiencing now is just a fluke ,no more natural resources are left to use, that is why we can see the rise of inflation nowadays therefore scientists are busy trying green energy as far as possible. Please understand, be responsible before it's too late, perhaps there is still time before we realize the reality. If you observe carefully it's happening here near you beside you, just think who's resources you are enjoying now and how long it will last ?? Now, Humans will ask what we should do? Well we are the only beings that can bring change compared to other animals and we need them(other lifes) too. Ask yourself? before it's too late, whether rich or poor it will affect both don't worry.🙏

  14. Can we create AI bot to play dice game?

  15. Always very high quality videos when I just need it 🙂 Thanks a lot! Looking forward to finish the video  😎

  16. Could you bring us a crash course of blender (3d modelling program)?

  17. A strange game.
    The only winning move is not to play.

  18. Nice effort and seems like the convergence is achieved upto some extent!

  19. What's your VSCode theme? It looks gooood

  20. Hi I have a question. I followed along and wrote everything. When I run it in the terminal it works but the plot doesn't. It gives me an empty minimized white screen. If someone else experienced this please help.

  21. where is he typing the commands? is this a CMD?

  22. On: agent.train_long_memory in the commentet forloop is a mistake (nexrt) 🤣 but otherwise its a nice video you helped me a lot

  23. What are the prerequisites of the video ?!

  24. why not just give the ai full board information as the state

  25. I created my own algorithm using visual coding and I created basic AI very cool course I will be working on it to further my learning my game algorithm is for raptor survival crisis 2 on dreams creation engine. Made the entire game and technology using visual coding and wiring code with wires and blocks

  26. There is a small bug where the snake will eat itself if you go in the opposite direction that its going in. To prevent this from happening change this line of code:

    if event.type == pygame.KEYDOWN:

    if event.key == pygame.K_LEFT :

    self.direction = Direction.LEFT

    elif event.key == pygame.K_RIGHT :

    self.direction = Direction.RIGHT

    elif event.key == pygame.K_UP:

    self.direction = Direction.UP

    elif event.key == pygame.K_DOWN :

    self.direction = Direction.DOWN

    to:

    if event.type == pygame.KEYDOWN:

    # 2nd conditional to prevent snake from eating itself!

    if event.key == pygame.K_LEFT and self.direction != Direction.RIGHT:

    self.direction = Direction.LEFT

    elif event.key == pygame.K_RIGHT and self.direction != Direction.LEFT:

    self.direction = Direction.RIGHT

    elif event.key == pygame.K_UP and self.direction != Direction.DOWN:

    self.direction = Direction.UP

    elif event.key == pygame.K_DOWN and self.direction != Direction.UP:

    self.direction = Direction.DOWN

  27. So far so good… but how about load the old 'brain' if u exit the game and want resume later ? Oo iam bite confused tryd with state
    if os.path.exists('model/model.pth'):
    self.model.load_state_dict(torch.load('model/model.pth'))
    self.model.eval()
    in the Agent init. but that dosent work

  28. Hi I am using Visual Studio and every time I try to move the plot window the program crashes and says "Could not convert to integer: 3221226505. Path 'exitCode'.Value was either too large or too small for an Int32"

  29. I copied it code for code and it doesn’t work. It gives me no errors but just runs and then ends with no result🤦🏽‍♂️🤦🏽‍♂️🤦🏽‍♂️🤦🏽‍♂️🤦🏽‍♂️

  30. how to perform UCB, optimal initial values and dynamic programming approaches in this model?

  31. I got this error: ValueError: not enough values to unpack (expected 3, got 2) for line 118 in agent.py

  32. very great course! thanks! A little question is , in class QTrainer maybe target = pred.clone().detach() ?

  33. What to install before creating an environment ,iam confused

Leave a Reply

Your email address will not be published.