This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Matrix = require('./matrix') | |
module.exports = class NeuralNetwork { | |
constructor( | |
inputNodes, | |
hiddenNodes, | |
outputNodes, | |
learningRate, | |
bias, | |
trainingCycles, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adjustWeights(input, weights, bias, gradients) { | |
const inputT = Matrix.transpose(input) | |
const weightsDelta = Matrix.multiply(gradients, inputT) | |
weights = Matrix.map(weights, { m: weightsDelta, add: true }) | |
bias = Matrix.map(bias, { m: gradients, add: true }) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
derivativeReLU(y) { | |
return y <= 0 ? 0 : 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calculateGradientDescent(input, error) { | |
let gradients = Matrix.map(input, { func: this.derivativeReLU }) | |
gradients = Matrix.map(gradients, { m: error, multiply: true }) | |
gradients = Matrix.map(gradients, { | |
number: this.learningRate, | |
multiply: true, | |
}) | |
return gradients | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train(trainData) { | |
for (let c = 0; c < this.trainingCycles; c++) { | |
const randomPicked = Math.floor(Math.random() * trainData.length) | |
const { output, hidden, input } = this.feedforward( | |
trainData[randomPicked].input | |
) | |
const answer = Matrix.fromArray(trainData[randomPicked].answer) | |
const outputError = Matrix.map(answer, { m: output, subtract: true }) | |
const outputGradients = this.calculateGradientDescent(output, outputError) | |
this.adjustWeights( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const trainingData = [ | |
// Black | |
{ input: [0, 0, 0], answer: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] }, | |
... | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mainColors = [ | |
'Black', | |
'Blue', | |
'Brown', | |
'Cyan', | |
'Green', | |
'Grey', | |
'Orange', | |
'Pink', | |
'Purple', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mainColors = [ | |
'Black', | |
'Blue', | |
'Brown', | |
'Cyan', | |
'Green', | |
'Grey', | |
'Orange', | |
'Pink', | |
'Purple', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ReLU(x) { | |
return x > 0 ? x : 0.0 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calculateFF(input, weights, bias) { | |
let output = Matrix.multiply(weights, input) | |
output = Matrix.map(output, { m: bias, add: true }) | |
output = Matrix.map(output, { func: this.ReLU }) | |
return output | |
} |
NewerOlder