Difference between revisions of "Selling Icecreams"
From Artificial Neural Network for PHP
(12 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. |
||
− | THIS EXAMPLE IS NOT UP TO DATE. PLEASE HAVE A LOOK TO XOR EXAMPLE!!! |
||
+ | |||
+ | == Training == |
||
<source lang="php"> |
<source lang="php"> |
||
− | require_once 'ANN/ |
+ | require_once 'ANN/Loader.php'; |
+ | |||
+ | use ANN\Network; |
||
+ | use ANN\InputValue; |
||
+ | use ANN\OutputValue; |
||
+ | use ANN\Values; |
||
try |
try |
||
{ |
{ |
||
− | $objNetwork = |
+ | $objNetwork = Network::loadFromFile('icecreams.dat'); |
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
Line 15: | Line 22: | ||
print 'Creating a new one...'; |
print 'Creating a new one...'; |
||
− | $objNetwork = new |
+ | $objNetwork = new Network(2, 5, 1); |
+ | |||
− | $objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius |
||
+ | $objTemperature = new InputValue(-15, 50); // Temperature in Celsius |
||
$objTemperature->saveToFile('input_temperature.dat'); |
$objTemperature->saveToFile('input_temperature.dat'); |
||
− | unset($objTemperature); |
||
− | $objHumidity = new |
+ | $objHumidity = new InputValue(0, 100); // Humidity percentage |
$objHumidity->saveToFile('input_humidity.dat'); |
$objHumidity->saveToFile('input_humidity.dat'); |
||
− | unset($objHumidity); |
||
− | $objIcecream = new |
+ | $objIcecream = new OutputValue(0, 300); // Quantity of sold ice-creams |
$objIcecream->saveToFile('output_quantity.dat'); |
$objIcecream->saveToFile('output_quantity.dat'); |
||
+ | |||
+ | $objValues = new 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); |
unset($objIcecream); |
||
} |
} |
||
Line 38: | Line 82: | ||
try |
try |
||
{ |
{ |
||
− | $objTemperature = |
+ | $objTemperature = InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius |
− | $objHumidity = |
+ | $objHumidity = InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage |
− | $objIcecream = |
+ | $objIcecream = OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams |
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
{ |
{ |
||
− | + | die('Error loading value objects'); |
|
} |
} |
||
+ | try |
||
− | $arrInputs = array( |
||
+ | { |
||
− | array($objTemperature->getInputValue(20), $objHumidity->getInputValue(10)), |
||
+ | $objValues = Values::loadFromFile('values_icecreams.dat'); |
||
− | array($objTemperature->getInputValue(30), $objHumidity->getInputValue(40)), |
||
+ | } |
||
− | array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)), |
||
+ | catch(Exception $e) |
||
− | array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20)) |
||
+ | { |
||
− | ); |
||
+ | die('Loading of values failed'); |
||
+ | } |
||
+ | $objNetwork->setValues($objValues); // to be called as of version 2.0.6 |
||
− | $arrOutputs = array( |
||
− | array($objIcecream->getOutputValue(20)), |
||
− | array($objIcecream->getOutputValue(90)), |
||
− | array($objIcecream->getOutputValue(70)), |
||
− | array($objIcecream->getOutputValue(75)) |
||
− | ); |
||
− | $objNetwork-> |
+ | $boolTrained = $objNetwork->train(); |
+ | print ($boolTrained) |
||
− | $objNetwork->setOutputs($arrOutputs); |
||
+ | ? 'Network trained' |
||
− | |||
+ | : 'Network not trained completely. Please re-run the script'; |
||
− | $objNetwork->train(); |
||
$objNetwork->saveToFile('icecreams.dat'); |
$objNetwork->saveToFile('icecreams.dat'); |
||
+ | |||
+ | $objNetwork->printNetwork(); |
||
</source> |
</source> |
||
Line 76: | Line 119: | ||
<source lang="php"> |
<source lang="php"> |
||
− | require_once 'ANN/ |
+ | require_once 'ANN/Loader.php'; |
+ | |||
+ | use ANN\Network; |
||
+ | use ANN\InputValue; |
||
+ | use ANN\OutputValue; |
||
+ | use ANN\Values; |
||
try |
try |
||
{ |
{ |
||
− | $objNetwork = |
+ | $objNetwork = Network::loadFromFile('icecreams.dat'); |
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
{ |
{ |
||
− | + | die('Network not found'); |
|
} |
} |
||
try |
try |
||
{ |
{ |
||
− | $objTemperature = |
+ | $objTemperature = InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius |
− | $objHumidity = |
+ | $objHumidity = InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage |
− | $objIcecream = |
+ | $objIcecream = OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams |
+ | } |
||
+ | catch(Exception $e) |
||
+ | { |
||
+ | die('Error loading value objects'); |
||
+ | } |
||
+ | |||
+ | try |
||
+ | { |
||
+ | $objValues = Values::loadFromFile('values_icecreams.dat'); |
||
} |
} |
||
catch(Exception $e) |
catch(Exception $e) |
||
{ |
{ |
||
+ | die('Loading of values failed'); |
||
− | print 'Error loading value objects'; |
||
} |
} |
||
+ | $objValues->input( // input values appending the loaded ones |
||
− | $arrInputs = array( |
||
− | + | $objTemperature->getInputValue(17), |
|
− | + | $objHumidity->getInputValue(12) |
|
+ | ) |
||
− | array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)), |
||
+ | ->input( |
||
− | array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20)) |
||
+ | $objTemperature->getInputValue(31), |
||
− | ); |
||
+ | $objHumidity->getInputValue(42) |
||
+ | ) |
||
+ | ->input( |
||
+ | $objTemperature->getInputValue(31), |
||
+ | $objHumidity->getInputValue(34) |
||
+ | ) |
||
+ | ->input( |
||
+ | $objTemperature->getInputValue(34), |
||
+ | $objHumidity->getInputValue(21) |
||
+ | ); |
||
− | $objNetwork-> |
+ | $objNetwork->setValues($objValues); |
$arrOutputs = $objNetwork->getOutputs(); |
$arrOutputs = $objNetwork->getOutputs(); |
||
− | foreach($arrOutputs as $ |
+ | foreach($arrOutputs as $arrOutput) |
+ | foreach($arrOutput as $floatOutput) |
||
− | print $objIcecream->getRealOutputValue($floatOutput). '<br />'; |
||
+ | print $objIcecream->getRealOutputValue($floatOutput). '<br />'; |
||
</source> |
</source> |
Latest revision as of 13:33, 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\InputValue;
use ANN\OutputValue;
use ANN\Values;
try
{
$objNetwork = Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Creating a new one...';
$objNetwork = new Network(2, 5, 1);
$objTemperature = new InputValue(-15, 50); // Temperature in Celsius
$objTemperature->saveToFile('input_temperature.dat');
$objHumidity = new InputValue(0, 100); // Humidity percentage
$objHumidity->saveToFile('input_humidity.dat');
$objIcecream = new OutputValue(0, 300); // Quantity of sold ice-creams
$objIcecream->saveToFile('output_quantity.dat');
$objValues = new 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 = InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$objHumidity = InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$objIcecream = OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
die('Error loading value objects');
}
try
{
$objValues = 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
$boolTrained = $objNetwork->train();
print ($boolTrained)
? 'Network trained'
: 'Network not trained completely. Please re-run the script';
$objNetwork->saveToFile('icecreams.dat');
$objNetwork->printNetwork();
Using trained network
require_once 'ANN/Loader.php';
use ANN\Network;
use ANN\InputValue;
use ANN\OutputValue;
use ANN\Values;
try
{
$objNetwork = Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
die('Network not found');
}
try
{
$objTemperature = InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$objHumidity = InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$objIcecream = OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
die('Error loading value objects');
}
try
{
$objValues = Values::loadFromFile('values_icecreams.dat');
}
catch(Exception $e)
{
die('Loading of values failed');
}
$objValues->input( // input values appending the loaded ones
$objTemperature->getInputValue(17),
$objHumidity->getInputValue(12)
)
->input(
$objTemperature->getInputValue(31),
$objHumidity->getInputValue(42)
)
->input(
$objTemperature->getInputValue(31),
$objHumidity->getInputValue(34)
)
->input(
$objTemperature->getInputValue(34),
$objHumidity->getInputValue(21)
);
$objNetwork->setValues($objValues);
$arrOutputs = $objNetwork->getOutputs();
foreach($arrOutputs as $arrOutput)
foreach($arrOutput as $floatOutput)
print $objIcecream->getRealOutputValue($floatOutput). '<br />';