Example #1
0
File: xor.php Project: 0-php/AI
<?php

require_once '../Loader.php';
use ANN\Network;
use ANN\Values;
try {
    $objNetwork = Network::loadFromFile('xor.dat');
} catch (Exception $e) {
    print 'Creating a new one...';
    $objNetwork = new Network();
    $objValues = new Values();
    $objValues->train()->input(0, 0)->output(0)->input(0, 1)->output(1)->input(1, 0)->output(1)->input(1, 1)->output(0);
    $objValues->saveToFile('values_xor.dat');
    unset($objValues);
}
try {
    $objValues = Values::loadFromFile('values_xor.dat');
} catch (Exception $e) {
    die('Loading of values failed');
}
$objNetwork->setValues($objValues);
// to be called as of version 2.0.6
$boolTrained = $objNetwork->train();
print $boolTrained ? 'Network trained' : 'Network not trained completely. Please re-run the script';
$objNetwork->saveToFile('xor.dat');
$objNetwork->printNetwork();
Example #2
0
<?php

require_once '../Loader.php';
use ANN\Network;
use ANN\Values;
try {
    $objNetwork = Network::loadFromFile('shapes.dat');
} catch (Exception $e) {
    print 'Creating a new one...';
    $objNetwork = new Network();
    $objValues = new Values();
    $shapes = (array) json_decode(file_get_contents('../../data/shapes/lines_and_squares.json'));
    for ($i = 0; $i < count($shapes['output']); $i++) {
        $objValues->train()->input($shapes['input'][$i])->output($shapes['output'][$i]);
    }
    $objValues->saveToFile('values_shapes.dat');
    unset($objValues);
}
try {
    $objValues = Values::loadFromFile('values_shapes.dat');
} catch (Exception $e) {
    die('Loading of values failed');
}
$objNetwork->setValues($objValues);
// to be called as of version 2.0.6
$boolTrained = $objNetwork->train();
print $boolTrained ? 'Network trained' : 'Network not trained completely. Please re-run the script';
$objNetwork->saveToFile('shapes.dat');
$objNetwork->printNetwork();