// Cloned by Nithin Sai K J on 8 Nov 2023 from World "XOR multi-layer network" by "Coding Train" project
// Please leave this clone trail here.
// XOR multi-layer network
// Port from:
// https://github.com/CodingTrain/Toy-Neural-Network-JS/tree/master/examples/xor
// with modifications
// libraries from:
// https://github.com/CodingTrain/Toy-Neural-Network-JS/tree/master/lib
// ported to here:
// https://ancientbrain.com/uploads.php?userid=codingtrain
//=== Tweaker's box ============================================
// number of nodes in each layer:
const noinput = 2;
const nohidden = 6;
const nooutput = 1;
// 3D
const noinput3d = 3;
const nohidden3d = 6;
const nooutput3d = 1;
// define the exemplars to learn from:
let training_data = [
{ inputs: [0, 0], outputs: [0] },
{ inputs: [0, 1], outputs: [1] },
{ inputs: [1, 0], outputs: [1] },
{ inputs: [1, 1], outputs: [0] }
];
// 3D
let training_data_3d = [
{ inputs: [0, 0, 0], outputs: [0] },
{ inputs: [0, 0, 1], outputs: [1] },
{ inputs: [0, 1, 0], outputs: [1] },
{ inputs: [0, 1, 1], outputs: [0] },
{ inputs: [1, 0, 0], outputs: [1] },
{ inputs: [1, 0, 1], outputs: [0] },
{ inputs: [1, 1, 0], outputs: [0] },
{ inputs: [1, 1, 1], outputs: [1] },
];
var nn; // global var
const learningrate = 0.2;
// train this number of times per draw()
const notrain = 10;
// Take screenshot on this step:
AB.screenshotStep = 200;
// divide 0,1 into squares
// show all squares or just the corner squares:
var showall = true;
const canvassize = 400;
const squaresize = 4;
const cols = 1;
const rows = 10;
// Matrix.randomize() is changed to point to this. Must be defined by user of Matrix.
function randomWeight()
{
// return 5;
return ( AB.randomFloatAtoB ( -0.5, 0.5 ) );
// Coding Train default is -1 to 1
}
//=== End of tweaker's box ============================================
function setup()
{
createCanvas (canvassize*2, canvassize*2, WEBGL);
// $.getScript ( "/uploads/codingtrain/matrix.js", function()
// {
// $.getScript ( "/uploads/codingtrain/nn.js", function()
// {
// nn = new NeuralNetwork ( noinput3d, nohidden3d, nooutput3d );
// });
// });
for (let i = 0; i < cols; i = i + cols - 1) {
for (let j = 0; j < cols; j = j + cols - 1) {
for (let k = 0; k < cols; k = k + cols - 1) {
drawcube(i,j,k);
}
}
}
}
function draw()
{
// check if libraries loaded yet:
// if ( typeof nn == 'undefined' ) return;
// nn.setLearningRate ( learningrate );
background(0, 0, 0, 64);
orbitControl();
// Train the neural network
for (let i = 0; i < training_data_3d.length; i++) {
let data = training_data_3d[i];
nn.train(data.inputs, data.outputs);
}
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
for (let k = 0; k < gridSize; k++) {
push();
translate(i * squaresize - 90, j * squaresize - 90, k * squaresize - 90);
let inputs = [
map(i, 0, gridSize - 1, 0, 1),
map(j, 0, gridSize - 1, 0, 1),
map(k, 0, gridSize - 1, 0, 1)
];
let output = nn.predict(inputs);
// Map the output to color
let col = map(output[0], 0, 1, 0, 255);
fill(col);
box(squaresize);
pop();
}
}
}
}
function drawsquare ( i, j )
{
let x1 = i / cols;
let x2 = j / rows;
let inputs = [x1, x2];
let y = nn.predict(inputs);
y = y[0].toFixed(2);
// console.log ( "input (" +x1 + "," + x2 + ") output " + y );
strokeWeight(2);
stroke('black');
fill ( y * 255 ); // 0 is black, 1 is white
rect ( i * squaresize, j * squaresize, squaresize, squaresize );
}
function drawcube( i,j,k ) {
let x1 = i / cols;
let x2 = j / cols;
let x3 = k / cols;
let inputs = [x1,x2,x3];
// let y = nn.predict(inputs);
// y = y[0].toFixed(2);
let y = randomWeight();
// console.log(i,j,k, y)
const c = color(y * 255, y * 255, y * 255, 50);
fill(c);
translate(i * squaresize, j * squaresize, k * squaresize);
box(squaresize);
}