//---- normal P5 code -------------------------------------------------------
function setup()
{
let nn = new NeuralNetwork(2, 2, 1);
let inputs = [1,0];
let targets = [1];
// let output = nn.feedforward(input);
//console.log(output);
nn.train(inputs, targets);
}
function draw()
{
}
function sigmoid(x){
console.log("Sigmoid" + x);
return 1 / (1 + Math.exp(-x));
}
class NeuralNetwork{
constructor(numI, numH, num0){
this.input_nodes = numI;
this.hidden_nodes = numH;
this.output_nodes = num0;
this.weights_ih = new Matrix(this.hidden_nodes, this.input_nodes);
this.weights_ho = new Matrix(this.output_nodes, this.hidden_nodes);
this.weights_ih.randomize();
this.weights_ho.randomize();
this.bias_h = new Matrix(this.hidden_nodes, 1);
this.bias_o = new Matrix(this.output_nodes, 1);
this.bias_h.randomize();
this.bias_o.randomize();
console.log(this.weights_ih.data);
}
feedforward(input_array){
let inputs = Matrix.fromArray(input_array);
let hidden = Matrix.multiply(this.weights_ih, inputs);
hidden.add(this.bias_h);
//activateion
hidden.map(sigmoid);
let output = Matrix.multiply(this.weights_ho, hidden);
output.add(this.bias_o);
//activate
output.map(sigmoid);
return output.toArray();
}
train(inputs, targets){
let outputs = this.feedforward(inputs);
console.table(outputs);
outputs = Matrix.fromArray(outputs);
targets = Matrix.fromArray(targets);
//calculate the error
let output_errors = Matrix.subtract(targets, outputs);
let who_t = Matrix.transpose(this.weights_ho);
let hidden_errors = Matrix.multiply(who_t, output_errors);
}
}
class Matrix{
constructor(rows, cols){
this.cols = cols;
this.rows = rows;
this.data = [];
for (var i = 0; i < this.rows; i++){
this.data[i] = [];
for (var j = 0; j < this.cols; j++){
this.data[i][j] = [];
}
}
return this;
}
static fromArray(arr){
let m = new Matrix(arr.length, 1);
for (var j = 0; j < arr.length; j++){
m.data[j][0] = arr[j];
}
m.print();
return m;
}
static subtract(a, b){
console.table(b);
let result = new Matrix(a.rows, a.cols)
for (var i = 0; i < result.rows; i++){
for (var j = 0; j < result.cols; j++){
result.data[i][j] = a.data[i][j] - b.data[i][j];
}
}
return result;
}
toArray(){
let arr = [];
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
array.push(this.data[i][j])
}
}
return arr;
}
randomize() {
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
this.data[i][j] = Math.random() * 2 - 1;
}
}
}
static multiply(a, b){
let result = new Matrix(this.rows, b.cols);
for (let i =0; i < result.rows; i++){
for(let j = 0; j < result.cols; j++){
let sum = 0;
for(let k = 0; k < a.cols; k++){
sum+= a.data[i][k] * b.data[k][j];
}
result.data[i][j] = sum;
}
}
return result;
}
map(func){
console.log("map");
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++)
{
let val = this.data[i][j];
console.log(val);
this.data[i][j] = func(val);
}
}
}
transpose(){
let result = new Matrix(this.cols, this.rows);
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
result.data[j][i] = this.data[i][j];
}
}
return result
}
add(n){
if(n instanceof Matrix){
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
this.data[i][j] += n.data[i][j];
}
}
}else{
for (var i = 0; i < this.rows; i++){
for (var j = 0; j < this.cols; j++){
this.data[i][j] += n;
}
}
}
}
print(){
console.table(this.data);
}
}