FAQ: Difference between revisions

From Artificial Neural Network for PHP
(New page: == 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 u...)
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
== What are the dat-files? ==
== 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()'''.
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.
The dat-files are not included to the example code downloads because they are auto-generated.
Line 36: Line 36:
Use this subdirectory in your PHP scripts:
Use this subdirectory in your PHP scripts:
<source lang="php">
<source lang="php">
require_once 'ANN/ANN_Loader.php';
require_once 'ANN/Loader.php';

use ANN\Network;
use ANN\Values;


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


$objValues = new ANN_Values;
$objValues = new Values;


$objValues->train()
$objValues->train()
Line 63: Line 66:
try
try
{
{
$objValues = ANN_Values::loadFromFile('dats/values_xor.dat');
$objValues = Values::loadFromFile('dats/values_xor.dat');
}
}
catch(Exception $e)
catch(Exception $e)
Line 81: Line 84:


$objNetwork->printNetwork();
$objNetwork->printNetwork();
</source>

== 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.

<source lang="php">

$objNetwork->setOutputErrorTolerance(0.1);

</source>
</source>

Latest revision as of 21:22, 12 December 2012

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();

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.

  $objNetwork->setOutputErrorTolerance(0.1);