Using date input support class: Difference between revisions

From Artificial Neural Network for PHP
(New page: == FAQ == For information about dat-files have a view to the FAQ page. == Training == <source lang="php"> require_once 'ANN/Loader.php'; try { $objNetwork = ANN_Network::loadFro...)
 
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:


For information about dat-files have a view to the [[FAQ]] page.
For information about dat-files have a view to the [[FAQ]] page.

== XML for holidays ==

The standard filename is ''Holidays.xml'' but can be changed by calling ANN_DateInputs::setHolidaysFilename().

<source lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<holidays>
<holiday>
<day>1</day>
<month>1</month>
<year>any</year>
<country>Germany</country>
<state>any</state>
<description>New Year</description>
</holiday>
<holiday>
<day>24</day>
<month>12</month>
<year>any</year>
<country>Germany</country>
<state>any</state>
<description>Christmas</description>
</holiday>
</holidays>

</source>


== Training ==
== Training ==
Line 18: Line 45:
$objNetwork = new ANN_Network(2, 8, 1);
$objNetwork = new ANN_Network(2, 8, 1);



$objDateInput = new ANN_DateInputs('2010-01-03'); // As of ANN 2.1.3
$objDateInput = new ANN_DateInputs('2010-01-03'); // As of ANN 2.1.3



$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius
$objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius

Latest revision as of 18:53, 6 January 2010

FAQ

For information about dat-files have a view to the FAQ page.

XML for holidays

The standard filename is Holidays.xml but can be changed by calling ANN_DateInputs::setHolidaysFilename().

<?xml version="1.0" encoding="UTF-8"?>
<holidays>
	<holiday>
		<day>1</day>
		<month>1</month>
		<year>any</year>
		<country>Germany</country>
		<state>any</state>
		<description>New Year</description>
	</holiday>
	<holiday>
		<day>24</day>
		<month>12</month>
		<year>any</year>
		<country>Germany</country>
		<state>any</state>
		<description>Christmas</description>
	</holiday>
</holidays>

Training

require_once '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);


  $objDateInput = new ANN_DateInputs('2010-01-03'); // As of ANN 2.1.3


  $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),
                   $objDateInput->getHolidaysInWeek() // As of ANN 2.1.3
                   )
            ->output(
                   $objIcecream->getOutputValue(20)
                   )
            ->input(
                   $objTemperature->getInputValue(30),
                   $objHumidity->getInputValue(40),
                   $objDateInput->getHolidaysInWeek()
                   )
            ->output(
                   $objIcecream->getOutputValue(90)
                   )
            ->input(
                   $objTemperature->getInputValue(32),
                   $objHumidity->getInputValue(30),
                   $objDateInput->getHolidaysInWeek()
                   )
            ->output(
                   $objIcecream->getOutputValue(70)
                   )
            ->input(
                   $objTemperature->getInputValue(33),
                   $objHumidity->getInputValue(20),
                   $objDateInput->getHolidaysInWeek()
                   )
            ->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)
{
  die('Error loading value objects');
}

try
{
  $objValues = ANN_Values::loadFromFile('values_icecreams.dat');
}
catch(Exception $e)
{
  die('Loading of values failed');
}

$objNetwork->setValues($objValues); // As of ANN 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();