Difference between revisions of "Selling Icecreams"
From Artificial Neural Network for PHP
Line 16: | Line 16: | ||
$objNetwork = new ANN_Network(2, 8, 1); |
$objNetwork = new ANN_Network(2, 8, 1); |
||
+ | |||
$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius |
$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius |
||
Line 21: | Line 22: | ||
$objTemperature->saveToFile('input_temperature.dat'); |
$objTemperature->saveToFile('input_temperature.dat'); |
||
⚫ | |||
$objHumidity = new ANN_InputValue(0, 100); // Humidity percentage |
$objHumidity = new ANN_InputValue(0, 100); // Humidity percentage |
||
Line 27: | Line 27: | ||
$objHumidity->saveToFile('input_humidity.dat'); |
$objHumidity->saveToFile('input_humidity.dat'); |
||
⚫ | |||
$objIcecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams |
$objIcecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams |
||
Line 33: | Line 32: | ||
$objIcecream->saveToFile('output_quantity.dat'); |
$objIcecream->saveToFile('output_quantity.dat'); |
||
+ | |||
+ | $objValues = new ANN_Values; |
||
+ | |||
+ | $objValues->train() |
||
+ | ->input( |
||
+ | $objTemperature->getInputValue(20), |
||
⚫ | |||
+ | ) |
||
+ | ->output( |
||
⚫ | |||
+ | ) |
||
+ | ->input( |
||
+ | $objTemperature->getInputValue(30), |
||
⚫ | |||
+ | ) |
||
+ | ->output( |
||
⚫ | |||
+ | ) |
||
+ | ->input( |
||
+ | $objTemperature->getInputValue(32), |
||
⚫ | |||
+ | ) |
||
+ | ->output( |
||
⚫ | |||
+ | ) |
||
+ | ->input( |
||
⚫ | |||
+ | $objHumidity->getInputValue(20) |
||
+ | ) |
||
+ | ->output( |
||
⚫ | |||
+ | ); |
||
+ | |||
+ | $objValues->saveToFile('values_icecreams.dat'); |
||
+ | |||
+ | unset($objValues); |
||
⚫ | |||
⚫ | |||
unset($objIcecream); |
unset($objIcecream); |
||
} |
} |
||
Line 49: | Line 86: | ||
} |
} |
||
+ | try |
||
− | $arrInputs = array( |
||
+ | { |
||
⚫ | |||
+ | $objValues = ANN_Values::loadFromFile('values_icecreams.dat'); |
||
⚫ | |||
+ | } |
||
⚫ | |||
+ | catch(Exception $e) |
||
⚫ | |||
+ | { |
||
− | ); |
||
+ | die('Loading of values failed'); |
||
− | |||
+ | } |
||
− | $arrOutputs = array( |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
− | ); |
||
− | |||
− | $objNetwork->setInputs($arrInputs); |
||
− | $objNetwork-> |
+ | $objNetwork->setValues($objValues); // to be called as of version 2.0.6 |
$objNetwork->train(); |
$objNetwork->train(); |
Revision as of 08:25, 19 December 2008
Training
THIS EXAMPLE IS NOT UP TO DATE. PLEASE HAVE A LOOK TO XOR EXAMPLE!!!
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');
$objHumidity = new ANN_InputValue(0, 100); // Humidity percentage
$objHumidity->saveToFile('input_humidity.dat');
$objIcecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams
$objIcecream->saveToFile('output_quantity.dat');
$objValues = new ANN_Values;
$objValues->train()
->input(
$objTemperature->getInputValue(20),
$objHumidity->getInputValue(10)
)
->output(
$objIcecream->getOutputValue(20)
)
->input(
$objTemperature->getInputValue(30),
$objHumidity->getInputValue(40)
)
->output(
$objIcecream->getOutputValue(90)
)
->input(
$objTemperature->getInputValue(32),
$objHumidity->getInputValue(30)
)
->output(
$objIcecream->getOutputValue(70)
)
->input(
$objTemperature->getInputValue(33),
$objHumidity->getInputValue(20)
)
->output(
$objIcecream->getOutputValue(75)
);
$objValues->saveToFile('values_icecreams.dat');
unset($objValues);
unset($objTemperature);
unset($objHumidity);
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';
}
try
{
$objValues = ANN_Values::loadFromFile('values_icecreams.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('icecreams.dat');
Using trained network
require_once 'ANN/ANN_Loader.php';
try
{
$objNetwork = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Network not found.';
}
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))
);
$objNetwork->setInputs($arrInputs);
$arrOutputs = $objNetwork->getOutputs();
foreach($arrOutputs as $floatOutput)
print $objIcecream->getRealOutputValue($floatOutput). '<br />';