EN
JavaScript - artificial neuron model
7 points
In this short article, we would like to show an example of Artificial Neuron implementation in JavaScript.
Hint: the article describes McCulloch–Pitts (MCP) neuron that is commonly used in AI.
Quick solution:
xxxxxxxxxx
1
const activationFunction = (sum) => sum;
2
3
const inputs = [ 1, 2, 3 ]; // x(1) x(2) x(3)
4
const bias = 1; // w(0)
5
const weights = [ 0, 0.5, 1 ]; // w(1) w(2) w(3)
6
7
const sum = weights.reduce((sum, weight, index) => sum + inputs[index] * weight, bias); // Σ
8
const output = activationFunction(sum); // y = f(Σ)
9
10
console.log(output); // y = 5

Where:
n | number of neuron inputs (number of neuron weights must be the same) |
1 | was placed on the above model only to show bias/threshold concept (multiplication by 1 is neutral but useful when we work on matrixes) |
w(0) | weight called bias/threshold that is used to stimulate or suppress neuron activity |
x(1) , ..., x(n) | neuron input values (it is just an array of input values) |
w(1) , ..., w(n) | neuron weight values that are used to describe how strong influence has related input to the neuron |
Σ | describes summation processΣ = 1*w(0) + x(1)*w(1) + ... + x(n-1)*w(n-1) + x(n)*w(n) |
f(Σ) | describes sum passing through a neuron activation function (we can use different activation functions depending on the solved problem) |
y | neuron output value |
So, the above neuron uses formula.
The above model can be calculated using matrixes.
Where:
w
is a column vector (array) of neuron weight values (fromw(0)
tow(n)
),x
is a column vector (array) of neuron input values (from1
tox(n)
),f
is neuron activation function,y
is neuron output value.
Note: go to the following articles to know more about vectors and matrixes:
In this section, you can see simple step-by-step calculations made by artificial neurons.
xxxxxxxxxx
1
// Calculates sum using:
2
// Σ = 1*w(0) + x(1)*w(1) + ... + x(n-1)*w(n-1) + x(n)*w(n)
3
//
4
const caclulateSum = (bias, weights, inputs) => {
5
let sum = bias;
6
for (let i = 0; i < weights.length; ++i) {
7
sum += inputs[i] * weights[i];
8
}
9
return sum;
10
};
11
12
// Used activation function
13
//
14
// We use simple linear activation function (called Identity Function)
15
// https://en.wikipedia.org/wiki/Activation_function
16
//
17
const activationFunction = (sum) => sum;
18
19
20
// Neuron calculation:
21
22
const inputs = [ 1, 2, 3 ]; // x(1) x(2) x(3)
23
24
const bias = 1; // w(0)
25
const weights = [ 0, 0.5, 1 ]; // w(1) w(2) w(3)
26
27
const sum = caclulateSum(bias, weights, inputs);
28
const output = activationFunction(sum);
29
30
console.log(output); // 5
In this section, you will find simple Neuron class implementation that can be combined into layers.
xxxxxxxxxx
1
function Neuron(bias, weights, activationFunction) {
2
this.bias = bias;
3
this.weights = weights;
4
this.calculate = function(inputs) {
5
var sum = this.bias;
6
for (var i = 0; i < this.weights.length; ++i) {
7
sum += inputs[i] * this.weights[i];
8
}
9
return activationFunction(sum);
10
};
11
}
12
13
14
// Usage example:
15
16
var identityFunction = function(sum) {
17
return sum;
18
};
19
20
var neuron = new Neuron(1, [0, 0.5, 1], identityFunction);
21
var output = neuron.calculate([1, 2, 3]);
22
23
console.log(output); // 5