Этот код JavaScript представляет концепцию простого персептрона в нейронной сети. Ниже приведен код прогнозирования штрафа для всей таблицы истинности , кроме таблицы XOR . Пожалуйста, запустите этот код в окне консоли вашего браузера и найдите, что не так.
Поскольку это простой одиночный нейрон, я не придавал большого значения скрытым слоям. Я тренирую это до 10000 итераций для лучшего результата.
//AND GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [0,1,0,0];
/*
//AND GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [0,1,0,0];
//OR GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [0,1,1,1];
//NAND GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [1,0,1,1];
//NOR GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [1,0,0,0];
//XOR GATE
var X1 = [0,1,1,0];
var X2 = [0,1,0,1];
var OUT = [0,0,1,1];
*/
var LR = 0.01; //Learning rate to speedup learning process.
var BIAS = 1; // Avoid sum become zero.
var TRAIN = 10000; //Epochs we need to run for accurate result
var WEIGHTS = [Math.random(),Math.random(),Math.random()]; //3 Random weights 2 for input & 1 for bias
//console.log("Initial Weights : "+WEIGHTS);
function neuron(x1,x2,out){
var sum = 0;
var error = 0;
//Sum of weighted x1,x2 and bias
sum = x1*WEIGHTS[0] + x2*WEIGHTS[1] + BIAS*WEIGHTS[2];
//Heaviside step function as activation function
if(sum>1){
sum = 1;
}else{
sum = 0;
}
//Calculate the error
error = out - sum;
//Adjust weights
WEIGHTS[0] = WEIGHTS[0] + error * x1 * LR;
WEIGHTS[1] = WEIGHTS[1] + error * x2 * LR;
WEIGHTS[2] = WEIGHTS[2] + error * BIAS * LR;
//console.log("Weights adjust : "+WEIGHTS);
}
function Train(){
//Epoch iteration eg- 10000 is good
for(var k=1;k<=TRAIN;k++){
//Train Four sets of truth table
for(var i=0;i<X1.length;i++){
neuron(X1[i],X2[i],OUT[i]);
}
}
}
function Predict(x1,x2){
var predict = 0;
predict = x1*WEIGHTS[0] + x2*WEIGHTS[1] + BIAS*WEIGHTS[2];
if(predict>1){
predict = 1;
}else{
predict = 0;
}
//Predict for given input
console.log("The prediction for "+(x1+","+x2)+" is "+predict);
}
//First train the perceptron
Train();
//Predict for given input
Predict(1,1);
Predict(0,0);
Predict(1,0);
Predict(0,1);
Выход для гейта XOR
The prediction for 1,1 is 1
The prediction for 0,0 is 1
The prediction for 1,0 is 1
The prediction for 0,1 is 1