Read 250 times | Created 2014-05-16 00:03:34 | Updated 2014-05-16 00:03:34 | | |

 

<?php
/*********************************
FILENAME  : delta_rule.php
CREATE BY  : cahya dsn
PURPOSE   : JST Delta Rule
CREATE DATE : 2014-05-16
***********************************/
class deltaRule{
    public $alfa=0.2;
    public $teta=0.5;
    public $output=array();
    public $eror=array();
    public $kondisi;
    public $bobot=array(0.1,0.3,0.2,0.6);
    public $epoh;
    public $y;
    public $yOut;
 
    public function learn($inputan,$target){
        $this->kondisi=false;
        $this->epoh=0;
        do{
            $n=count($inputan);
            for($a=0; $a<$n; $a++){
                //pencarian nilai output
                $this->yOut=0;
                $m=count($inputan[0]);
                for($b=0; $b<$m; $b++){
                  $this->yOut+=$inputan[$a][$b]*$this->bobot[$b];
                }
 
                //pergantian bobot jika antara target dan output belum sesuai
                for($b=0; $b<$m; $b++){
                    //pengaktifan biner
                    if($this->yOut<=$this->teta){
                      $this->output[$a]=0;
                    }else{
                      $this->output[$a]=1;
                    }
                    $this->eror[$a]=$target[$a]-$this->output[$a];
                    $this->bobot[$b]+=$this->alfa*$inputan[$a][$b]*$this->eror[$a];
                }
            }
 
            //penetuan kondisi
            $x=0;
            $n=count($target);
            for($b=0; $b<$n; $b++){
                if($this->output[$b]!=$target[$b]){
                  $x++;
                }
            }
            echo "iterasi ke ".(++$this->epoh)." ";
            print_r($this->output);
            echo " ditemukan ".($n-$x)." yang cocok<br>";
            if($x==0) $this->kondisi=true;
            //jika sistem tidak bisa melakukan pembelajaran
            if($this->epoh==10000){
                echo "I Dont Know This...n";
                $this->kondisi=true;
            }
        }
        while($this->kondisi==false);
    }
 
    public function test($inputan){
        $y=0;
        $i=0;
        foreach($inputan as $inp){
          $y+=$inp*$this->bobot[$i++];
          echo $inp."~";
        }
        if($y<=$this->teta){
            $y=0;
        }else{
            $y=1;
        }
        echo "  ->  $y<br/>";
    }
}

$delta = new deltaRule();
$data = array(array(1,1),array(1,0),array(0,1),array(0,0));
$target = array(1,1,1,0);
$delta->learn($data, $target);


$delta->test(array(0, 1));
$delta->test(array(1, 0));
$delta->test(array(0, 0));
$delta->test(array(1, 1));

$data2=array(array(1,1,0),array(1,0,1),array(1,0,0),array(1,1,1),array(0,1,0),array(0,0,0));
$target2=array(1,1,0,1,0,0);
$delta2 = new deltaRule();
$delta2->learn($data2, $target2);

$delta2->test(array(0,1,0));
$delta2->test(array(1,0,0));
$delta2->test(array(1,0,1));