xxxxxxxxxx
47
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
let network
function setup() {
createCanvas(640, 360);
// Create the Network object
network = new Network(width/2, height/2);
let layers = 3;
let inputs = 2;
let output = new Neuron(250, 0);
for (let i = 0; i < layers; i++) {
for (let j = 0; j < inputs; j++) {
let x = map(i, 0, layers, -280, 280);
let y = map(j, 0, inputs-1, -75, 75);
let n = new Neuron(x, y);
if (i > 0) {
for (let k = 0; k < inputs; k++) {
let prev = network.neurons[network.neurons.length-inputs+k-j];
network.connect(prev, n, random(0.0000001,1));
}
}
if (i == layers-1) {
network.connect(n, output, random(0.0000001, 1));
}
network.addNeuron(n);
}
}
network.addNeuron(output);
}
function draw() {
background(220);
network.update();
network.display();
if (frameCount % 30 == 0) {
network.feedforward(random(1), random(1));
}
}