FAQ

From Artificial Neural Network for PHP

What are the dat-files?

A dat-file is an auto-generated file. Its contents is a serialized structure of the saved object. It will be generated while training the neural network by using \ANN\InputValue::saveToFile() or \ANN\Network::saveToFile() and can be reloaded into the running network by \ANN\InputValue::loadFromFile() or \ANN\InputValue::loadFromFile().

The dat-files are not included to the example code downloads because they are auto-generated.

To save the dat-files the directory you are saving the files should have write permission to the PHP process running.

For example if your PHP script is running as a PHP module by Apache and the Apache is running as user www-data, so you can use the following code to set the permissions.

Change to the directory where your own ANN script is stored.

>cd <PROJECTDIR-OF-YOUR-ANN>

Create a subdirectory for all your dat-files.

>mkdir dats

Change the group owner of this subdirectory to www-data.

>chgrp www-data dats

Change the UNIX permissions to the group for write access to this subdirectory.

>chmod g+w dats

Due to security remove all others permissions in this case.

>chmod o-rwx dats

List your permissions.

>ls -la dats
drwxrwx---  7  user  www-data  4.0K  2009-10-28  08:52  dats

Use this subdirectory in your PHP scripts:

require_once 'ANN/Loader.php';

use ANN\Network;
use ANN\Values;

try
{
  $objNetwork = Network::loadFromFile('dats/xor.dat');
}
catch(Exception $e)
{
  print 'Creating a new one...';
	
  $objNetwork = new Network;

  $objValues = new Values;

  $objValues->train()
            ->input(0,0)->output(0)
            ->input(0,1)->output(1)
            ->input(1,0)->output(1)
            ->input(1,1)->output(0);

  $objValues->saveToFile('dats/values_xor.dat');
  
  unset($objValues);
}

try
{
  $objValues = Values::loadFromFile('dats/values_xor.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('dats/xor.dat');

$objNetwork->printNetwork();