Skip to content

Commit

Permalink
Make ann use mutable object instead of numbers directly
Browse files Browse the repository at this point in the history
  • Loading branch information
U1F30C committed Nov 26, 2020
1 parent b9e5544 commit 46a2a36
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ai/ann/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Network(layerDescriptors, learningRate = 0.5) {
network.layers[l].neurons.forEach((neuron, i) => {
neuron.error = sum(
network.layers[l + 1].neurons.map(function (n) {
return n.weights[i] * n.delta;
return n.weights[i].value * n.delta;
}),
);

Expand Down
11 changes: 7 additions & 4 deletions src/ai/ann/Neuron.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { dot, activations } = require('./math');
const { times } = require('lodash');

function Neuron(inputQuantity = 1, type = 'linear') {
let weights = times(inputQuantity + 1, Math.random);
let weights = times(inputQuantity + 1, () => ({ value: Math.random() }));
let neuron = {
weights,
predict,
Expand All @@ -13,12 +13,15 @@ function Neuron(inputQuantity = 1, type = 'linear') {
};

function _predict(inputs) {
return dot(neuron.weights, inputs);
return dot(
neuron.weights.map(w => w.value),
inputs,
);
}

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({ value: Math.random() });
neuron.inputs = inputs;
neuron.output = activations[type].function(_predict(inputs));

Expand All @@ -27,7 +30,7 @@ function Neuron(inputQuantity = 1, type = 'linear') {

function adjust(delta) {
for (let i = 0; i < neuron.weights.length; i++) {
neuron.weights[i] += delta * neuron.inputs[i];
neuron.weights[i].value += delta * neuron.inputs[i];
}
}

Expand Down

0 comments on commit 46a2a36

Please sign in to comment.