# Snake Neural Net

**URL:** <https://discourse.threejs.org/t/snake-neural-net/68657>\
**Category:** Showcase\
**Created:** [July 27, 2024, 9:36am UTC](https://discourse.threejs.org/t/snake-neural-net/68657 "2024-07-27T09:36:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kritb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kritb/32/37234_2.png) [@kritb](https://discourse.threejs.org/u/kritb)\
**Post date:** [July 27, 2024, 9:36am UTC](https://discourse.threejs.org/t/snake-neural-net/68657/1 "2024-07-27T09:36:55Z")

</div>

I used threejs to train this model.  
[https://satellitevisualization.pythonanywhere.com/snakeneuralnet](https://satellitevisualization.pythonanywhere.com/snakeneuralnet)

What I like

- I use raycasters to help alert the neural net. With the AI snake games I have seen, you can hardcode it’s environment. I used raycasters instead since I think it could extend past the grided 2D example. Maybe one day I’ll try to make more complicated models that travel 3D.
- The neural net visual. I think threejs could easily plug into large python neural net models and make cool visuals.

Thanks!

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/f/b/fb367e88b7cfe91c1d3530f4e05657e55cd2c316.jpeg)

---

<div class="post-metadata">

**Author:** ![manthrax](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/manthrax/32/2596_2.png) [@manthrax](https://discourse.threejs.org/u/manthrax)\
**Post date:** [July 28, 2024, 11:25pm UTC](https://discourse.threejs.org/t/snake-neural-net/68657/2 "2024-07-28T23:25:13Z")

</div>

This is suuper cool. How did you train the network in python? Any resources? Love this stuff…

---

<div class="post-metadata">

**Author:** ![byDezign](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/bydezign/32/24605_2.png) [@byDezign](https://discourse.threejs.org/u/byDezign)\
**Post date:** [July 29, 2024, 11:44am UTC](https://discourse.threejs.org/t/snake-neural-net/68657/3 "2024-07-29T11:44:01Z")

</div>

[Premiers pas avec TensorFlow.js](https://www.tensorflow.org/js/tutorials) is pretty solid. It’s what I used to make my image Denoiser and has tons of examples.

If you have pretrained models already ONNX is also an option: [Web | onnxruntime](https://onnxruntime.ai/docs/tutorials/web/)

Eventually we’ll be able to do this in the browser with WebNN but it’s even farther back than WebGPU

---

<div class="post-metadata">

**Author:** ![kritb](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/kritb/32/37234_2.png) [@kritb](https://discourse.threejs.org/u/kritb)\
**Post date:** [July 29, 2024, 1:34pm UTC](https://discourse.threejs.org/t/snake-neural-net/68657/4 "2024-07-29T13:34:13Z")

</div>

thanks!

I have two versions. A local site where I I trained it using a python backend connected to threejs via websocket.  
I used this [repo](https://github.com/patrickloeber/snake-ai-pytorch) for a basis of the python model. There is also a very good youtube video where he breaks it down.

Then this second (published) site where I transferred over the numpy matrices as JS arrays and perform the dot products and activation functions inside the javascript loop.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/d/3/d3f110bb00a70501dad28f24a1dad9659b80f63c.png)
