Last active
May 16, 2017 21:49
-
-
Save tpgmartin/35b14ec3775a65cef21d51f06d8b63e1 to your computer and use it in GitHub Desktop.
Basic feedforward neural network in javascript
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
class Network { | |
constructor(neuronsPerLayer = []) { | |
this.layers = neuronsPerLayer.length | |
this.neuronsPerLayer = neuronsPerLayer | |
this.biases = [] // call initialiseValues() | |
this.weights = [] // call initialiseValues() | |
} | |
// Train network with SGD | |
train() { | |
if (testData) tests = testData.length | |
n = trainingData.length | |
for (let i = 0; i < epochs; i++ ) { | |
// randomise training data | |
if (testData { | |
console.log(`Epoch ${i}: ${_evaluate(testData)}, ${tests}`) | |
} else { | |
console.log(`Epoch ${i} complete`) | |
} | |
} | |
} | |
_forwardPropagation(input) { | |
const biases = this.biases | |
return self.weights.map((w, i) => ( | |
sigomid(dotProduct(w, input) + biases[i]) | |
)) | |
} | |
_evaluate(testData) { | |
} | |
_cost_function() { | |
} | |
} | |
// Activation function and derivative | |
function sigmoid(x) { | |
return 1 / (1 + Math.exp(-x)) | |
} | |
function sigmoidDerivative(x) { | |
return sigmoid(x) * (1 - sigmoid(x)) | |
} | |
// Randomly generate weights using Box-Muller transformation | |
function initialiseValues() { | |
const u = Math.random() | |
const v = Math.random() | |
return Math.sqrt( -2.0 * Math.log( u ) ) * Math.cos( 2.0 * Math.PI * v ) | |
} | |
// Linear algebra helpers | |
function matrixMultiplication(m1, m2) { | |
const result = [] | |
for (let i = 0; i < m2.length; i++) { | |
result.push([]) | |
} | |
for (let i = 0; i < m2.length; i++) { | |
for (let j = 0; j < m1[0].length; j++) { | |
let sum = 0 | |
for (let k = 0; k < m1.length; k++) { | |
sum += m1[k][j] * m2[i][k] | |
} | |
result[i].push(sum) | |
} | |
} | |
return result | |
} | |
function transpose(m) { | |
const result = [] | |
for (let i = 0; i < m[0].length; i++) { | |
result.push([]) | |
} | |
for (let i = 0; i < m.length; i++) { | |
for (let j = 0; j < m[0].length; j++) { | |
result[j][i] = m[i][j] | |
} | |
} | |
return result | |
} | |
function dotProduct(m1, m2) { | |
return matrixMultiplication(m1, transpose(m2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment