Single neuron on JavaScript

Pavel Ilin
3 min readDec 3, 2019

--

Goal: Create single neuron which would accept vector of values and output a number in range 0.5…1

For example:
input: [5,3,2]
output: 0.5…1

Initialization (n) — process of weights initialization
Neuron (x, w) — function of a single neuron

w = () array of weights

let w = []
const x = [10,3,1]
const a = 0.033
function initialization (n) {
for(let i = 0; i < n; i++){
w[i] = Math.random()
}
}
function neuron (x, w) {
let y = 0
for(let i = 0; i < x.length; i++){
y += x[i] * w[i]
}
return (1 / (1 + Math.exp(-a * y) ) )
}

Learning process

We have space 10x5 cels which separated on two horizontal sheets.
We need to teach neuron to determine in coordinates on which sheet it is.

Considering that we working in 2 dimension space, as an input we feed to neuron call address on X and Y coordinates.

We can divide our sheet on left and right side. Output of the neuron will be side of the sheet which current neuron on.

Xn = 10 — amount horizontal cels
Yn = 5 — amount vertical cels
P = 2 — amount sheets
N = 3 — amount of weights
L = 0.5 — neuron output for left side
R = 1 — neuron output for right side

Preparation of the data

const Xn = 10, Yn = 5, P = 2, N = 3, L = 0.5, R = 1let buf = [], Ts = [], Result = ['Left side', 'Right side']
function begin(n) {
initialization(n)
cArr(Xn, Yn)
sortArr(buf)
}
function cArr(x,y) {
let number = 0
for(let i = 1; i <= x; i++)
{
for(let j = 1; j <= y; j++)
{
buf.push( [] );
buf[number].push( i );
buf[number].push( j );
buf[number].push( ( i <= (Xn/P) ? L : R ) );
number++;
}
}
}
function sortArr(b) {
while(b.length > 0)
{
Ts.push( b.pop() );
Ts.push( b.shift() );
}
}

Learning function

function teach() {
let k = Ts.length, n = 0.01;
for(let i = 0; i < k; i++)
{
var b = Ts[i][2] - neuron([Ts[i][0],Ts[i][1],1],w);
for(let j = 0; j < (N-1); j++){
for(let l = 0; l <= 1; l++){
w[j] += n * b * Ts[i][l];
}
}
w[N-1] += n * b * 1;
}
}

Testing Model

Now let’s try our neuron. We call function answer() with argument o with which we pass neuron function: a = neuron(x,w)

// o = neuron(x,w)function answer(a) {
let left = Math.abs(L - a),
right = Math.abs(R - a);
if(left < right){
return Result[0];
}
else
{
return Result[1];
}
}

Code of working neuron can be find here.

Conclusion

Despite the fact that python most popular programming language for machine learning, javascript doing great job as well. We can use any language to implement math for designing intelligent systems.

--

--

Pavel Ilin
Pavel Ilin

Written by Pavel Ilin

Software Engineer, Researcher and Transhumanist.

No responses yet