## Neural networks in ActionScript 3

"An **artificial neural network (ANN)**, usually called "neural network" (NN), is a mathematical model or computational model that tries to simulate the structure and/or functional aspects of biological neural networks. It consists of an interconnected group of artificial neurons and processes information using a connectionist approach to computation. In most cases an ANN is an adaptive system that changes its structure based on external or internal information that flows through the network during the learning phase. Neural networks are non-linear statistical data modeling tools. They can be used to model complex relationships between inputs and outputs or to find patterns in data." (Wikipedia)

So, repetition is the mother of all learning they say. You damn right it is. And you can do it in AS3 of course. Not the fastest choice out there but that's not the point. NN's are usually not that fast but they're useful in so many ways.

So, here is my implementation of a neural network multi-layer-perceptron made in AS3, set to learn a simple XOR problem. It uses 2 inputs neurons , 2 hidden layers, each having 2 neurons and one output neuron. It takes about 2 seconds to train it using 10.000 epochs, but then you can save a snapshot of the NN memory as a byteArray, save it to the server and load it back again in an instant without requiring a new training. I didn't take the time to thoroughly document the classes just yet but I'm sure you'll find them pretty easy to use.

Some reading material:

http://en.wikipedia.org/wiki/Artificial_neural_network

http://fbim.fh-regensburg.de/~saj39122/jfroehl/diplom/e-index.html (this is great)

http://www.ai-junkie.com/ann/evolved/nnt1.html

Knight.zhouOctober 28th, 2010 - 05:39

Great post! and I have do the same research on AS3 AI programming, did you have any interesting we discuss it?

Knight.zhou recently posted..Bitboards

oliver merxDecember 17th, 2010 - 18:33

excellent code-example. We are working an a NN-approach in flex too to recognise patterns of rectangles.

I will keep you and knight.zhou updated.

with best regards from germany

oliver merx

ntJune 8th, 2011 - 21:16

Very nice work!

I have a problem though. Using this configuration

var inputNeurons : int = 4;

var outputNeurons : int = 3;

var hiddenNeuronsPerLayer : int = 3;

var hiddenLayers : int = 2;

var neuronalBias : Number = 1;

var initialWeightRange : Number = 1;

patterns.push(new TrainingPattern(“0.40591048,0.387517028,-0.244808193,0.346336001″,”001”));

the program doesn’t seem to work.

I just changed this line in TrainingPattern.as:

this.input = inputPattern.split(“,”);

in order to split the input, which in my case contains commas.

Thanks!

adminJune 9th, 2011 - 09:41

a training pattern must contain binary input and binary output.

if you have a large 64 bit number as input, then you must add 64 inputNeurons and covert your Number to binary and give it like that. a neuron can input either a 0 or a 1 not a decimal number.

PedroApril 25th, 2013 - 16:32

Hi!

I´m really interested in neural networks in actionscript and I was wondering if you could help understand the basis of this example.

King regards

Pedro

adminApril 25th, 2013 - 17:28

Hi, the second link as reference material at the bottom of the article is extremely nice. My code is based in what I’ve learned from there. After few pages you’ll learn to calculate it all by hand on a piece of paper. If you try and fail give me a shout and I’ll try to explain it myself. Cheers

PedroApril 27th, 2013 - 02:53

Really appreciate your help!

Thank you!

Pedro

d.August 9th, 2013 - 12:55

Hey,

I think there is an error in the NNetwork.as in the function create.

It creates one Layer less than expected.

otherwise great example, thanks. I was able to extend it quite easy

to some simple Number recognition and currently trying to include

a context Layer to get an Elman net.

regards

d.