Skip to content

Commit

Permalink
Update network inputs
Browse files Browse the repository at this point in the history
Add case use for testing :(
  • Loading branch information
U1F30C committed Nov 26, 2020
1 parent a01914c commit b9e5544
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/ai/ann/Layer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Neuron } from "./../utils/Neuron";
const { Neuron } = require('./Neuron');

function Layer([size, activation]) {
let layer = {
neurons: Array.from(Array(size)).map((_) => Neuron(1, activation)),
neurons: Array.from(Array(size)).map(_ => Neuron(1, activation)),
error: Infinity,
predict,
};
function predict(inputs) {
layer.output = layer.neurons.map((neuron) => neuron.predict(inputs));
layer.output = layer.neurons.map(neuron => neuron.predict(inputs));
return layer.output;
}
return layer;
}

export { Layer };
module.exports = { Layer };
82 changes: 78 additions & 4 deletions src/ai/ann/Network.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Layer } from './Layer';
import { delta, mse } from './math';
import { sum } from 'lodash';
const { Layer } = require('./Layer');
const { delta, mse } = require('./math');
const { sum } = require('lodash');

function Network(layerDescriptors, learningRate = 0.5) {
let layers = [];
let trainingData = [];
layers = layerDescriptors.map(Layer);

const network = {
layers,
forward,
error: Infinity,
trainingData,
train,
converges,
};

function forward(inputs) {
Expand All @@ -19,7 +24,76 @@ function Network(layerDescriptors, learningRate = 0.5) {
return layerOutputs.slice(-1)[0];
}

function converges(acceptableError = 0.05) {
return network.error < acceptableError;
}

function train() {
const outLayer = network.layers.slice(-1)[0];
network.trainingData.forEach(([inputs, outputs]) => {
network.forward(inputs);
outLayer.neurons.forEach((neuron, i) => {
neuron.error = outputs[i] - neuron.output;
neuron.delta = neuron.deltaFunction(neuron.output, neuron.error);
});

for (let l = network.layers.length - 2; l >= 0; l--) {
network.layers[l].neurons.forEach((neuron, i) => {
neuron.error = sum(
network.layers[l + 1].neurons.map(function (n) {
return n.weights[i] * n.delta;
}),
);

neuron.delta = neuron.deltaFunction(neuron.output, neuron.error);

network.layers[l + 1].neurons.forEach(nextNeur =>
nextNeur.adjust(learningRate * nextNeur.delta),
);
});
}
});
network.error = mse(outLayer.neurons.map(n => n.error));
}

return network;
}

export { Network };
// export { Neuron };

const network = Network(
[
[3, 'sigmoid'],
[1, 'sigmoid'],
],
0.9,
);
const inputs = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
];
const outputs = [[0], [1], [1], [0]];

network.trainingData = inputs.map((inputSet, i) => [inputSet, outputs[i]]);

let actualOutputs;
let i = 0;
while (!network.converges(0.1)) {
network.train();
actualOutputs = inputs
.map(input =>
network
.forward(input)
.map(x => Math.round(x))
.join(','),
)
.join(';');
if (i == 1000) {
console.log(actualOutputs);
console.log(network.error);
i = 0;
}
i++;
}
11 changes: 5 additions & 6 deletions src/ai/ann/Neuron.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dot, activations } from "./math";
import { times } from "lodash";
const { dot, activations } = require('./math');
const { times } = require('lodash');

function Neuron(inputQuantity = 1, type = "linear") {
function Neuron(inputQuantity = 1, type = 'linear') {
let weights = times(inputQuantity + 1, Math.random);
let neuron = {
weights,
Expand All @@ -18,8 +18,7 @@ function Neuron(inputQuantity = 1, type = "linear") {

function predict(inputs) {
inputs = [...inputs, -1];
while (inputs.length > neuron.weights.length)
neuron.weights.push(Math.random());
while (inputs.length > neuron.weights.length) neuron.weights.push(Math.random());
neuron.inputs = inputs;
neuron.output = activations[type].function(_predict(inputs));

Expand All @@ -35,4 +34,4 @@ function Neuron(inputQuantity = 1, type = "linear") {
return neuron;
}

export { Neuron };
module.exports = { Neuron };
6 changes: 3 additions & 3 deletions src/ai/ann/math.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sumBy } from "lodash";
const { sumBy } = require('lodash');

function dot(v1, v2) {
let result = 0;
Expand All @@ -21,7 +21,7 @@ function generateLine(neuron) {
}

function mse(arr) {
return sumBy(arr, (x) => Math.pow(x, 2)) / arr.length;
return sumBy(arr, x => Math.pow(x, 2)) / arr.length;
}

const activations = {
Expand Down Expand Up @@ -66,4 +66,4 @@ const activations = {
},
};

export { dot, activations, mse };
module.exports = { dot, activations, mse };

0 comments on commit b9e5544

Please sign in to comment.