Selling Icecreams: Difference between revisions
From Artificial Neural Network for PHP
Line 7: | Line 7: | ||
try |
try |
||
{ |
{ |
||
$ |
$objNetwork = ANN_Network::loadFromFile('icecreams.dat'); |
||
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
Line 13: | Line 13: | ||
print 'Creating a new one...'; |
print 'Creating a new one...'; |
||
$ |
$objNetwork = new ANN_Network(2, 8, 1); |
||
$ |
$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius |
||
$ |
$objTemperature->saveToFile('input_temperature.dat'); |
||
unset($ |
unset($objTemperature); |
||
$ |
$objHumidity = new ANN_InputValue(0, 100); // Humidity percentage |
||
$ |
$objHumidity->saveToFile('input_humidity.dat'); |
||
unset($ |
unset($objHumidity); |
||
$ |
$objIcecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams |
||
$ |
$objIcecream->saveToFile('output_quantity.dat'); |
||
unset($ |
unset($objIcecream); |
||
} |
} |
||
try |
try |
||
{ |
{ |
||
$ |
$objTemperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius |
||
$ |
$objHumidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage |
||
$ |
$objIcecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams |
||
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
Line 47: | Line 47: | ||
} |
} |
||
$ |
$arrInputs = array( |
||
array($objTemperature->getInputValue(20), $objHumidity->getInputValue(10)), |
|||
array($objTemperature->getInputValue(30), $objHumidity->getInputValue(40)), |
|||
array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)), |
|||
array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20)) |
|||
); |
); |
||
$ |
$arrOutputs = array( |
||
array($objIcecream->getOutputValue(20)), |
|||
array($objIcecream->getOutputValue(90)), |
|||
array($objIcecream->getOutputValue(70)), |
|||
array($objIcecream->getOutputValue(75)) |
|||
); |
); |
||
$ |
$objNetwork->setInputs($arrInputs); |
||
$ |
$objNetwork->setOutputs($arrOutputs); |
||
$ |
$objNetwork->train(); |
||
$ |
$objNetwork->saveToFile('icecreams.dat'); |
||
</source> |
</source> |
||
Revision as of 12:47, 18 December 2008
Training
require_once 'ANN/ANN_Loader.php';
try
{
$objNetwork = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Creating a new one...';
$objNetwork = new ANN_Network(2, 8, 1);
$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius
$objTemperature->saveToFile('input_temperature.dat');
unset($objTemperature);
$objHumidity = new ANN_InputValue(0, 100); // Humidity percentage
$objHumidity->saveToFile('input_humidity.dat');
unset($objHumidity);
$objIcecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams
$objIcecream->saveToFile('output_quantity.dat');
unset($objIcecream);
}
try
{
$objTemperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$objHumidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$objIcecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
print 'Error loading value objects';
}
$arrInputs = array(
array($objTemperature->getInputValue(20), $objHumidity->getInputValue(10)),
array($objTemperature->getInputValue(30), $objHumidity->getInputValue(40)),
array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)),
array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20))
);
$arrOutputs = array(
array($objIcecream->getOutputValue(20)),
array($objIcecream->getOutputValue(90)),
array($objIcecream->getOutputValue(70)),
array($objIcecream->getOutputValue(75))
);
$objNetwork->setInputs($arrInputs);
$objNetwork->setOutputs($arrOutputs);
$objNetwork->train();
$objNetwork->saveToFile('icecreams.dat');
Using trained network
require_once 'ANN/ANN_Loader.php';
try
{
$network = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Network not found.';
}
try
{
$temperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$humidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$icecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
print 'Error loading value objects';
}
$inputs = array(
array($temperature->getInputValue(20), $humidity->getInputValue(10)),
array($temperature->getInputValue(30), $humidity->getInputValue(40)),
array($temperature->getInputValue(32), $humidity->getInputValue(30)),
array($temperature->getInputValue(33), $humidity->getInputValue(20))
);
$network->setInputs($inputs);
$outputs = $network->getOutputs();
foreach($outputs as $output)
print $icecream->getRealOutputValue($output). '<br />';