Comparar commits
20 Commits
| Autor | SHA1 | Data | |
|---|---|---|---|
| ff88ddbb2d | |||
| 666a9782e9 | |||
| cbceac3277 | |||
| df0d3efa40 | |||
| dea7be11e1 | |||
| 31aa82d04e | |||
| 9752d4c3b9 | |||
| 684fd7efa5 | |||
| 318449e9f7 | |||
| 18a9742978 | |||
| 456f5d75ac | |||
| 34f60ecc33 | |||
| 6c97ba9124 | |||
| bdf74eb008 | |||
| bbe05abdc6 | |||
| 64b4a588fa | |||
| 5cb637544a | |||
| 6ed98be3ff | |||
| 39d169640e | |||
| 3ca0916b35 |
@@ -20,6 +20,8 @@ $ sudo apt-get install libfann-dev
|
||||
```
|
||||
Fann installation can be skipped if an RPM for Fedora is used (`libfann` is in the package dependencies).
|
||||
|
||||
If the library is re-installed manually, then all old library file should be removed before re-installing otherwise the old library version could be linked.
|
||||
|
||||
#### Fedora
|
||||
|
||||
The RPM package for PHP FANN is available in Remi's repository: http://rpms.famillecollet.com/
|
||||
@@ -70,46 +72,46 @@ Precompiled binary `dll` libraries for php-fann and libfann are available on [th
|
||||
|
||||
## Examples
|
||||
|
||||
These are just two basic examples for simple training and running supplied data on the trained network.
|
||||
There are three example projects: [Logic Gates](examples/logic_gates/), [OCR](examples/ocr/) & [Pathfinder](examples/pathfinder/).
|
||||
|
||||
### `simple_train.php`
|
||||
#### Logic Gates
|
||||
|
||||
```php
|
||||
$num_input = 2;
|
||||
$num_output = 1;
|
||||
$num_layers = 3;
|
||||
$num_neurons_hidden = 3;
|
||||
$desired_error = 0.001;
|
||||
$max_epochs = 500000;
|
||||
$epochs_between_reports = 1000;
|
||||
###### Simple
|
||||
|
||||
$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);
|
||||
The Simple example trains a single neural network to perform the XOR operation.
|
||||
|
||||
if ($ann) {
|
||||
fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
|
||||
fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);
|
||||
[simple_train.php](examples/logic_gates/simple_train.php)
|
||||
|
||||
$filename = dirname(__FILE__) . "/xor.data";
|
||||
if (fann_train_on_file($ann, $filename, $max_epochs, $epochs_between_reports, $desired_error))
|
||||
fann_save($ann, dirname(__FILE__) . "/xor_float.net");
|
||||
[simple_test.php](examples/logic_gates/simple_test.php)
|
||||
|
||||
fann_destroy($ann);
|
||||
}
|
||||
```
|
||||
### `simple_test.php`
|
||||
|
||||
```php
|
||||
$train_file = (dirname(__FILE__) . "/xor_float.net");
|
||||
if (!is_file($train_file))
|
||||
die("The file xor_float.net has not been created! Please run simple_train.php to generate it");
|
||||
###### All
|
||||
|
||||
The All example trains 7 seperate neural networks to perform the AND, NAND, NOR, NOT, OR, XNOR & XOR operations.
|
||||
|
||||
[train_all.php](examples/logic_gates/train_all.php)
|
||||
|
||||
[test_all.php](examples/logic_gates/test_all.php)
|
||||
|
||||
|
||||
#### OCR
|
||||
|
||||
OCR is a practical example of Optical Character Recognition using FANN. While this example is limited and does make mistakes, the concepts illustrated by OCR can be applied to a more robust stacked network that uses feature extraction and convolution layers to recognize text of any font in any size image.
|
||||
|
||||
[train_ocr.php](examples/ocr/train_ocr.php)
|
||||
|
||||
[test_ocr.php](examples/ocr/test_ocr.php)
|
||||
|
||||
|
||||
|
||||
#### Pathfinder
|
||||
|
||||
Pathfinder is an example of a neural network that is capable of plotting an 8 direction step path from a starting position in a 5x5 grid to an ending position in that grid. To keep the Pathfinder example simple it is not trained to deal with walls or non-traversable terrain however it would be very easy to add that by adding additional training.
|
||||
|
||||
[pathfinder_train.php](examples/pathfinder/pathfinder_train.php)
|
||||
|
||||
[pathfinder_test.php](examples/pathfinder/pathfinder_test.php)
|
||||
|
||||
|
||||
$ann = fann_create_from_file($train_file);
|
||||
if (!$ann)
|
||||
die("ANN could not be created");
|
||||
|
||||
$input = array(-1, 1);
|
||||
$calc_out = fann_run($ann, $input);
|
||||
printf("xor test (%f,%f) -> %f\n", $input[0], $input[1], $calc_out[0]);
|
||||
fann_destroy($ann);
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
4 2 1
|
||||
-1 -1
|
||||
-1
|
||||
-1 1
|
||||
-1
|
||||
1 -1
|
||||
-1
|
||||
1 1
|
||||
1
|
||||
@@ -0,0 +1,9 @@
|
||||
4 2 1
|
||||
-1 -1
|
||||
1
|
||||
-1 1
|
||||
1
|
||||
1 -1
|
||||
1
|
||||
1 1
|
||||
-1
|
||||
@@ -0,0 +1,9 @@
|
||||
4 2 1
|
||||
-1 -1
|
||||
1
|
||||
-1 1
|
||||
-1
|
||||
1 -1
|
||||
-1
|
||||
1 1
|
||||
-1
|
||||
@@ -0,0 +1,5 @@
|
||||
2 1 1
|
||||
-1
|
||||
1
|
||||
1
|
||||
-1
|
||||
@@ -0,0 +1,9 @@
|
||||
4 2 1
|
||||
-1 -1
|
||||
-1
|
||||
-1 1
|
||||
1
|
||||
1 -1
|
||||
1
|
||||
1 1
|
||||
1
|
||||
@@ -16,7 +16,10 @@ if ($ann) {
|
||||
|
||||
$filename = dirname(__FILE__) . "/xor.data";
|
||||
if (fann_train_on_file($ann, $filename, $max_epochs, $epochs_between_reports, $desired_error))
|
||||
fann_save($ann, dirname(__FILE__) . "/xor_float.net");
|
||||
print('xor trained.<br>' . PHP_EOL);
|
||||
|
||||
if (fann_save($ann, dirname(__FILE__) . "/xor_float.net"))
|
||||
print('xor_float.net saved.<br><a href="simple_test.php">Test</a>' . PHP_EOL);
|
||||
|
||||
fann_destroy($ann);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
function test($ann, $test_data) {
|
||||
$train_file = (dirname(__FILE__) . '/' . $ann . '_float.net');
|
||||
if (!is_file($train_file)) {
|
||||
print('The file ' . $ann . '_float.net has not been created! Please run train_all.php to generate it.<br>' . PHP_EOL);
|
||||
} else{
|
||||
$ann = fann_create_from_file($train_file);
|
||||
if ($ann) {
|
||||
$calc_out = fann_run($ann, $test_data);
|
||||
|
||||
$num_inputs = count($test_data);
|
||||
$num_outputs = count($calc_out);
|
||||
|
||||
$test_result = $ann . ' test (';
|
||||
for($i = 0; $i < $num_inputs; $i++) {
|
||||
$test_result .= $test_data[$i];
|
||||
|
||||
if ($i < $num_inputs - 1) {
|
||||
$test_result .= ', ';
|
||||
}
|
||||
}
|
||||
$test_result .= ') -> ';
|
||||
for($i = 0; $i < $num_outputs; $i++) {
|
||||
$test_result .= $calc_out[$i];
|
||||
|
||||
if ($i < $num_outputs - 1) {
|
||||
$test_result .= ', ';
|
||||
}
|
||||
}
|
||||
print($test_result . '<br>' . PHP_EOL);
|
||||
|
||||
fann_destroy($ann);
|
||||
} else {
|
||||
die("Invalid file format" . PHP_EOL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test('and', array(1, 1));
|
||||
test('nand', array(-1, -1));
|
||||
test('nor', array(-1, -1));
|
||||
test('not', array(-1));
|
||||
test('or', array(1, -1));
|
||||
test('xnor', array(-1, -1));
|
||||
test('xor', array(-1, 1));
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
function train($data, $num_input, $num_output, $num_layers, $num_neurons_hidden, $desired_error, $max_epochs, $epochs_between_reports){
|
||||
|
||||
$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);
|
||||
|
||||
if ($ann) {
|
||||
fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
|
||||
fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);
|
||||
|
||||
$filename = dirname(__FILE__) . '/' . $data . '.data';
|
||||
if (fann_train_on_file($ann, $filename, $max_epochs, $epochs_between_reports, $desired_error)) {
|
||||
print($data . ' trained.<br>' . PHP_EOL);
|
||||
}
|
||||
|
||||
if (fann_save($ann, dirname(__FILE__) . '/' . $data . '_float.net')) {
|
||||
print($data . '_float.net saved.<br>' . PHP_EOL);
|
||||
}
|
||||
|
||||
fann_destroy($ann);
|
||||
}
|
||||
}
|
||||
|
||||
train('and', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('nand', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('nor', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('not', 1, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('or', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('xnor', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
train('xor', 2, 1, 3, 3, 0.001, 500000, 1000);
|
||||
|
||||
|
||||
print("<a href='test_all.php'>Test All</a>");
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,9 @@
|
||||
4 2 1
|
||||
-1 -1
|
||||
1
|
||||
-1 1
|
||||
-1
|
||||
1 -1
|
||||
-1
|
||||
1 1
|
||||
1
|
||||
|
Depois Largura: | Altura: | Tamanho: 114 B |
|
Depois Largura: | Altura: | Tamanho: 111 B |
|
Depois Largura: | Altura: | Tamanho: 115 B |
|
Depois Largura: | Altura: | Tamanho: 116 B |
|
Depois Largura: | Altura: | Tamanho: 112 B |
|
Depois Largura: | Altura: | Tamanho: 115 B |
|
Depois Largura: | Altura: | Tamanho: 129 B |
|
Depois Largura: | Altura: | Tamanho: 127 B |
|
Depois Largura: | Altura: | Tamanho: 122 B |
|
Depois Largura: | Altura: | Tamanho: 138 B |
|
Depois Largura: | Altura: | Tamanho: 130 B |
|
Depois Largura: | Altura: | Tamanho: 132 B |
|
Depois Largura: | Altura: | Tamanho: 114 B |
|
Depois Largura: | Altura: | Tamanho: 132 B |
|
Depois Largura: | Altura: | Tamanho: 132 B |
|
Depois Largura: | Altura: | Tamanho: 132 B |
|
Depois Largura: | Altura: | Tamanho: 122 B |
|
Depois Largura: | Altura: | Tamanho: 134 B |
|
Depois Largura: | Altura: | Tamanho: 115 B |
|
Depois Largura: | Altura: | Tamanho: 122 B |
|
Depois Largura: | Altura: | Tamanho: 129 B |
|
Depois Largura: | Altura: | Tamanho: 113 B |
|
Depois Largura: | Altura: | Tamanho: 129 B |
|
Depois Largura: | Altura: | Tamanho: 133 B |
|
Depois Largura: | Altura: | Tamanho: 130 B |
|
Depois Largura: | Altura: | Tamanho: 135 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 122 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 128 B |
|
Depois Largura: | Altura: | Tamanho: 118 B |
|
Depois Largura: | Altura: | Tamanho: 130 B |
|
Depois Largura: | Altura: | Tamanho: 117 B |
|
Depois Largura: | Altura: | Tamanho: 123 B |
|
Depois Largura: | Altura: | Tamanho: 129 B |
|
Depois Largura: | Altura: | Tamanho: 114 B |
|
Depois Largura: | Altura: | Tamanho: 123 B |
|
Depois Largura: | Altura: | Tamanho: 127 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 128 B |
|
Depois Largura: | Altura: | Tamanho: 126 B |
|
Depois Largura: | Altura: | Tamanho: 133 B |
|
Depois Largura: | Altura: | Tamanho: 128 B |
|
Depois Largura: | Altura: | Tamanho: 115 B |
|
Depois Largura: | Altura: | Tamanho: 118 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 131 B |
|
Depois Largura: | Altura: | Tamanho: 117 B |
|
Depois Largura: | Altura: | Tamanho: 128 B |
|
Depois Largura: | Altura: | Tamanho: 113 B |
|
Depois Largura: | Altura: | Tamanho: 117 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 111 B |
|
Depois Largura: | Altura: | Tamanho: 113 B |
|
Depois Largura: | Altura: | Tamanho: 129 B |
|
Depois Largura: | Altura: | Tamanho: 124 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 124 B |
|
Depois Largura: | Altura: | Tamanho: 128 B |
|
Depois Largura: | Altura: | Tamanho: 124 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 130 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 119 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 117 B |
|
Depois Largura: | Altura: | Tamanho: 115 B |
|
Depois Largura: | Altura: | Tamanho: 118 B |
|
Depois Largura: | Altura: | Tamanho: 121 B |
|
Depois Largura: | Altura: | Tamanho: 124 B |
|
Depois Largura: | Altura: | Tamanho: 125 B |
|
Depois Largura: | Altura: | Tamanho: 124 B |
|
Depois Largura: | Altura: | Tamanho: 118 B |
|
Depois Largura: | Altura: | Tamanho: 123 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 118 B |
|
Depois Largura: | Altura: | Tamanho: 120 B |
|
Depois Largura: | Altura: | Tamanho: 119 B |
|
Depois Largura: | Altura: | Tamanho: 123 B |
|
Depois Largura: | Altura: | Tamanho: 123 B |