JavaScript Machine Learning Program



This awesome code uses Machine Learning to as the Program title indicates "How to teach a dumb program to add two numbers"


     *Featured code submitted and created by SOU✌️IK


Javascript in HTML


<head>
  <script
    src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
  <div class="card-header" align="center">
    <h3>How to teach a dumb program to add two numbers </h3>
  </div><br />
  <div class="alert alert-warning" role="alert" align="center">
    <div class="input-group mb-3" align="center">
      <div class="input-group-prepend">
        <span class="input-group-text" id="basic-addon1">a:</span>
      </div>
      <input type="text" class="col-xs-2" placeholder="enter any number" id="data_1" aria-describedby="basic-addon1">

        <div class="input-group-prepend">
          <span class="input-group-text" id="basic-addon1">b:</span>
        </div>
        <input type="text" class="col-xs-2" placeholder="enter any number" id="data_2" aria-describedby="basic-addon1">
    </div>
    <div class="input-group mb-3" >
      <div class="input-group-prepend">
        <span class="input-group-text" id="basic-addon1">a+b:</span>
      </div>
      <input type="text" class="col-xs-2" placeholder="enter value of a+b" id="data_sum" aria-describedby="basic-addon1">
    </div>
      <button type="button" class="btn btn-primary" id="train">Train</button>
    </div>
    <br />
    <div class="alert alert-success" role="alert" id="output" align="center">
      <div class="input-group mb-3" >
        <div class="input-group-prepend">
          <span class="input-group-text" id="wt_1">wt_1*</span>
        </div>
        <input type="text" class="col-xs-2" placeholder="enter any number" id="test_data_1" aria-describedby="basic-addon1">
         +
         <div class="input-group-prepend">
           <span class="input-group-text" id="wt_2">wt_2*</span>
         </div>
         <input type="text" class="col-xs-2" placeholder="enter any number" id="test_data_2" aria-describedby="basic-addon1">
         =<h4><span class="badge badge-secondary" id="res">null</span></h4>
      </div>
        <button type="button" class="btn btn-primary" id="test">Test</button>
    </div>
</div>

  <div class="card-footer text-muted" align="center" id="credit">
  author:<a href="https://www.sololearn.com/Profile/4194601" id="auth"> souvik kar mahapatra </a>
  <br/>
</div>
</body>

<script>
if(document.getElementById('auth')){
alert("lets teach a dumb program how to add two numbers.");
}else{
alert("this is a copied code, original author is souvik kar mahapatra. plz report");
}

data=[5.234,5.234];//training data
wts=[(Math.random()*2)-1,(Math.random()*2)-1];//random weights btwn 1 and -1
var err=0;
var got_res=0;
var c=0.01;
var want_res=10.468;
//train function
var train=function(data,wts,err,c,want_res,got_res){
  //calculate result
  for(let i=0;i<data.length;i++){
    got_res+=data[i]*wts[i];
  }
  err=want_res-got_res;//calulating error
  if(Number(err.toFixed(2))==0){
     //training complete
    // console.log(wts);
  }else{
     //adjust errors
     for(let j=0;j<data.length;j++){
       wts[j]+=c*err*data[j];
     }
     got_res=0;
     train(data,wts,err,c,want_res,got_res);
  }
  return wts;
}
//train(data,wts,err,c,want_res,got_res)

$('#train').click(function(){
  data=[document.getElementById('data_1').value,document.getElementById('data_2').value];//training data
  wts=[(Math.random()*2)-1,(Math.random()*2)-1];//random weights btwn 1 and -1
  var err=0;
  var got_res=0;
  var c=0.01;
  var want_res=document.getElementById('data_sum').value;
  var wt=train(data,wts,err,c,want_res,got_res);
  //console.log(wt);
  //testing part***********************************************************
$('#test').click(function(){
  var a=document.getElementById('test_data_1').value;
  var b=document.getElementById('test_data_2').value;
  console.log(wt);
  var test_sum=wt[0]*a+wt[1]*b;
  document.getElementById('wt_1').innerHTML=wt[0]+" x ";
  document.getElementById('wt_2').innerHTML=wt[1]+" x ";
  document.getElementById('res').innerHTML=test_sum;
   });
});
</script>

Click here to run this fun code




More Free Code Examples:


Javascript Substitution Cipher


Javascript Easy Battleship Game


Hello World in 10 Programming Languages







Comments

Post a Comment

Popular posts from this blog

Crypto Mining

Multi-tap Keypad Text Entry