Created
November 28, 2022 15:42
-
-
Save martinobettucci/cac23475c8e9b9a5f9277e8b879824bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MatrixMath { | |
public static function dotVectors($vector1, $vector2) | |
{ | |
$total = 0; | |
$dim = count($vector1); | |
for ($i = 0; $i < $dim; $i++) { | |
$total += $vector1[$i] * $vector2[$i]; | |
} | |
return $total; | |
} | |
} | |
class PerceptronBase { | |
protected $bias; | |
protected $learningRate; | |
protected $errorSum = 0; | |
protected $iterationError = 0; | |
public function getBias() | |
{ | |
return $this->bias; | |
} | |
public function setBias($bias) | |
{ | |
if (!is_numeric($bias)) { | |
throw new \InvalidArgumentException(); | |
} | |
$this->bias = $bias; | |
} | |
public function getLearningRate() | |
{ | |
return $this->learningRate; | |
} | |
public function setLearningRate($learningRate) | |
{ | |
$this->learningRate = $learningRate; | |
} | |
public function getIterationError() | |
{ | |
return $this->iterationError; | |
} | |
} | |
class Perceptron extends PerceptronBase | |
{ | |
protected $tailleDesFeatures; | |
protected $listeDesCoeff; | |
protected $activation = null; | |
//, $bias = 0.0)//, $learningRate = .5) | |
public function __construct($listeDesFeatures) | |
{ | |
$this->tailleDesFeatures = $listeDesFeatures; | |
for ($i = 0; $i < $this->tailleDesFeatures; $i++) { | |
$this->listeDesCoeff[$i] = rand()/getrandmax() * 2 - 1; | |
} | |
} | |
//$this->bias = $bias; | |
//$this->learningRate = $learningRate; | |
public function test($listeDesFeatures) | |
{ | |
$regression = MatrixMath::dotVectors($this->listeDesCoeff, $listeDesFeatures);// + $this->bias; | |
// Activation function | |
$this->activation = $regression > 0 ? 1 : 0; | |
return $this->activation; | |
} | |
public function train($listeDesFeature, $bonneResponseAttendue) | |
{ | |
$this->iterations += 1; | |
$regression = $this->test($listeDesFeature); | |
for ($i = 0; $i < $this->tailleDesFeatures; $i++) { | |
$this->listeDesCoeff[$i] = | |
$this->listeDesCoeff[$i] + ((int) $bonneResponseAttendue - (int) $regression) * $listeDesFeature[$i] /* * $this->learningRate */; | |
} | |
} | |
//$this->bias = $this->bias + ((int) $outcome - (int) $output); | |
//$this->errorSum += (int) $outcome - (int) $output; | |
//$this->iterationError = 1 / $this->iterations * $this->errorSum; | |
protected $iterations = 0; | |
public function getActivation() | |
{ | |
if(is_null($this->activation)) | |
{ | |
throw new \RuntimeException(); | |
} | |
else | |
{ | |
return $this->activation; | |
} | |
} | |
public function getListeDesCoeff() | |
{ | |
return $this->listeDesCoeff; | |
} | |
public function setListeDesCoeff($listeDesCoeff) | |
{ | |
$this->listeDesCoeff = $listeDesCoeff; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment