Initial commit.
This commit is contained in:
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "pwa-node",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch Program",
|
||||||
|
"skipFiles": [
|
||||||
|
"<node_internals>/**"
|
||||||
|
],
|
||||||
|
"program": "${workspaceFolder}\\nevron.js"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
207
nevron.js
Normal file
207
nevron.js
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
function nn_activate(x) {
|
||||||
|
var result = 1 / (1 + Math.pow(Math.E, (-1*x)));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cmpArray(a, b) {
|
||||||
|
if(a.length == b.length
|
||||||
|
&& a.every(function(u, i) {
|
||||||
|
return u === b[i];
|
||||||
|
})
|
||||||
|
){
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function shuffle(array) {
|
||||||
|
let currentIndex = array.length, randomIndex;
|
||||||
|
|
||||||
|
// While there remain elements to shuffle...
|
||||||
|
while (currentIndex != 0) {
|
||||||
|
|
||||||
|
// Pick a remaining element...
|
||||||
|
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||||
|
currentIndex--;
|
||||||
|
|
||||||
|
// And swap it with the current element.
|
||||||
|
[array[currentIndex], array[randomIndex]] = [
|
||||||
|
array[randomIndex], array[currentIndex]];
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Perceptron {
|
||||||
|
constructor() {
|
||||||
|
this.detectors = undefined;
|
||||||
|
this.data = undefined;
|
||||||
|
this.weights = [];
|
||||||
|
this.rezultat = 0;
|
||||||
|
this.treshold = 0.8;
|
||||||
|
this.learningRate = 0.1;
|
||||||
|
console.log("Ustvarjen nov perceptron");
|
||||||
|
}
|
||||||
|
|
||||||
|
percept() {
|
||||||
|
var i;
|
||||||
|
this.rezultat = 0;
|
||||||
|
for (i = 0; i < this.detectors; i++) {
|
||||||
|
this.rezultat += this.data[i] * this.weights[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
activate(data) {
|
||||||
|
this.data = data;
|
||||||
|
if(this.detectors === undefined) {
|
||||||
|
this.detectors = data.length;
|
||||||
|
for (var i = 0; i < this.detectors; i++) {
|
||||||
|
// return Math.floor(Math.random() * (max - min) + min);
|
||||||
|
this.weights[i] = (Math.random() * (10.0 - (0.0)) + (0.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.percept();
|
||||||
|
//console.log("Rezultat: " + this.rezultat + " : " + "Prag: " + this.treshold);
|
||||||
|
if (nn_activate(this.rezultat) >= this.treshold) {
|
||||||
|
//if (this.rezultat >= this.treshold) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateWeights(expectedOutput) {
|
||||||
|
var i;
|
||||||
|
var actualOutput;
|
||||||
|
var error;
|
||||||
|
if(expectedOutput == 1) {
|
||||||
|
expectedOutput = 5;
|
||||||
|
} else {
|
||||||
|
expectedOutput = -5;
|
||||||
|
}
|
||||||
|
for (i = 0; i < this.detectors; i++) {
|
||||||
|
actualOutput = this.weights[i] * this.data[i];
|
||||||
|
error = expectedOutput - actualOutput;
|
||||||
|
this.weights[i] = this.weights[i] + (this.learningRate * this.data[i] * error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
train(trainingData, expectedResult) {
|
||||||
|
var result = this.activate(trainingData);
|
||||||
|
if(result != expectedResult) {
|
||||||
|
this.updateWeights(expectedResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Layer {
|
||||||
|
constructor(perceptrons) {
|
||||||
|
this.perceptrons = perceptrons;
|
||||||
|
this.data = undefined;
|
||||||
|
this.layerPerceptrons = [];
|
||||||
|
this.rezultat = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
activate(data) {
|
||||||
|
this.data = data;
|
||||||
|
this.rezultat = [];
|
||||||
|
if(this.layerPerceptrons.length != this.perceptrons) {
|
||||||
|
for (var i = 0; i < this.perceptrons; i++) {
|
||||||
|
this.layerPerceptrons[i] = new Perceptron();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var j = 0; j < this.perceptrons; j++) {
|
||||||
|
this.rezultat[j] = this.layerPerceptrons[j].activate(this.data);
|
||||||
|
}
|
||||||
|
return this.rezultat;
|
||||||
|
}
|
||||||
|
|
||||||
|
trainPerceptrons(data, expectedResult) {
|
||||||
|
this.data = data;
|
||||||
|
this.rezultat = [];
|
||||||
|
if(this.layerPerceptrons.length != this.perceptrons) {
|
||||||
|
for (var i = 0; i < this.perceptrons; i++) {
|
||||||
|
this.layerPerceptrons[i] = new Perceptron();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (var j = 0; j < this.perceptrons; j++) {
|
||||||
|
this.layerPerceptrons[j].train(this.data, expectedResult[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
train(trainingData, expectedResult) {
|
||||||
|
//this.expectedOutput = expectedOutput;
|
||||||
|
var result = this.activate(trainingData);
|
||||||
|
if(cmpArray(result, expectedResult) != true) {
|
||||||
|
console.log('updating weights ...');
|
||||||
|
this.trainPerceptrons(trainingData, expectedResult);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Network {
|
||||||
|
constructor(inputLayer, hiddenLayer, outputLayer) {
|
||||||
|
this.inputLayer = new Layer(inputLayer);
|
||||||
|
this.hiddenLayer = new Layer(hiddenLayer);
|
||||||
|
this.outputLayer = new Layer(outputLayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
train(trainingData, expectedResult) {
|
||||||
|
var inputLayerResult = this.inputLayer.activate(trainingData);
|
||||||
|
var hiddenLayerResult = this.hiddenLayer.activate(inputLayerResult);
|
||||||
|
var networkResult = this.outputLayer.activate(hiddenLayerResult);
|
||||||
|
|
||||||
|
if(cmpArray(networkResult, expectedResult) != true) {
|
||||||
|
for(var i = 0; i < networkResult.length; i++) {
|
||||||
|
if(networkResult[i] < expectedResult[i]) {
|
||||||
|
var trainInput = this.inputLayer.train(trainingData, [1,1]);
|
||||||
|
var trainHidden = this.hiddenLayer.train(trainInput, [1,1,1,1,1]);
|
||||||
|
var trainOutput = this.outputLayer.train(trainHidden, expectedResult);
|
||||||
|
} else if(networkResult[i] > expectedResult[i]) {
|
||||||
|
var trainInput1 = this.inputLayer.train(trainingData, [0,0]);
|
||||||
|
var trainHidden1 = this.hiddenLayer.train(trainInput1, [0,0,0,0,0]);
|
||||||
|
var trainOutput1 = this.outputLayer.train(trainHidden1, expectedResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
activate(data) {
|
||||||
|
var inputLayerResult = this.inputLayer.activate(data);
|
||||||
|
var hiddenLayerResult = this.hiddenLayer.activate(inputLayerResult);
|
||||||
|
var networkResult = this.outputLayer.activate(hiddenLayerResult);
|
||||||
|
return networkResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("Hello World!");
|
||||||
|
|
||||||
|
mm_training_data = [
|
||||||
|
{ input: [1, 0], output: [1, 0, 0] },
|
||||||
|
{ input: [0, 1], output: [0, 1, 0] },
|
||||||
|
{ input: [1, 1], output: [0, 0, 1] },
|
||||||
|
{ input: [0, 0], output: [0, 0, 0] }
|
||||||
|
];
|
||||||
|
|
||||||
|
// const network = new Network(2, 2, 2);
|
||||||
|
|
||||||
|
|
||||||
|
const layer = new Layer(3);
|
||||||
|
layer.activate([1, 0]);
|
||||||
|
for(var xx = 0; xx < 10000; xx++) {
|
||||||
|
shuffle(mm_training_data);
|
||||||
|
layer.train(mm_training_data[0].input, mm_training_data[0].output);
|
||||||
|
layer.train(mm_training_data[1].input, mm_training_data[1].output);
|
||||||
|
layer.train(mm_training_data[2].input, mm_training_data[2].output);
|
||||||
|
layer.train(mm_training_data[3].input, mm_training_data[3].output);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(layer.activate([1, 0]));
|
||||||
|
console.log(layer.activate([0, 1]));
|
||||||
|
console.log(layer.activate([1, 1]));
|
||||||
|
console.log(layer.activate([0, 0]));
|
||||||
|
|
||||||
|
console.log('Tu smo');
|
||||||
|
|
12
package.json
Normal file
12
package.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"name": "nevron",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "nvron.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "node nvron.js"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
Reference in New Issue
Block a user