|
Author |
Topic: Neural Networks (Read 2742 times) |
|
st33d
|
Neural Networks
« on: Feb 1st, 2005, 11:59pm » |
|
I've been wondering how to make my applets look cool for ages. I just wandered by TomC's gaffe after looking up the G2 portal and had a bit of a revelation about using the pixels array. So I thought I'd share this number now that it looks prettier and see if any one had any ideas for its expansion and development. I've basically been looking at neural net theory. Whilst I've been unable to implement it so far I had an idea that perhaps I could create a simple perceptron based neural net with the information passed being about trajectory. Instead of firing the information through layers of perceptrons they fire to each other, but their weights steer them to the ideal direction of the mouse pointer. Making this lovely looping dance of confusion. I've only experimented with multiples of the wiring class (which sends information between perceptrons) so far. Code: wiring p; void setup(){ p = new wiring(400); background(255); size(400,400); } void loop(){ p.load(); p.fire(); p.learn(); p.update(); } class wiring{ perceptron [] p; wiring(int n){ p = new perceptron[n]; for (int i = 0; i < p.length; i++){ p[i] = new perceptron(0.01,(400/n)*i); } } void load(){ for (int i1 = 0; i1 < p.length; i1++){ for (int i2 = 0; i2 < p[i1].ins.length; i2++){ p[i1].ins[i2] = p[(i1+(i2+1))%p.length].ot; } } } void fire(){ for (int i = 0; i < p.length; i++){ p[i].fire(); } } void learn(){ for (int i = 0; i < p.length; i++){ float theta = atan2(mouseY-p[i].y,mouseX-p[i].x); //float theta = atan2(y-p[i].y,x-p[i].x) / TWO_PI; //float theta = atan2(200-p[i].y,200-p[i].x) / TWO_PI; float d = (theta + PI)/TWO_PI; for (int i2 = 0; i2 < p[i].w.length; i2++){ p[i].learn(i2,d); } } } void update(){ //background(200); for (int i = 0; i < p.length; i++){ p[i].x = fwrap(p[i].x + (cos(p[i].ot*TWO_PI) * 1),width); p[i].y = fwrap(p[i].y + (sin(p[i].ot*TWO_PI) * 1),height); //ellipse(p[i].x-5,p[i].y-5,10,10); stroke((255.0/400.0)*i); //println((255.0/400.0)*i); point(p[i].x,p[i].y); } } } class perceptron{ float x,y,ot,ln; float [] ins; float [] w; perceptron(float ln,float y){ //this.x = random(400); this.x = 0; //this.y = random(400); this.y = y; this.ot = 0.75; this.ln = ln; this.ins = new float[100]; this.w = new float[100]; for(int i = 0; i < w.length; i++){ w[i] = 1.0; } } void fire(){ ot = meanOut(ins,w); } void learn(int i, float d){ w[i] += ln * (d - ot); //w[i] += ln * (d - (w[i]*ins[i])); } } float meanOut(float [] inputs, float [] weights){ float sum = 0.0; for (int i = 0; i < inputs.length; i++){ sum += inputs[i] * weights[i]; } return sum / inputs.length; } float fwrap (float fvalu, float flimi){ float fcar=fvalu; if (fcar>flimi){ fcar=(fcar)%flimi; } if (fcar<0){ fcar=flimi-fcar; fcar=(fcar)%flimi; fcar=flimi-fcar; } return fcar; } |
|
|
I could murder a pint.
|
|
|
|