EN
TypeScript - artificial neuron model
0 points
In this short article, we would like to show an example of Artificial Neuron implementation in TypeScript.
Hint: the article describes McCulloch–Pitts (MCP) neuron that is commonly used in AI.
Quick solution:
xxxxxxxxxx
1
const activationFunction = (sum: number): number => sum;
2
3
const inputs: number[] = [ 1, 2, 3 ]; // x(1) x(2) x(3)
4
const bias: number = 1; // w(0)
5
const weights: number[] = [ 0, 0.5, 1 ]; // w(1) w(2) w(3)
6
7
const sum: number = weights.reduce((sum, weight, index) => sum + inputs[index] * weight, bias); // Σ
8
const output: number = activationFunction(sum); // y = f(Σ)
9
10
console.log('y = ' + output); // y = 5
Output:
xxxxxxxxxx
1
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 a 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: number, weights: number[], inputs: number[]): number => {
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: number[] = [ 1, 2, 3 ]; // x(1) x(2) x(3)
23
24
const bias: number = 1; // w(0)
25
const weights: number[] = [ 0, 0.5, 1 ]; // w(1) w(2) w(3)
26
27
const sum: number = caclulateSum(bias, weights, inputs);
28
const output: number = activationFunction(sum);
29
30
console.log(output); // 5