Logical XOR function: Difference between revisions

From Artificial Neural Network for PHP
Line 45: Line 45:
try
try
{
{
$network = ANN_Network::loadFromFile('xor.dat');
$objNetwork = ANN_Network::loadFromFile('xor.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 52: Line 52:
}
}


$inputs = array(
$arrInputs = array(
array(0, 0),
array(0, 0),
array(0, 1),
array(0, 1),
Line 59: Line 59:
);
);


$network->setInputs($inputs);
$objNetwork->setInputs($arrInputs);


print_r($network->getOutputs());
print_r($objNetwork->getOutputs());
</source>
</source>

Revision as of 12:42, 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;
}

$arrInputs = array(
	array(0, 0),
	array(0, 1),
	array(1, 0),
	array(1, 1)
);

$arrOutputs = array(
	array(0),
	array(1),
	array(1),
	array(0)
);

$objNetwork ->setInputs($arrInputs);

$objNetwork ->setOutputs($arrOutputs);

$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');
}

$arrInputs = array(
	array(0, 0),
	array(0, 1),
	array(1, 0),
	array(1, 1)
);

$objNetwork->setInputs($arrInputs);

print_r($objNetwork->getOutputs());