Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Perceptron {
- protected $learnRate;
- protected $synapseWeight = array();
- protected $biaWeight;
- public static function factory($learnRate = 0.6) {
- return new static($learnRate);
- }
- public function __construct($learnRate = 0.6) {
- $this->learnRate = $learnRate;
- }
- protected function learnFunction($oldWeight, $errorValue, $value) {
- return $oldWeight + ($errorValue * $value * $this->learnRate);
- }
- protected function setSynapseWeight($synapse, $weight) {
- $this->synapseWeight[$synapse] = $weight;
- }
- protected function synapseWeight($synapse, $inicialize = 0) {
- if (!isset($this->synapseWeight[$synapse]))
- $this->synapseWeight[$synapse] = $inicialize;
- return $this->synapseWeight[$synapse];
- }
- public function process(Array $values) {
- $sum = 1 * $this->biaWeight;
- foreach ($values as $key => $value)
- $sum += $this->synapseWeight($key) * $value;
- return $sum > 0.5 ? 1 : 0;
- }
- public function forceLearn(Array $values, $learn) {
- while ($this->learn($values, $learn));
- return $this;
- }
- public function learn(Array $values, $learn) {
- if (($ret = $this->process($values)) == $learn)
- return false;
- $errorValue = $learn - $ret;
- $this->biaWeight = $this->
- learnFunction($this->biaWeight, $errorValue, 1);
- foreach ($values as $key => $value)
- $this->setSynapseWeight($key,
- $this->learnFunction($this->synapseWeight($key),
- $errorValue, $value));
- return true;
- }
- }
Add Comment
Please, Sign In to add comment