From ANN - Artificial Neural Network for PHP 5
Jump to: navigation, search

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.


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;
  $objNetwork = Network::loadFromFile('dats/xor.dat');
catch(Exception $e)
  print 'Creating a new one...';
  $objNetwork = new Network;
  $objValues = new Values;
  $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';

How to adjust the network's tolerance down?

The default error tolerance of the neural network is set to 0.02 if running a linear network. To increase tolerance use the following code.