Added examples

Added train_all.php and test_all.php
Esse commit está contido em:
Joy Harvel
2016-07-29 00:25:24 -07:00
commit bdf74eb008
2 arquivos alterados com 83 adições e 0 exclusões
+48
Ver Arquivo
@@ -0,0 +1,48 @@
<?php
function test($net, $test_data) {
$train_file = (dirname(__FILE__) . '/' . $net . '_float.net');
if (!is_file($train_file)) {
print('The file ' . $net . '_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 = $net . ' test (';
for($i = 0; $i < $num_inputs; $i++) {
$test_result .= $test_data[$i];
if ($i < $num_inputs) {
$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<br>" . 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));
?>
+35
Ver Arquivo
@@ -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>");
?>