note 120073 added to function.fann-create-train-from-callback
| From: | geekgirljoy at gmail dot com | Date: | Fri, 21 Oct 2016 17:34:47 +0000 |
| Subject: | note 120073 added to function.fann-create-train-from-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206936@lists.php.net to get a copy of this message | ||
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);
}
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 76.174.86.153
----
Manual Page -- http://php.net/manual/en/function.fann-create-train-from-callback.php
Edit -- https://master.php.net/note/edit/120073
Del: integrated -- https://master.php.net/note/delete/120073/integrated
Del: useless -- https://master.php.net/note/delete/120073/useless
Del: bad code -- https://master.php.net/note/delete/120073/bad+code
Del: spam -- https://master.php.net/note/delete/120073/spam
Del: non-english -- https://master.php.net/note/delete/120073/non-english
Del: in docs -- https://master.php.net/note/delete/120073/in+docs
Del: other reasons-- https://master.php.net/note/delete/120073
Reject -- https://master.php.net/note/reject/120073
Search -- https://master.php.net/manage/user-notes.php