Live data from Hacker News

Show HN: Watch a neural net learn to play Snake

ppo.gradexp.xyz

21–30 of 56 posts

Re: Show HN: Watch a neural net learn to play Snake

#21
post #3

More details and implementation notes please?

It's on the page, if you click the little info icon in the upper-right. Here's the text but there's some nice graphics there too:

  Snake Game, training entirely in the browser. Built on tinygrad: the rollout / targets / train graphs are TinyJits authored in Python, then compiled once to WGSL and replayed here under WebGPU.

  Observation: flat 10×10 board (100) + 4-dim prev-action one-hot = 104 dims. fc_pi.weight is zero-init so the opening policy is uniform over the legal actions; fc_v uses tinygrad's default Kaiming init.

  Per rollout: T=24 × N=384 parallel snakes (9,216 transitions), then K=3 epochs × 4 mini-batches of PPO updates. GAE γ=0.99, λ=0.95; AdamW wd=0.01; ratio clip ε=0.1; grad-norm 0.5; Huber value β=1, val_coef=1; entropy bonus 0.008333333333333333.

  Action mask + value clip + KL early stop. The 4-dim prev_a obs tail lets fc_pi zero the U-turn logit (the env silently overrides same-axis reversals anyway). Value loss is max(huber(v_new−td), huber(v_clip−td)) at ε=0.2. Approx-KL is sampled after each epoch and breaks the loop at 1.5·kl_target.

Re: Show HN: Watch a neural net learn to play Snake

#24
Poorly programmed, it doesn't learn from its mistakes, the games get stuck in a loop because the snake doesn't capture a piece but the piece remains and there's a gap, constantly moving the snake along the same path with negative scores in an infinite loop leaving an unaltered yin and yang ;) there's a repetitive pattern in these infinite games between the position of the gap and the piece

Re: Show HN: Watch a neural net learn to play Snake

#25
post #23

I noticed snake gets penalized for not getting to the apple early, is that what you really want? Snake is about how long it gets not about the balance between length and wall clock time

But if not the snake could go into an infinite loop, never growing, never eating.

Re: Show HN: Watch a neural net learn to play Snake

#27
post #24

Poorly programmed, it doesn't learn from its mistakes, the games get stuck in a loop because the snake doesn't capture a piece but the piece remains and there's a gap, constantly moving the snake along the same path with negative scores in an infinite loop leaving an unaltered yin and yang ;) there's a repetitive pattern in these infinite games between the position of the gap and the piece

Did you let it train? This doesn’t happen for me

Re: Show HN: Watch a neural net learn to play Snake

#28
post #24

Poorly programmed, it doesn't learn from its mistakes, the games get stuck in a loop because the snake doesn't capture a piece but the piece remains and there's a gap, constantly moving the snake along the same path with negative scores in an infinite loop leaving an unaltered yin and yang ;) there's a repetitive pattern in these infinite games between the position of the gap and the piece

Did you let it train? This doesn’t happen for me

Yes, thousands of games, you can see how it happens in the displayed game matrix, there comes a point when they all enter those loops https://ibb.co/bM4RPzPb

Re: Show HN: Watch a neural net learn to play Snake

#29
post #28

Earlier quoted context omitted.

Did you let it train? This doesn’t happen for me

Yes, thousands of games, you can see how it happens in the displayed game matrix, there comes a point when they all enter those loops https://ibb.co/bM4RPzPb

Makes sense, author mentioned training collapses eventually

Re: Show HN: Watch a neural net learn to play Snake

#30
post #19
post #8

Really cool! But right as it was nearing 4,000, it seems to have corrupted itself and no longer got any scores above 0. Not sure if that's a code bug or a neural net issue. avg500 -4.6 last 500 episodes peak 3959.3 best window roll/s 20.68 20-step avg progress 4388 562749 episodes

Yes it just collapses eventually — never stabilizes. The training process is flawed, I suspect it has to do with the fact that some weights blow up over time, you can see in “weights” tab. But at around 4K avg score you should see it solve the env almost every time. Just a demo :) optimized for speed over stability. Reward structure: Step: -1 Dot: +100 Win: +1000 so ~4k is max theoretical score on 6x6.

maybe because it doesn't understand "done"? perfect play is impossible, random variance will cause scores to drop even if the model plays well and "wins". feels like it would get stuck in a loop trying to improve what can't be improved.
Post reply on HN