Thursday, September 24, 2009

Part one in my AI for Flash Devs: How to make your own SkyNet in AS3!

I have an unhealthy obsession with Artificial Intelligence. I've always wanted to make a trainable system that learns on it's own. Sounds like fun right?

Don't be scared. It's actually not that difficult to do.

For part one of my intro to Artificial Intelligence for Flash Devs series I could have gone over Finite State Machines, Chasing and Evading, Pattern Movement, or Flocking. Yep, I really could have, but those have been done to death. Let's do something interesting. Let's make a Perceptron.

Think of a Perceptron as a smart Boolean. Due to the various stimulus it receives and it's training it will choose one of 2 choices. This is called a Linear Classifier because the problems it can solve have to be linearly separable (yes or no, red or blue, buy or don't buy).

So, here's an example with everything shown: Seantron's Perceptron.

Let me explain what's going on here.



This is your Network. Blue is input X and Red is input Y and those are connected directly to your output O (the white circle). So each connection has a weight value, and by adjusting the weights we are able to train the Network to do stuff on it's own.



This is the graphical representation of your Training Data. Huh? Well, let's say you have figured out a system for buying stocks. Your blue points could be stocks that meet the criteria that you want to follow to Buy The Crap Out of those Stocks! And guess what the red points would be? You guessed it, Those Stocks Suck Ass, Don't BUY!

So you training data sets the weights (which are those crazy numbers in the Network pic) which allows for the Network to buy or not to buy on it's own. Yes, in this example we are training the system by hand, but I will teach you how to use something called Back Propagation to set up a system to train your Network for you.

*As per makc3d's directions* end edit:

Here are some links to help you with what is going on with the science stuff:

http://en.wikipedia.org/wiki/Perceptron
http://www.youtube.com/watch?v=eDYOH9q2QdA

Gradient Descent is what is used to calibrate the Weights.

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



Here's the AS3 for the Perceptron example:

DOWNLOAD THE .AS HERE



*Updated the Code and posted it at the link above.  It's been awhile and it seems this blog got weird, sorry about that.*

6 comments:

makc said...

(continued from twitter) what I mean is that you jump straight to code without explaining how does it work and why does it work, or even linking to wikipedia. I agree that 90% of people just want to copy and paste code, but I think blogs should be written for other 10% of people.

Seantron said...

There dude, a little better? This is my first one in the series, so I'm still learning how to teach this stuff too.

So, please be patient with me. :)

Anonymous said...

Your code around the for loop is formatted improperly. It does not compile and I can not figure out what the exact errors are to fix it myself.

Seantron said...

My apologies. This blog is old, and seems that the code got weirdly formatted (lost the code format). I've updated the site with a link to the .AS file.

Anonymous said...

Thanks! It works great now. Are you going to do more in this AI series?

Seantron said...

I would love to, and I have every intention of doing so. Just finding the time is difficult.