Logical XOR function: Difference between revisions
From Artificial Neural Network for PHP
| Line 55: | Line 55: | ||
<source lang="php"> |
<source lang="php"> |
||
<?php |
|||
require_once 'ANN/Loader.php'; |
require_once 'ANN/Loader.php'; |
||
| Line 62: | Line 63: | ||
try |
try |
||
{ |
{ |
||
$objNetwork = Network::loadFromFile('xor.dat'); |
$objNetwork = Network::loadFromFile('xor.dat'); |
||
} |
} |
||
catch(Exception $e) |
catch (Exception $e) |
||
{ |
{ |
||
die('Network not found'); |
die('Network not found'); |
||
} |
} |
||
try |
try |
||
{ |
{ |
||
$objValues = Values::loadFromFile('values_xor.dat'); |
$objValues = Values::loadFromFile('values_xor.dat'); |
||
} |
} |
||
catch(Exception $e) |
catch (Exception $e) |
||
{ |
{ |
||
die('Loading of values failed'); |
die('Loading of values failed'); |
||
} |
} |
||
$objValues->input(0, 1) |
$objValues->input(0, 1) |
||
-> // input values appending the loaded ones |
|||
input(1, 1) |
|||
->input(1, 0) |
|||
->input(0, 0) |
|||
->input(0, 1) |
|||
->input(1, 1); |
|||
$objNetwork->setValues($objValues); |
$objNetwork->setValues($objValues); |
||
Revision as of 12:04, 4 October 2025
FAQ
For information about dat-files have a view to the FAQ page.
Training
<?php
require_once '../ANN/Loader.php';
use ANN\Network;
use ANN\Values;
try
{
$objNetwork = Network::loadFromFile('xor.dat');
} catch (Exception $e)
{
$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');
}
try
{
$objValues = Values::loadFromFile('values_xor.dat');
}
catch (Exception $e)
{
die('Loading of values failed');
}
$objNetwork->setValues($objValues);
$boolTrained = $objNetwork->train();
print ($boolTrained) ? 'Network trained' : 'Network not trained completely. Please re-run the script';
$objNetwork->saveToFile('xor.dat');
Using trained network
<?php
require_once 'ANN/Loader.php';
use ANN\Network;
use ANN\Values;
try
{
$objNetwork = Network::loadFromFile('xor.dat');
}
catch (Exception $e)
{
die('Network not found');
}
try
{
$objValues = Values::loadFromFile('values_xor.dat');
}
catch (Exception $e)
{
die('Loading of values failed');
}
$objValues->input(0, 1)
-> // input values appending the loaded ones
input(1, 1)
->input(1, 0)
->input(0, 0)
->input(0, 1)
->input(1, 1);
$objNetwork->setValues($objValues);
print_r($objNetwork->getOutputs());