php教程

fann_create_train_from_callback

(PECL fann >= 1.0.0)

fann_create_train_from_callback从用户提供的函数创建训练数据结构。

说明

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

从用户提供的函数创建训练数据结构。当训练数据可数,用户需要构造一个用来接收训练数据集(包括输入,输出)数量和返回值为集合的函数。

参数

num_data

训练数据的数量。

num_input

每个训练数据的输入数。

num_output

每个训练数据的输出数。

user_function

用户提供的函数包含以下参数:

  • num - 训练数据集的数量。
  • num_input - 数量数据的输入数。
  • num_output - 数量数据的输出数。

函数应该返回一个包含inputoutput 键的数组,并且这两个键的值分别表示输入输出的值(皆为数组)。

返回值

Returns a train data resource on success, or FALSE on error.

范例

Example #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_input1),
        
"output" => array_fill(0$num_output1),
    );
}

$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
}
?>

参见

User Contributed Notes

geekgirljoy at gmail dot com 21-Oct-2016 05:34
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);
        }
    }
}
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788