PHP 8.3.4 Released!

fann_create_train_from_callback

(PECL fann >= 1.0.0)

fann_create_train_from_callbackCrea una estructura de datos de entrenamiento desde una función proporcionada por el usuario

Descripción

fann_create_train_from_callback(
    int $num_data,
    int $num_input,
    int $num_output,
    callable $user_function
): resource

Crea una estructura de datos de entrenamiento desde una función proporcionada por el usuario. Debido a que los datos de entrenamiento se numeran (datos 1, datos 2...), el usuario debe escribir una función que reciba el número del conjunto de datos de entrenamiento (entrada, salida) y que devuelva el conjunto.

Parámetros

num_data

El número de datos de entrenamiento

num_input

El número de entradas por datos de entrenamiento

num_output

El número de salidas por datos de entrenamiento

user_function

La función proporcionada por elusuario con los siguientes parámetros:

  • num - El número del conjunto de datos de entrenamiento
  • num_input - El número de entradas por datos de entrenamiento
  • num_output - El número de salidas por datos de entrenamiento

La función debería devolver un array asociativo con las claves input y output y con dos valores para la entrada y la salida.

Valores devueltos

Devuelve un resource de datos de entrenamiento en caso de éxito, o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de fann_create_train_from_callback()

<?php
function create_train_callback($num_data, $num_input, $num_output) {
return array(
"input" => array_fill(0, $num_input, 1),
"output" => array_fill(0, $num_output, 1),
);
}

$num_data = 3;
$num_input = 2;
$num_output = 1;
$train_data = fann_create_train_from_callback($num_data, $num_input, $num_output, "create_train_callback");
if (
$train_data) {
// Hacer algo con $train_data
}
?>

Notas

Nota:

This function is only available if the fann extension has been build against libfann >= 2.2.

Ver también

add a note

User Contributed Notes 1 note

up
6
geekgirljoy at gmail dot com
7 years ago
This code can be used to read training data from MySQL rather than a text file.

<?php

// MySQL for This Example:
/*
CREATE TABLE `TrainingSets` (
`ID` int(11) NOT NULL,
`Name` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
`TrainingData` text COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `TrainingSets` ADD PRIMARY KEY (`ID`);

INSERT INTO `TrainingSets` (`ID`, `Name`, `TrainingData`) VALUES(1, 'XOR', '-1 -1\n-1\n-1 1\n1\n1 -1\n1\n1 1\n-1');

ALTER TABLE `TrainingSets` MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
*/

// This function calls pulls the TrainingData from MySQL
function get_training_data_from_db($id) {
$table_name = "TrainingSets";
$field = "TrainingData";
$connection=mysqli_connect("host","username","password","database"); // change to your DB credentials
$result=mysqli_query($connection,"SELECT $field FROM $table_name");
$data=mysqli_fetch_assoc($result);
mysqli_close($connection);

return
$data[$field];
}

// This function prepares the newline delimited data to be handed off to FANN
/*
Example of "newline delimited data" (like XOR in a Plain Text File) stored in MySQL:
-1 -1
-1
-1 1
1
1 1
-1
1 -1
1
*/
function prepare_data_from_db($training_data) {
$training_data = explode( "\n", $training_data ); // convert training data rows to array
$num_data = count($training_data);

// Sift the data and split inputs and outputs
for($i=0;$i<$num_data;$i++) {
if(
$i % 2) { // $training_data[$i] is Output
$training_data['outputs'][] = explode( " ", $training_data[$i]);
}else{
// $training_data[$i] is Input
$training_data['inputs'][] = explode( " ", $training_data[$i]);
}
}
// remove the unsifted data
foreach ($training_data as $key => $value) {
if (
is_numeric($key)) {
unset(
$training_data[$key]);
}
}
return
$training_data; // returned the prepaired associative array
}

// This function hands the prepared data over to FANN
function create_train_callback($num_data, $num_input, $num_output) {
global
$training_data;
global
$current_dataset;

$dataset = array("input" => $training_data['inputs'][$current_dataset],
"output" => $training_data['outputs'][$current_dataset]);
$current_dataset++;

return
$dataset;
}

// Initialize the program variables
$record_id = 1; // the 'ID' for the training data in MySQL
$current_dataset = 0;
$num_input = 2;
$num_output = 1;
$num_layers = 3;
$num_neurons = 3;
$desired_error = 0.001;
$max_epochs = 500000;
$epochs_between_reports = 1000;

$training_data = get_training_data_from_db($record_id); // Get the Training Data from MySQL
$training_data = prepare_data_from_db($training_data); // Prepare the data
$num_data = count($training_data["input"]); // How many sets are there?

// Hand the data over to FANN
$train_data = fann_create_train_from_callback($num_data, $num_input, $num_output, "create_train_callback");

// Test for $train_data
if ($train_data) {

// Create $ann
$ann = fann_create_standard($num_layers, $num_input, $num_neurons, $num_output);

// Test for $ann
if ($ann) {
fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);

// Train XOR ANN with training data obtainied from MySQL
if (fann_train_on_data($ann, $train_data, $max_epochs, $epochs_between_reports, $desired_error)){
print(
'XOR trained.<br>' . PHP_EOL);

// Test $ann
$input = array(-1, 1);
$calc_out = fann_run($ann, $input);
printf("xor test (%f,%f) -> %f\n", $input[0], $input[1], $calc_out[0]);

// destore $ann
fann_destroy($ann);
}
}
}
?>
To Top