This code demonstrates training XOR using fann_train_epoch and will let you watch the training process by observing a psudo MSE (mean squared error).
Other training functions: fann_train_on_data, fann_train_on_file, fann_train.
fann_train_epoch is useful when you want to observe the ANN while it is training and perhaps save snapshots or compare competing networks during training.
fann_train_epoch is different from fann_train in that it takes a data resource (training file) whereas fann_train takes an array of inputs and a separate array of outputs so use fann_train_epoch for observing training on data files (callback training resources) and use fann_train when observing manually specified data.
Example code:
<?php
$num_input = 2;
$num_output = 1;
$num_layers = 3;
$num_neurons_hidden = 3;
$desired_error = 0.0001;
$max_epochs = 500000;
$current_epoch = 0;
$epochs_between_saves = 100; // Minimum number of epochs between saves
$epochs_since_last_save = 0;
$filename = dirname(__FILE__) . "/xor.data";
// Initialize psudo mse (mean squared error) to a number greater than the desired_error
// this is what the network is trying to minimize.
$psudo_mse_result = $desired_error * 10000; // 1
$best_mse = $psudo_mse_result; // keep the last best seen MSE network score here
// Initialize ANN
$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);
if ($ann) {
echo 'Training ANN... ' . PHP_EOL;
// Configure the ANN
fann_set_training_algorithm ($ann , FANN_TRAIN_BATCH);
fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);
// Read training data
$train_data = fann_read_train_from_file($filename);
// Check if psudo_mse_result is greater than our desired_error
// if so keep training so long as we are also under max_epochs
while(($psudo_mse_result > $desired_error) && ($current_epoch <= $max_epochs)){
$current_epoch++;
$epochs_since_last_save++;
// See: http://php.net/manual/en/function.fann-train-epoch.php
// Train one epoch with the training data stored in data.
//
// One epoch is where all of the training data is considered
// exactly once.
//
// This function returns the MSE error as it is calculated
// either before or during the actual training. This is not the
// actual MSE after the training epoch, but since calculating this
// will require to go through the entire training set once more.
// It is more than adequate to use this value during training.
$psudo_mse_result = fann_train_epoch ($ann , $train_data );
echo 'Epoch ' . $current_epoch . ' : ' . $psudo_mse_result . PHP_EOL; // report
// If we haven't saved the ANN in a while...
// and the current network is better then the previous best network
// as defined by the current MSE being less than the last best MSE
// Save it!
if(($epochs_since_last_save >= $epochs_between_saves) && ($psudo_mse_result < $best_mse)){
$best_mse = $psudo_mse_result; // we have a new best_mse
// Save a Snapshot of the ANN
fann_save($ann, dirname(__FILE__) . "/xor.net");
echo 'Saved ANN.' . PHP_EOL; // report the save
$epochs_since_last_save = 0; // reset the count
}
} // While we're training
echo 'Training Complete! Saving Final Network.' . PHP_EOL;
// Save the final network
fann_save($ann, dirname(__FILE__) . "/xor.net");
fann_destroy($ann); // free memory
}
echo 'All Done!' . PHP_EOL;
?>fann_train_epoch
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
fann_train_epoch
Референца за `function.fann-train-epoch.php` со подобрена типографија и навигација.
fann_train_epoch
(PECL fann >= 1.0.0)
fann_train_epoch — Обучи една епоха со сет на податоци за обука
= NULL
Обучи една епоха со податоците за обука зачувани во data. Една епоха е кога сите податоци за обука се земаат предвид точно еднаш.
Оваа функција го враќа MSE грешката како што се пресметува или пред или за време на самата обука. Ова не е вистинскиот MSE по епохата на обука, но бидејќи пресметувањето на ова ќе бара повторно поминување низ целиот сет за обука. Повеќе од доволно е да се користи оваа вредност за време на обуката.
Алгоритмот за обука што се користи од оваа функција се избира со fann_set_training_algorithm() function.
Параметри
Вратени вредности
MSE, или false при грешка.
Види Исто така
- fann_train_on_data() Низа од посакувани излези. Оваа низа мора да биде точно
- fann_test_data() Тестирај сет на податоци за обука и пресметува MSE за податоците за обука
- fann_get_MSE() - Ги чита средните квадратни грешки од мрежата
- fann_set_training_algorithm() - Го поставува алгоритмот за тренирање