Difference between revisions of "Logical XOR function"
From Artificial Neural Network for PHP
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | == FAQ == |
||
+ | |||
+ | For information about dat-files have a view to the [[FAQ]] page. |
||
+ | |||
== Training == |
== Training == |
||
<source lang="php"> |
<source lang="php"> |
||
+ | |||
− | require_once 'ANN/ |
+ | require_once 'ANN/Loader.php'; |
+ | |||
+ | use ANN\Network; |
||
+ | use ANN\Values; |
||
try |
try |
||
{ |
{ |
||
− | $objNetwork = |
+ | $objNetwork = Network::loadFromFile('xor.dat'); |
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
Line 12: | Line 20: | ||
print 'Creating a new one...'; |
print 'Creating a new one...'; |
||
− | $objNetwork = new |
+ | $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 |
||
− | $arrInputs = array( |
||
+ | { |
||
− | array(0, 0), |
||
+ | $objValues = Values::loadFromFile('values_xor.dat'); |
||
− | array(0, 1), |
||
+ | } |
||
− | array(1, 0), |
||
+ | catch(Exception $e) |
||
− | array(1, 1) |
||
+ | { |
||
− | ); |
||
+ | die('Loading of values failed'); |
||
+ | } |
||
+ | $objNetwork->setValues($objValues); // to be called as of version 2.0.6 |
||
− | $arrOutputs = array( |
||
− | array(0), |
||
− | array(1), |
||
− | array(1), |
||
− | array(0) |
||
− | ); |
||
+ | $boolTrained = $objNetwork->train(); |
||
⚫ | |||
+ | print ($boolTrained) |
||
− | $objNetwork ->setOutputs($arrOutputs); |
||
+ | ? 'Network trained' |
||
+ | : 'Network not trained completely. Please re-run the script'; |
||
− | $objNetwork |
+ | $objNetwork->saveToFile('xor.dat'); |
− | $objNetwork |
+ | $objNetwork->printNetwork(); |
</source> |
</source> |
||
Line 41: | Line 60: | ||
<source lang="php"> |
<source lang="php"> |
||
− | require_once 'ANN/ |
+ | require_once 'ANN/Loader.php'; |
+ | |||
+ | use ANN\Network; |
||
+ | use ANN\Values; |
||
try |
try |
||
{ |
{ |
||
− | $ |
+ | $objNetwork = Network::loadFromFile('xor.dat'); |
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
Line 52: | Line 74: | ||
} |
} |
||
+ | try |
||
− | $inputs = array( |
||
+ | { |
||
− | array(0, 0), |
||
+ | $objValues = Values::loadFromFile('values_xor.dat'); |
||
− | array(0, 1), |
||
+ | } |
||
− | array(1, 0), |
||
+ | catch(Exception $e) |
||
− | array(1, 1) |
||
+ | { |
||
− | ); |
||
+ | 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); |
||
⚫ | |||
− | $network->setInputs($inputs); |
||
− | print_r($ |
+ | print_r($objNetwork->getOutputs()); |
</source> |
</source> |
Latest revision as of 13:29, 1 June 2011
FAQ
For information about dat-files have a view to the FAQ page.
Training
require_once 'ANN/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();
Using trained network
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());