what is wrong with the code?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is wrong with the code??

hello everyone i was trying to make a single perceptron learning model in js in my browser but i dont know why first it worked good for 2-3 time after that my browser crashed, i tried this many times same thing happened. please help me out(here is the code): var weights=[]; var n=3; var c=0.01;//learning constant //weights initialization console.log("initial weights \n"); for(let i=0;i<n;i++){ weights[i]=((Math.random()*2)-1).toFixed(3); console.log(weights[i]); } //train function /*function train(inputs,weights,c,desired,n){ var sum=0; for(let j=0;j<n;j++){ sum+=weights[j]*inputs[j]; //doing sum; } console.log("sum:"+sum+"\n"); var opt=activate(sum);//activating to get desired output console.log("des:"+opt+"\n"); var err=desired-opt; if(err===0){ console.log("all set perfect"); }else{ //minimizing error var newwt=learn(err,c,weights,inputs); console.log("---------------------------------------\n"); train(inputs,newwt,c,desired,n); } } //activation sign function function activate(sum){ if(sum>0){ return 1; }else{ return -1; } } function learn(err,c,weights,inputs){ for(let k=0;k<n;k++){ weights[k]+=err*inputs[k]*c; } console.log("new weights:\n"+weights+"\n"); return weights; } var inputs=[12,4,1]; train(inputs,weights,c,1,n); i tried running it in sololearn playground the same thing happened first it displayed the for 2-3 time and the when i tried running it again it was displaying running.... plz help me out

21st May 2018, 9:33 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
15 Answers
+ 2
your train function call itself without stop.... In pratice your code get an infinite recursive loop
21st May 2018, 10:52 AM
KrOW
KrOW - avatar
+ 5
sorry, I can't help you. my SoloLearn app is crashed while I trying your code.
21st May 2018, 9:53 AM
Amethyst Animion
Amethyst Animion - avatar
+ 1
souvik kar mahapatra n parameter in train function is the number that you have to repeat training?? If yes, when you call it inside train, you have to decrement it and if zero you have to stop training
21st May 2018, 12:13 PM
KrOW
KrOW - avatar
+ 1
KrOW ok I will make changes and if it works I'll let u know by the way thanx for helping
21st May 2018, 1:48 PM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
21st May 2018, 9:54 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
KrOW how can I fix this plz give me a hint
21st May 2018, 11:17 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
Start with create a code and save to you code list... Then post his link (not his code)
21st May 2018, 11:35 AM
KrOW
KrOW - avatar
21st May 2018, 11:47 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
ok thanx
21st May 2018, 12:16 PM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
KrOW basically n is the no of inputs so first I run a loop n times to randomly initialize weight btwn 1 and -1. then I run a loop inside function to calculate weighted sum . pass the sum through the activation function which returns 1 for +ve sum and -1 for -ve . after getting result from activation function I calculate error. if error is 0 then the model is perfect but if it is not then I product error with input and add it to weight and again repeat the above process untill it gets the desired output.
21st May 2018, 12:21 PM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
suppose I got a result which is not equal to the desired output then after continuous calling of train function a time will come when the result will be equal to the desired so instead of going to the 'else' part That is 'error!=0' it goes to the 'error=0' that is 'if' part and prints the message and stops
21st May 2018, 12:25 PM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
I don't understand why there is an infinite loop going on
21st May 2018, 12:25 PM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
souvik kar mahapatra Sorry for late... I want make you note that .toFixed return a string not a float and because you use much weight (and they influence how much train its called) i think that its the problem
21st May 2018, 1:36 PM
KrOW
KrOW - avatar
0
KrOW thanx man after removing .toFixed() it was working fine
23rd May 2018, 5:38 AM
S O U ✌️ I K
S O U ✌️ I K - avatar
0
I made changes in above code u can now run it
23rd May 2018, 5:39 AM
S O U ✌️ I K
S O U ✌️ I K - avatar