Logical XOR function: Difference between revisions

From Artificial Neural Network for PHP
Line 57: Line 57:
}
}


try
$arrInputs = array(
{
array(0, 0),
$objValues = ANN_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
$objNetwork->setInputs($arrInputs);
->input(1, 1)
->input(1, 0)
->input(0, 0)
->input(0, 1)
->input(1, 1);

$objNetwork->setValues($objValues);


print_r($objNetwork->getOutputs());
print_r($objNetwork->getOutputs());

Revision as of 17:54, 18 December 2008

Training

require_once 'ANN/ANN_Loader.php';

try
{
  $objNetwork = ANN_Network::loadFromFile('xor.dat');
}
catch(Exception $e)
{
  print 'Creating a new one...';
	
  $objNetwork = new ANN_Network;

  $objValues = new ANN_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 = ANN_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

$objNetwork->train();

$objNetwork->saveToFile('xor.dat');

Using trained network

require_once 'ANN/ANN_Loader.php';

try
{
  $objNetwork = ANN_Network::loadFromFile('xor.dat');
}
catch(Exception $e)
{
  die('Network not found');
}

try
{
  $objValues = ANN_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());