Neuronal Networks => small(ish) project

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Neuronal Networks => small(ish) project

Post by a_bertrand »

Hi Guys,

First of all, be ready to be blown away at first if you never touched the subject. BUT if you stick with me a bit, you may discover really some cool things which you may never actually directly experienced.

So let's start with trying to explain the why somebody would even come to something called "neuronal networks" and then we will check the "what" and "how" later.

The why is pretty simply put: when you do have a problem and you know you want some output, but don't know how you can code the logic between, then maybe a solution could be a neuronal network. Good examples are imagine recognition (find a bike inside a picture) or identify people by their pictures. We know what we have at start (an image composed by pixels) and we want to get as output "it is likely a bike". The problem is known, but how to solve that is somewhat complex or really complex if you start doings if / else statement like you traditionally do. A neuronal network on the other side is able to solve those problem for you, maybe not in the quickest way possible, and maybe not with 100% of success, but still give you a good output which otherwise you will not be able to reach. Today most "AI" we end up using like Google tons of services (Google now, Google image, Google translate and many more) do use one or more neuronal networks.

So what is a neuronal network? Well that's actually something SIMPLE as concept:
neuron.png
A network is just a set of "nodes" called "neurons" which are inter-connected. So all inputs goes to all the middle layer nodes (hidden layer), and all the hidden layer nodes goes to all the outputs.

When you put a 1 on the first input this one value will be used by all the nodes under and basically each nodes will sum up all the nodes of the previous layer and apply for each a "weight", so think that the hidden "layer 1" do "input 1" * "weight 1" + "input 2" * "weight 2"

At the end (on the output) the value is (usually) "normalize" between either -1 and +1 or 0 and 1 using some formula.

So far, this seems just a number of simple operations, and it is actually NOTHING else than that. You feed your system with some values, you apply some add and multiply, and get back some other values. Doesn't seems so smart, right? Well the smartness of the whole is on the "weight" you put on each of those node links. This is where the logic is and where it is applied and you already start to discover, that those weight must have something special to be able to solve the problem.

Where the smartness of the whole is, is that the system is able to tweak those weights over time, such to improve the results.

There is many online playgrounds, one which you may look at:
https://goo.gl/QSoKTF

So let's try to make our very one one in JS:
http://jsfiddle.net/fdjfhptb/

This is a complete (yet naive) neuronal network I wrote in Javascript (actually in Typescript which compiled to JS). The Init function is called and will create a network trying to solve a simple problem: 3 input values should lead either to a 0 or a 1. The rules are:
0, 0, 1 = 1
1, 1, 1 = 1
1, 0, 1 = 1
0, 1, 1 = 0

Those number could be replaced by anything, but I took that from some python neuronal network tutorial (yet my code has little to nothing to do with their example).

My training is an "evolution" algorithm which randomly change the weights of the network till it find a good enough solution. Good enough as it will NEVER be perfect.

So far you may wonder why I should even lose time on something which take some input like that and spit either 0 or 1, but let's go over and try something a bit more fun and more complex. What if the SAME network can actually play for you a game like the snake game? What if the network would be able to learn how to play and reach the eggs eat them avoid walls and itself and go on? Well guess what, this is what I did:

http://jsfiddle.net/L69q19po

You can see that at start the network has a "learning" phase where the error counter goes slowly down, then the "game" starts and a snake will chase the eggs (the red dots).

The code between line 460-507 is what creates the rules which will then be feed inside the network which basically tells going toward a wall is bad, going toward a egg is good.

The snake fires 13 rays from its head and check if something is seen, that value is then feed inside the input of the network, and 2 output says "go to left" or "go to right".

I have an even more complex version running here:
http://www.ludiculus.com/temp/snake.html
which uses a background to continuously update the snake logic while it runs.

For any question / discussion please post ;)
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Neuronal Networks => small(ish) project

Post by hallsofvallhalla »

:shock: :shock: :shock:
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Neuronal Networks => small(ish) project

Post by a_bertrand »

Yes? Hall?
Creator of Dot World Maker
Mad programmer and annoying composer
Post Reply

Return to “General Development”