Advertisement
sohotcall

Backpropagation

Jul 4th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.44 KB | None | 0 0
  1. <?php
  2. session_start();
  3. SET_TIME_LIMIT(2400);
  4. include_once "classes/db.class.php";
  5. include_once "classes/hak-akses.inc.php";
  6. include_once "classes/template.class.php";
  7. include_once "classes/class.file.php";
  8. include_once "pagination.php";
  9. include_once "ekstraksiPelatihan.function.php";
  10. include_once "kuda.php";
  11. $db = new DB();
  12. $file = new File();
  13. kickView($hakAkses[$_SESSION['hak']]);
  14. kickManage($hakAkses[$_SESSION['hak']]);
  15.  
  16. if (! class_exists('BackProp')) { Class BackProp {
  17.  
  18. function import($data){
  19.     $this->x = $data->x;
  20.     $this->z = $data->z;
  21.     $this->y = $data->y;
  22.     $this->minInput = $data->minInput;
  23.     $this->maxInput = $data->maxInput;
  24.     $this->minTarget = $data->minTarget;
  25.     $this->maxTarget = $data->maxTarget;
  26.     $this->v = $data->v;
  27.     $this->w = $data->w;
  28. }
  29.  
  30. function export(){
  31.     return (object) array(
  32.         'x'=>$this->x,
  33.         'z'=>$this->z,
  34.         'y'=>$this->y,
  35.         'minInput'=>$this->minInput,
  36.         'maxInput'=>$this->maxInput,
  37.         'minTarget'=>$this->minTarget,
  38.         'maxTarget'=>$this->maxTarget,
  39.         'v'=>$this->v,
  40.         'w'=>$this->w
  41.     );
  42. }
  43.  
  44. function setInputTarget($it){
  45.     $this->input = $this->target = array();
  46.     foreach (func_num_args()==1 ? $it : func_get_args() as $k=>$v){
  47.         if ($k % 2 == 0){
  48.             $this->input[] = $v;
  49.         } else {
  50.             $this->target[] = $v;
  51.         }
  52.     }
  53.     $this->x = $this->input[0];
  54.         $this->x[] = $this->BIAS;
  55.     $this->y = $this->target[0];
  56.     $this->nTrainings = count($this->target);
  57. }
  58.  
  59. function normalize(){
  60.     $this->nInput = $this->nTarget = array();
  61.     $this->maxInput = $this->minInput = array();
  62.     $this->maxTarget = $this->minTarget = array();
  63.     foreach ($this->input as $k=>$v){
  64.         if ($k == 0)
  65.             $this->maxInput = $this->minInput = $v;
  66.         else foreach ($v as $kk=>$vv){
  67.             $this->maxInput[$kk] = max($this->maxInput[$kk], $vv);
  68.             $this->minInput[$kk] = min($this->minInput[$kk], $vv);
  69.         }
  70.     }
  71.     foreach ($this->target as $k=>$v){
  72.         if ($k == 0)
  73.             $this->maxTarget = $this->minTarget = $v;
  74.         else foreach ($v as $kk=>$vv){
  75.             $this->maxTarget[$kk] = max($this->maxTarget[$kk], $vv);
  76.             $this->minTarget[$kk] = min($this->minTarget[$kk], $vv);
  77.         }
  78.     }
  79.     foreach ($this->input as $k=>$v)
  80.         foreach ($v as $kk=>$vv)
  81.             $this->nInput[$k][$kk] = ($vv-$this->minInput[$kk])/($this->maxInput[$kk]-$this->minInput[$kk]);
  82.     foreach ($this->target as $k=>$v)
  83.         foreach ($v as $kk=>$vv)
  84.             $this->nTarget[$k][$kk] = ($vv-$this->minTarget[$kk])/($this->maxTarget[$kk]-$this->minTarget[$kk]) * 0.5 + 0.25;
  85. }
  86.  
  87. function setInput($input){
  88.     $this->x = array();
  89.     foreach ($input as $k=>$v)
  90.         $this->x[] = ($v-$this->minInput[$k])/($this->maxInput[$k]-$this->minInput[$k]);
  91.     $this->x[] = $this->BIAS;
  92. }
  93.  
  94. function getOutput(){
  95.     $ret = $this->y;
  96.     foreach ($ret as $k=>$v)
  97.         $ret[$k] = 2 * ($v - 0.25) * ($this->maxTarget[$k]-$this->minTarget[$k]) + $this->minTarget[$k];
  98.     return $ret;
  99. }
  100.  
  101. function setHidden($n){
  102.     $this->z = array();
  103.     for ($i=0; $i<=$n; $i++)
  104.         $this->z[] = $this->BIAS;
  105. }
  106.  
  107. function randomWeight(){
  108.     $this->v = $this->w = array();
  109.     foreach ($this->x as $i=>$x) foreach ($this->z as $j=>$z){
  110.         if ($j == count($this->z) - 1) continue;
  111.         $this->v[$i][$j] = (rand(0, 1000) - 500) / 500;
  112.     }
  113.     foreach ($this->z as $i=>$z)
  114.         foreach ($this->y as $j=>$y)
  115.             $this->w[$i][$j] = (rand(0, 1000) - 500) / 500;
  116. }
  117.  
  118. function loadNthTrainData($which){
  119.     $this->x = $this->nInput[$which];
  120.         $this->x[] = $this->BIAS;
  121.     $this->t = $this->nTarget[$which];
  122. }
  123.  
  124. function goForward(){
  125.     foreach ($this->z as $j=>$z){
  126.         if ($j == count($this->z) - 1) continue;
  127.         $temp = 0;
  128.         foreach ($this->x as $i=>$x)
  129.             $temp += $x * $this->v[$i][$j];
  130.         $this->z[$j] = 1 / (1 + exp(- $temp));
  131.     }
  132.     foreach ($this->y as $j=>$y){
  133.         $temp = 0;
  134.         foreach ($this->z as $i=>$z)
  135.             $temp += $z * $this->w[$i][$j];
  136.         $this->y[$j] = 1 / (1 + exp(- $temp));
  137.     }
  138. }
  139.  
  140. public $ALPHA = 0.2;
  141. public $BIAS = 0.5;
  142. function goBackward(){
  143.     $this->se = 0;
  144.     $old_w = $this->w;
  145.     foreach ($this->y as $k=>$y){
  146.         $this->se += ($this->t[$k] - $y) * ($this->t[$k] - $y);
  147.         $delta_w[$k] = ($this->t[$k] - $y) * $y * (1 - $y);
  148.         foreach ($this->z as $j=>$z)
  149.             $this->w[$j][$k] += $this->ALPHA * $delta_w[$k] * $z;
  150.     }
  151.     foreach ($this->z as $j=>$z){
  152.         if ($j == count($this->z) - 1) continue;
  153.         $delta_v[$j] = 0;
  154.         foreach ($this->y as $k=>$y)
  155.             $delta_v[$j] += $delta_w[$k] * $old_w[$j][$k];
  156.         $delta_v[$j] = $delta_v[$j] * $z * (1 - $z);
  157.         foreach ($this->x as $i=>$x)
  158.             $this->v[$i][$j] += $this->ALPHA * $delta_v[$j] * $x;
  159.     }
  160. }
  161.  
  162. }}
  163. $bp = new BackProp();
  164. Template::head($db,$file, $hakAkses);
  165.  
  166. ?>
  167. <script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
  168.     <script src="plugins/datepicker/bootstrap-datepicker.js"></script>
  169.     <div class="content-wrapper">
  170.         <section class="content-header">
  171.             <h1 style="float: left;margin-right: 10px !important;">
  172.                 Pelatihan Jaringan Syaraf Tiruan
  173.             </h1>
  174.         </section>
  175.         <section class="content">
  176.             <div class="row">
  177.                 <div class="col-lg-12">
  178.                     <?php showMessageAndErrors($message,$error);?>
  179.                     <div class="nav-tabs-custom">
  180.                         <ul class="nav nav-tabs">
  181.                             <li ><a href="parameter-pengujian-produk.php?produk=<?=$_REQUEST['idProduk'];?>&size=<?=$_REQUEST['idSize'];?>&outlet=<?=$_REQUEST['idOutlet']?>&idPelatihan=<?=$_REQUEST['idPelatihan'];?>">Parameter</a></li>
  182.                             <li class="active"><a href="#ekstraksi-pelatihan" data-toggle="tab">Ekstraksi</a></li>
  183.                             <li><a  href="hasil-pelatihan-pengujian.php?idPelatihan=<?=($_REQUEST['idPelatihan']) ? $_REQUEST['idPelatihan'] : ''?>">Hasil</a></li>
  184.                         </ul>
  185.                         <div class="tab-content">
  186.                             <div class="active tab-pane" id="bobot-pelatihan">
  187.                                 <div class="box box-info">
  188.                                     <div class="box-header with-border">
  189.                                         <h3 class="box-title">Pelatihan Kembali Pengujian dari data Opname berdasarkan Jenis Produk Produk</h3>
  190.                                     </div>
  191.                                    
  192.                                         <div class="box-body">
  193.                                                 <input type="hidden" name="idPel" value="<?=$_REQUEST['idPelatihan'];?>"/>
  194.                                                 <input type="hidden" class="form-control" name="mode" value="<?=($_REQUEST['mode'] == 'edit')? 'edit' : 'insert';?>"/>
  195.                                                 <?php
  196.                                                     foreach ($db->table("SELECT a.idPelatihan, a.x1,a.x2,a.x3,a.y FROM inputpelatihan AS a WHERE a.idPelatihan = '%s'",$_REQUEST['idPelatihan']) as $v){
  197.                                                         $inputTargets[] = array($v->x1, $v->x2, $v->x3);
  198.                                                         $inputTargets[] = array($v->y);
  199.                                                     }
  200.                                                    
  201.                                                     $bp->setInputTarget($inputTargets);
  202.                                                     $bp->normalize();
  203.                                                     $bp->setHidden(7);
  204.                                                     foreach ($db->table("SELECT * FROM bobotpelatihan WHERE jenis='v' AND idPelatihan='%s'",$_REQUEST['idPelatihan']) as $v){
  205.                                                         $bp->v[$v->neuronFrom][$v->neuronTo] = $v->weight;
  206.                                                     }
  207.                                                     foreach ($db->table("SELECT * FROM bobotpelatihan WHERE jenis='w' AND idPelatihan='%s'",$_REQUEST['idPelatihan']) as $w){
  208.                                                         $bp->w[$w->neuronFrom][$w->neuronTo] = $w->weight;
  209.                                                     }
  210.                                                 ?>
  211.                                                 <p>Initialization</p>
  212.                                                 <table class="table table-bordered table-striped dataTable"  border="1">
  213.                                                     <thead>
  214.                                                         <tr>
  215.                                                             <th>Max Epoch</th>
  216.                                                             <th>Hidden Neuron</th>
  217.                                                             <th>Learning Rate</th>
  218.                                                             <th>Ambang MSE</th>
  219.                                                         </tr>
  220.                                                     </thead>
  221.                                                     <tbody>
  222.                                                         <tr>
  223.                                                             <td>
  224.                                                                 100000
  225.                                                             </td>
  226.                                                             <td>
  227.                                                                 7
  228.                                                             </td>
  229.                                                             <td>
  230.                                                                 0.2
  231.                                                             </td>
  232.                                                             <td>
  233.                                                                 0.000000001
  234.                                                             </td>
  235.                                                         </tr>
  236.                                                     </tbody>
  237.                                                 </table>
  238.                                                 <?php
  239.                                                     $MAXEPOCH = 100000;
  240.                                                     $db->exec("DELETE FROM parameterpelatihan WHERE idPelatihan=%d", $_REQUEST['idPelatihan']);
  241.                                                     $db->exec("DELETE FROM msepelatihan WHERE idPelatihan=%d", $_REQUEST['idPelatihan']);
  242.                                                     $db->begin();
  243.                                                     for ($i=0; $i<$MAXEPOCH; $i++){
  244.                                                         $se = 0;
  245.                                                         for ($j=0; $j<$bp->nTrainings; $j++){
  246.                                                             $bp->loadNthTrainData($j);
  247.                                                             $bp->goForward();
  248.                                                             //$db->exec("INSERT INTO parameterpelatihan(idPelatihan,name,value,epoch,forback) VALUES ('%s','%s','%s','%s','%s')",$_REQUEST['idPelatihan'], 'v',json_encode($bp->v),$i, 1);
  249.                                                             //$db->exec("INSERT INTO parameterpelatihan(idPelatihan,name,value,epoch,forback) VALUES ('%s','%s','%s','%s','%s')",$_REQUEST['idPelatihan'], 'w',json_encode($bp->w),$i, 1);
  250.                                                             $bp->goBackward();
  251.                                                             //$db->exec("INSERT INTO parameterpelatihan(idPelatihan,name,value,epoch,forback) VALUES ('%s','%s','%s','%s','%s')",$_REQUEST['idPelatihan'], 'v',json_encode($bp->v),$i, 0);
  252.                                                             //$db->exec("INSERT INTO parameterpelatihan(idPelatihan,name,value,epoch,forback) VALUES ('%s','%s','%s','%s','%s')",$_REQUEST['idPelatihan'], 'w',json_encode($bp->w),$i, 0);
  253.                                                             $se += $bp->se;
  254.                                                             //$db->exec("INSERT INTO parameterpelatihan(idPelatihan,name,value,epoch,forback) VALUES ('%s','%s','%s','%s','%s')",$_REQUEST['idPelatihan'], 'se',json_encode($bp->se),$i, 0);
  255.                                                         }
  256.                                                         $mse = $se/$bp->nTrainings;
  257.                                                         echo "((Epoch $i --> MSE : $mse)) \r\n";
  258.                                                         $db->exec("INSERT INTO msepelatihan(idPelatihan,epoch,MSE) VALUES ('%s','%s','%s')",$_REQUEST['idPelatihan'], $i,$mse);
  259.                                                         if ($mse <= 1e-8) break;
  260.                                                     }
  261.                                                     $db->commit();
  262.                                                     $idmsePel = $db->row("SELECT a.idMsePelatihan FROM msepelatihan AS a WHERE a.idPelatihan = '%s' ORDER BY a.idMsePelatihan DESC LIMIT 1",$_REQUEST['idPelatihan']);
  263.                                                     $mseTerakhir = $db->row("SELECT a.epoch, a.MSE FROM msepelatihan AS a WHERE idMsePelatihan = '%s'",$idmsePel->idMsePelatihan);
  264.                                                     $db->exec("DELETE FROM bobotpelatihan WHERE idPelatihan = '%s'",$_REQUEST['idPelatihan']);
  265.                                                     foreach ($bp->v as $i=>$v){
  266.                                                         foreach ($v as $j=>$vv)
  267.                                                             //$db->exec("UPDATE bobotpelatihan SET neuronFrom = '%s',neuronTo = '%s', weight = '%s', jenis='%s' WHERE idPelatihan = '%s'",$i,$j,$vv,'v',$_REQUEST['idPelatihan']);
  268.                                                             $db->exec("INSERT INTO `bobotpelatihan`(`idPelatihan`, `neuronFrom`, `neuronTo`, `weight`, `jenis`) VALUES ('%s','%d','%d', '%f', '%s')",
  269.                                                                     $_REQUEST['idPelatihan'], $i, $j, $vv,'v');
  270.                                                     }
  271.                                                     foreach ($bp->w as $i=>$w){
  272.                                                         foreach ($w as $j=>$ww)
  273.                                                             //$db->exec("UPDATE bobotpelatihan SET neuronFrom = '%s',neuronTo = '%s', weight = '%s', jenis='%s' WHERE idPelatihan = '%s'",$i,$j,$ww,'w',$_REQUEST['idPelatihan']);
  274.                                                             $db->exec("INSERT INTO `bobotpelatihan`(`idPelatihan`, `neuronFrom`, `neuronTo`, `weight`, `jenis`) VALUES ('%s','%d','%d', '%f', '%s')",
  275.                                                                     $_REQUEST['idPelatihan'], $i, $j, $ww,'w');
  276.                                                     }
  277.                                                     $message = "Sukses ! Berhenti pada epoch ke ".$mseTerakhir->epoch." dengan MSE ".$mseTerakhir->MSE.". Harap close page ini setelah melakukan perhitungan!";
  278.                                                 ?>
  279.                                                 <?php showMessageAndErrors($message,$error);?>
  280.                                                 <div class="box-footer">
  281.                                                     <a href="pengujian.php"class="btn btn-info pull-left">Back</a>
  282.                                                    
  283.                                                 </div>
  284.                                         </div>
  285.                                 </div>
  286.                             </div>
  287.                         </div>
  288.                     </div>
  289.                 </div>
  290.             </div>
  291.         </section>
  292.     </div>
  293. <?php
  294.  
  295. //$bp->setInput(array(74, 180000, 202));
  296. //$bp->goForward();
  297. //echo "SET INPUT (74, 180000, 202) TARGET (47) HASIL = ".json_encode($bp->getOutput());
  298. ?>
  299. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement