Difference between revisions of "Selling Icecreams"
From Artificial Neural Network for PHP
Line 14: | Line 14: | ||
$network = new ANN_Network(2,8,1); |
$network = new ANN_Network(2,8,1); |
||
+ | |||
⚫ | |||
+ | |||
+ | $temperature->saveToFile('input_temperature.dat'); |
||
+ | |||
+ | unset($temperature); |
||
+ | |||
+ | $humidity = new ANN_InputValue(0, 100); // Humidity percentage |
||
+ | |||
+ | $humidity->saveToFile('input_humidity.dat'); |
||
+ | |||
+ | unset($humidity); |
||
+ | |||
+ | $icecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams |
||
+ | |||
+ | $icecream->saveToFile('output_quantity.dat'); |
||
+ | |||
+ | unset($icecream); |
||
} |
} |
||
+ | try |
||
⚫ | |||
+ | { |
||
+ | $temperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius |
||
− | $humidity |
+ | $humidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage |
− | $icecream |
+ | $icecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams |
+ | } |
||
+ | catch(Exception $e) |
||
+ | { |
||
+ | print 'Error loading value objects'; |
||
+ | } |
||
$inputs = array( |
$inputs = array( |
||
Line 43: | Line 68: | ||
$network->saveToFile('icecreams.dat'); |
$network->saveToFile('icecreams.dat'); |
||
+ | </source> |
||
+ | |||
+ | == Using trained network == |
||
+ | |||
+ | <source lang="php"> |
||
+ | |||
+ | require_once 'ANN/ANN_Network.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 />'; |
||
+ | |||
</source> |
</source> |
Revision as of 14:25, 31 December 2007
Training
require_once 'ANN/ANN_Network.php';
try
{
$network = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Creating a new one...';
$network = new ANN_Network(2,8,1);
$temperature = new ANN_InputValue(-15, 50); // Temperature in Celsius
$temperature->saveToFile('input_temperature.dat');
unset($temperature);
$humidity = new ANN_InputValue(0, 100); // Humidity percentage
$humidity->saveToFile('input_humidity.dat');
unset($humidity);
$icecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams
$icecream->saveToFile('output_quantity.dat');
unset($icecream);
}
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))
);
$outputs = array(
array($icecream->getOutputValue(20)),
array($icecream->getOutputValue(90)),
array($icecream->getOutputValue(70)),
array($icecream->getOutputValue(75))
);
$network->setInputs($inputs);
$network->setOutputs($outputs);
$network->train();
$network->saveToFile('icecreams.dat');
Using trained network
require_once 'ANN/ANN_Network.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 />';