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

340,681
0
Published 2022-04-25
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: github.com/python-engineer/snake-ai-pytorch

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

🎨 Art by Rachel: rachel.likespizza.com/

⭐️ 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: www.freecodecamp.org/

Read hundreds of articles on programming: freecodecamp.org/news

All Comments (21)
  • @edwintjoa6099
    Thanks for the awesome video! Really fun to see agent improving during the games.
  • @HRDmonk
    Thank you so much for this tutorial. I have always wanted to introduce snake with ML. and now looking forward to learning more about pytorch
  • Super great video! I run it and after more than 2000 games it seems that the average score reached a plateau of 30. It Avoids boundaries very well. But it hit itself when the tail is too long. But this movement should be predicted before.
  • @khalidelgazzar
    Watched the first 4 mins and the game and the learning process is fantastic! 🎉 Will go on it with the rest hopefully soon.
  • @adrian46647
    Awesome, so hard to find that type of explanation of dqn. All clear, great balance between theory and coding part for beginners in rl.
  • Always very high quality videos when I just need it :) Thanks a lot! Looking forward to finish the video  😎
  • @Hiyori___
    Very interesting tutorial. I was familiar with Snake code implementations, Tensorflow basics and RL principles and I could follow along. For some reason the plot doesn't appear, and I only get a small blank window where the plot should be. Probably some bug while typing the code down. Other than that everything works for me. Thank you very much!
  • A nice video. My only critique is that the presenter kept writing code for almost an hour without running it. That's not giving a good example.
  • @sergiomollo
    Thanks for this video, I have solved all my doubts
  • @lukasgamedev
    Hello! Is there a way to save the state of the neural model? So I can load later a trained enemy AI, ready for being the player opponent? Thank you for the video!
  • @Fr4nk4000
    Following this since I wanna make a pygame project of mine that I poured a lot of time into and have no idea what to make the game about play itself. Wish me luck.
  • @vikramganesan
    how to perform UCB, optimal initial values and dynamic programming approaches in this model?
  • @pactube8833
    Thanks for freeCodeCamp for making this possible.