Selling Icecreams: Difference between revisions
From Artificial Neural Network for PHP
| (23 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. |
|||
== Training == |
== Training == |
||
<source lang="php"> |
<source lang="php"> |
||
<?php |
|||
require_once '../ANN/Loader.php'; |
|||
use ANN\Network; |
|||
require_once 'ANN/ANN_Network.php'; |
|||
use ANN\InputValue; |
|||
use ANN\OutputValue; |
|||
use ANN\Values; |
|||
try |
try { |
||
$objNetwork = Network::loadFromFile('icecreams.dat'); |
|||
{ |
|||
} catch (Exception $e) { |
|||
$network = ANN_Network::loadFromFile('icecreams.dat'); |
|||
$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'); |
|||
} |
} |
||
catch(Exception $e) |
|||
try { |
|||
{ |
|||
$objValues = Values::loadFromFile('values_icecreams.dat'); |
|||
print 'Creating a new one...'; |
|||
} catch (Exception $e) { |
|||
die('Loading of values failed'); |
|||
$network = new ANN_Network(2,8,1); |
|||
} |
} |
||
$objNetwork->setValues($objValues); |
|||
$temperature = new ANN_InputValue(-15, 50); // Temperature |
|||
$boolTrained = $objNetwork->train(); |
|||
$humidity = new ANN_InputValue(0, 100); // Humidity |
|||
print ($boolTrained) ? 'Network trained' : 'Network not trained completely. Please re-run the script'; |
|||
$icecream = new ANN_OutputValue(0, 300); // Ice-Cream |
|||
$objNetwork->saveToFile('icecreams.dat'); |
|||
$inputs = array( |
|||
</source> |
|||
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)) |
|||
); |
|||
== Using trained network == |
|||
$outputs = array( |
|||
array($icecream->GetOutputValue(20)), |
|||
<source lang="php"> |
|||
array($icecream->GetOutputValue(90)), |
|||
<?php |
|||
array($icecream->GetOutputValue(70)), |
|||
require_once '../ANN/Loader.php'; |
|||
array($icecream->GetOutputValue(75)) |
|||
); |
|||
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 |
|||
$network->setInputs($inputs); |
|||
$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); |
|||
$network->setOutputs($outputs); |
|||
$arrOutputs = $objNetwork->getOutputs(); |
|||
$network->train(); |
|||
foreach ($arrOutputs as $arrOutput) |
|||
$network->saveToFile('icecreams.dat'); |
|||
foreach ($arrOutput as $floatOutput) |
|||
print $objIcecream->getRealOutputValue($floatOutput) . "\n"; |
|||
</source> |
</source> |
||
Latest revision as of 07:59, 6 October 2025
FAQ
For information about dat-files have a view to the FAQ page.
Training
<?php
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) {
$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');
}
try {
$objValues = Values::loadFromFile('values_icecreams.dat');
} catch (Exception $e) {
die('Loading of values failed');
}
$objNetwork->setValues($objValues);
$boolTrained = $objNetwork->train();
print ($boolTrained) ? 'Network trained' : 'Network not trained completely. Please re-run the script';
$objNetwork->saveToFile('icecreams.dat');
Using trained network
<?php
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) . "\n";