Last active
August 5, 2022 04:22
-
-
Save joshnuss/464cd8db026af7a66d58e69218ca259e to your computer and use it in GitHub Desktop.
Simple neuron
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
/* activation functions */ | |
// linear activation (no-op) | |
function linear(value) { return value } | |
// threshold activation | |
function threshold(x = 0) { | |
return (value) => value >= x ? 1 : 0 | |
} | |
// rectified/ReLU activation | |
function rectified(x = 0) { | |
return (value) => Math.max(x, value) | |
} | |
/* utility functions */ | |
function sum(list, mapFn) { | |
return list.reduce((acc, x, i) => { | |
return acc + mapFn(x, i) | |
}, 0) | |
} | |
// define a neuron | |
// | |
// bias: integer | |
// weights: integer[n] | |
// activationFn: function | |
// | |
// returns: function(...integer[n]) | |
function neuronFactory(bias, weights, activationFn = linear) { | |
return (...inputs) => { | |
// verify that parameter size is right | |
if (weights.length != inputs.length) { | |
throw new Error(`Invalid length. Expected ${weights.length} arguments, got ${inputs.length}.`) | |
} | |
// sum the total of each input multiplied by weight | |
const total = sum(inputs, (input, i) => input * weights[i]) | |
// compute the result value using the sum + bias | |
return activationFn(total + bias) | |
} | |
} | |
// define a neuron: bias, weights, activation function | |
let neuron = neuronFactory(3, [1, 1, 3], linear) | |
// use the neuron, and print result | |
console.log(neuron(1, 2, 3)) | |
// define a neuron | |
neuron = neuronFactory(10, [1, 2, 3, 8], rectified(0)) | |
// use the neuron, and print result | |
console.log(neuron(5, 2, 3, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment