0
$\begingroup$

I'm pretty new to ML and was starting out with linear regression combined with gradient descend.

This is the equation I was trying to achieve using javascript-

enter image description here

And this is what I came up with in js-

      function algorithmify(){
    let sumHDiff=hypotheses();
    if(Math.round(hypotheses())!=0){
      let sumHDiffMult=hypothesesWithMult();
      T0-=0.0001*sumHDiff/points;
      T1-=0.0001*sumHDiffMult/points;
      
      console.log(T0,T1);
    }
  }
  
  function hypotheses(){
    let sum=0;
    for(var i=0;i<points;i++){
      let hypo=T0+(T1*X[i]);
      let diff=hypo-Y[i];
      diff*=diff;
      sum+=diff;
    }
    return sum;
  }
  function hypothesesWithMult(){
    let sum=0;
    for(var i=0;i<points;i++){
      let hypo=T0+(T1*X[i]);
      let diff=hypo-Y[i];
      diff*=diff;
      mult=diff*X[i];
      sum+=mult;
    }
    return sum;
  }

I tried to match the code with the equation and even though I haven't done good variable naming, I'm pretty sure it is following the equation correctly.

But the result of $T0$ and $T1$ representing theta 0 and theta 1 first outputs very large negative numbers and after a few iterations, output $-infinity$ both times.

Any help would be greatly appreciated!

$\endgroup$
0
$\begingroup$

Found the solution! In the equation,there is no squaring the sum but idk why I added it in.

Removing that fixed the issue. Yet I still am not sure when the data is converging

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.