Skip to content

Instantly share code, notes, and snippets.

@tpgmartin
Last active May 16, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpgmartin/35b14ec3775a65cef21d51f06d8b63e1 to your computer and use it in GitHub Desktop.
Save tpgmartin/35b14ec3775a65cef21d51f06d8b63e1 to your computer and use it in GitHub Desktop.
Basic feedforward neural network in javascript
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