Languages
[Edit]
EN

JavaScript - distance neuron

7 points
Created by:
DEX7RA
580

In this short article, we would like to show how to implement distance neuron in JavaScript.

Practical example:

// ONLINE-RUNNER:browser;

// Distance neuron:

// -- distances

// Source: https://dirask.com/posts/JavaScript-Chebyshev-Distance-function-p5q6Rp
//
function calculateChebyshevDistance(a, b) {
    if (a.length === 0 || a.length !== b.length) {
      	return NaN;
    }
    var max = Math.abs(a[0] - b[0]);
    for (var i = 1; i < a.length; ++i) {
        var tmp = Math.abs(a[i] - b[i]);
        if (tmp > max) {
            max = tmp;
        }
    }
    return max;
}

function calculateEuclideanDistance(a, b) {
    if (a.length === 0 || a.length !== b.length) {
      	return NaN;
    }
	var sum = 0.0;
	for (var i = 0; i < a.length; ++i) {
		var tmp = a[i] - b[i];
		sum += tmp * tmp;
	}
	return Math.sqrt(sum);
}

// Source: https://dirask.com/posts/JavaScript-Rectilinear-Distance-function-1XgJgj
//
function calculateRectilinearDistance(a, b) {
    if (a.length === 0 || a.length !== b.length) {
      	return NaN;
    }
    var sum = 0.0;
    for (var i = 0; i < a.length; ++i) {
        sum += Math.abs(a[ i ] - b[i]);
    }
    return sum;
}

// -- neuron

function DistanceNeuron(weights, distance) {
	this.getWeights = function() {
		return weights;
	};
	this.getDistance = function() {
		return distance;
	};
	this.randomize$1 = function() {
		this.randomize$2(0.0, 1.0);
	};
	this.randomize$2 = function(min, max) {
		var range = max - min;
        for (var i = 0; i < weights.length; ++i) {
            weights[i] = min + range * Math.random();
        }
	};
	this.compute = function(inputs) {
		return distance.call(null, weights, inputs);
    };
}

// -- utils

function createDistanceNeuron(inputsCount, distance, createWeight) {
    var weights = Array(inputsCount);
    for (var i = 0; i < weights.length; ++i) {
        weights[i] = createWeight(i);
    }
    return new DistanceNeuron(weights, distance);
}

function createZeroedNeuron(inputsCount, distance) {
    return createDistanceNeuron(inputsCount, distance, function() {
        return 0.0;
    });
}

function createRandomNeuron(inputsCount, distance) {
    return createDistanceNeuron(inputsCount, distance, function() {
        return Math.random();
    });
}


// Usage example:

const inputsCount = 3;

// Available dinstance functions:
//
//   - calculateChebyshevDistance
//   - calculateRectilinearDistance
//   - calculateEuclideanDistance
//
var neuron = createRandomNeuron(inputsCount, calculateEuclideanDistance);

var inputs = [1, 2, 3];
var ouput = neuron.compute(inputs);

console.log(ouput);  // Example value: 3.2837672683668795

Hint: in real problem it is good to work with normalized data (input data).

 

See also

  1. JavaScript - Chebyshev Distance function

  2. JavaScript - Rectilinear Distance function

  3. JavaScript - distance network

  4. JavaScript - artificial neuron model

  5. JavaScript - Kohonen Neural Network (WTA Learning)

  6. JavaScript - Kohonen Neural Network (WTM Learning)

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join