public function train(Perceptron $p) { for ($i = -10000; $i < 10000; $i++) { $x = rand(-100000, 100000); $y = rand(-100000, 100000); $answer = 1; if ($y < $this->f($x)) { $answer = -1; } $p->train($answer, $x, $y, 1); } }
<?php include "../Perceptron.php"; /** * Example */ $filename = 'w1.json'; /** * Наш перцептрон будет говорить что дали на вход, квадрат или прямую. * Следует учесть, что в этом примере перцептрона спрашивают о том, чего небыло в учении. */ $neural = new Perceptron(64); // матрица будет 8х8, размерность 64. if (!isset($filename)) { /** * Учим квадраты */ $v1 = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1); $neural->teach($v1, 1); $v1 = array(0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1); $neural->teach($v1, 1); $v1 = array(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1); $neural->teach($v1, 1); $v1 = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1); $neural->teach($v1, 1); $v1 = array(0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0); $neural->teach($v1, 1); $v1 = array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1); $neural->teach($v1, 1); /** * Теперь учим прямые.
function learn($letters, Perceptron $perceptron) { $learnFor = 11; $iterations = 100; display($perceptron->weights); for ($i = 0; $i <= $iterations; $i++) { foreach ($letters as $letterNumber => $fonts) { $isCorrect = $letterNumber == $learnFor; foreach ($fonts as $letter) { //if ($i % 250 == 0) // display($letter, '#999'); $perceptronResponse = $perceptron->calculate($letter); if ($perceptronResponse == $isCorrect) { //if ($i % 250 == 0) // display($perceptron->weights, 'green'); continue; } //if ($i % 250 == 0) //display($perceptron->weights, 'red'); $perceptron->correctWeights($letter, (int) $isCorrect, (int) $perceptronResponse); } } //if ($i % 250 == 0) // display($perceptron->weights); } display($perceptron->weights); }
<?php include "../../classes/perceptron.class.php"; include '../../classes/tests.class.php'; //Перцептрон будет говорить какую фигуру дали на вход. Этих фигур не было в обучении $nn = new Perceptron(64); // матрица будет 8х8, размерность 64. $tests = new Tests('geometry_2'); $train = $tests->train_data; $test = $tests->test_data; //print_r($test); for ($i = 0; $i < count($train['input']); $i++) { $nn->learn($train['input'][$i], $train['output'][$i]); } for ($i = 0; $i <= count($test['input']) - 1; $i++) { $answer = $nn->activation($test['input'][$i]); echo $i . ": <b>" . $answer . "</b> (" . $test['output'][$i] . ")<br>"; }
public function rank(Message $message) { $message->setRank($this->perceptron->process(array(self::TYPE_FOLLOWERS => $message->getFollowers(), self::TYPE_FRIENDS => $message->getFriends(), self::TYPE_TIME => $message->getTime() - time()))); return $message; }