fann_create_train_from_callback
(PECL fann >= 1.0.0)
fann_create_train_from_callback — Creates the training data struct from a user supplied function
Описание
$num_data
, int $num_input
, int $num_output
, callable $user_function
)Creates the training data struct from a user supplied function. As the training data are numerable (data 1, data 2...), the user must write a function that receives the number of the training data set (input, output) and returns the set.
Список параметров
-
num_data
-
The number of training data
-
num_input
-
The number of inputs per training data
-
num_output
-
The number of ouputs per training data
-
user_function
-
The user supplied function with following parameters:
- num - The number of the training data set
- num_input - The number of inputs per training data
- num_output - The number of ouputs per training data
The function should return an associative array with keys input and output and two array values of input and output.
Возвращаемые значения
Возвращает ресурс (resource) обучающих данных, или FALSE
в случае ошибки.
Примеры
Пример #1 fann_create_train_from_callback() example
<?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) {
// Do something with $train_data
}
?>
Смотрите также
- fann_read_train_from_file() - Reads a file that stores training data
- fann_train_on_data() - Trains on an entire dataset for a period of time
- fann_destroy_train() - Destructs the training data
- fann_save_train() - Save the training structure to a file
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- FANN (Fast Artificial Neural Network)
- fann_cascadetrain_on_data
- fann_cascadetrain_on_file
- fann_clear_scaling_params
- fann_copy
- fann_create_from_file
- fann_create_shortcut_array
- fann_create_shortcut
- fann_create_sparse_array
- fann_create_sparse
- fann_create_standard_array
- fann_create_standard
- fann_create_train_from_callback
- fann_create_train
- fann_descale_input
- fann_descale_output
- fann_descale_train
- fann_destroy_train
- fann_destroy
- fann_duplicate_train_data
- fann_get_activation_function
- fann_get_activation_steepness
- fann_get_bias_array
- fann_get_bit_fail_limit
- fann_get_bit_fail
- fann_get_cascade_activation_functions_count
- fann_get_cascade_activation_functions
- fann_get_cascade_activation_steepnesses_count
- fann_get_cascade_activation_steepnesses
- fann_get_cascade_candidate_change_fraction
- fann_get_cascade_candidate_limit
- fann_get_cascade_candidate_stagnation_epochs
- fann_get_cascade_max_cand_epochs
- fann_get_cascade_max_out_epochs
- fann_get_cascade_min_cand_epochs
- fann_get_cascade_min_out_epochs
- fann_get_cascade_num_candidate_groups
- fann_get_cascade_num_candidates
- fann_get_cascade_output_change_fraction
- fann_get_cascade_output_stagnation_epochs
- fann_get_cascade_weight_multiplier
- fann_get_connection_array
- fann_get_connection_rate
- fann_get_errno
- fann_get_errstr
- fann_get_layer_array
- fann_get_learning_momentum
- fann_get_learning_rate
- fann_get_MSE
- fann_get_network_type
- fann_get_num_input
- fann_get_num_layers
- fann_get_num_output
- fann_get_quickprop_decay
- fann_get_quickprop_mu
- fann_get_rprop_decrease_factor
- fann_get_rprop_delta_max
- fann_get_rprop_delta_min
- fann_get_rprop_delta_zero
- fann_get_rprop_increase_factor
- fann_get_sarprop_step_error_shift
- fann_get_sarprop_step_error_threshold_factor
- fann_get_sarprop_temperature
- fann_get_sarprop_weight_decay_shift
- fann_get_total_connections
- fann_get_total_neurons
- fann_get_train_error_function
- fann_get_train_stop_function
- fann_get_training_algorithm
- fann_init_weights
- fann_length_train_data
- fann_merge_train_data
- fann_num_input_train_data
- fann_num_output_train_data
- fann_print_error
- fann_randomize_weights
- fann_read_train_from_file
- fann_reset_errno
- fann_reset_errstr
- fann_reset_MSE
- fann_run
- fann_save_train
- fann_save
- fann_scale_input_train_data
- fann_scale_input
- fann_scale_output_train_data
- fann_scale_output
- fann_scale_train_data
- fann_scale_train
- fann_set_activation_function_hidden
- fann_set_activation_function_layer
- fann_set_activation_function_output
- fann_set_activation_function
- fann_set_activation_steepness_hidden
- fann_set_activation_steepness_layer
- fann_set_activation_steepness_output
- fann_set_activation_steepness
- fann_set_bit_fail_limit
- fann_set_callback
- fann_set_cascade_activation_functions
- fann_set_cascade_activation_steepnesses
- fann_set_cascade_candidate_change_fraction
- fann_set_cascade_candidate_limit
- fann_set_cascade_candidate_stagnation_epochs
- fann_set_cascade_max_cand_epochs
- fann_set_cascade_max_out_epochs
- fann_set_cascade_min_cand_epochs
- fann_set_cascade_min_out_epochs
- fann_set_cascade_num_candidate_groups
- fann_set_cascade_output_change_fraction
- fann_set_cascade_output_stagnation_epochs
- fann_set_cascade_weight_multiplier
- fann_set_error_log
- fann_set_input_scaling_params
- fann_set_learning_momentum
- fann_set_learning_rate
- fann_set_output_scaling_params
- fann_set_quickprop_decay
- fann_set_quickprop_mu
- fann_set_rprop_decrease_factor
- fann_set_rprop_delta_max
- fann_set_rprop_delta_min
- fann_set_rprop_delta_zero
- fann_set_rprop_increase_factor
- fann_set_sarprop_step_error_shift
- fann_set_sarprop_step_error_threshold_factor
- fann_set_sarprop_temperature
- fann_set_sarprop_weight_decay_shift
- fann_set_scaling_params
- fann_set_train_error_function
- fann_set_train_stop_function
- fann_set_training_algorithm
- fann_set_weight_array
- fann_set_weight
- fann_shuffle_train_data
- fann_subset_train_data
- fann_test_data
- fann_test
- fann_train_epoch
- fann_train_on_data
- fann_train_on_file
- fann_train
Коментарии
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);
}
}
}
?>