2024 年 PHP Conference Japan

fann_train_epoch

(PECL fann >= 1.0.0)

fann_train_epoch使用一組訓練資料訓練一個 epoch

說明

fann_train_epoch(資源 $ann, 資源 $data): 浮點數

使用儲存在 data 中的訓練資料訓練一個 epoch。一個 epoch 指的是所有訓練資料都被完整考慮一次。

此函式會傳回在實際訓練之前或期間計算出的 MSE 誤差。這並非訓練 epoch 後的實際 MSE,因為計算實際 MSE 需要再次遍歷整個訓練集。在訓練期間使用此值已足夠。

此函式使用的訓練演算法是由 fann_set_training_algorithm() 函式選擇的。

參數

ann

神經網路 資源

data

神經網路訓練資料 資源

回傳值

均方誤差 (MSE),或錯誤時回傳 false

參見

新增註解

使用者貢獻的註解 1 則註解

geekgirljoy at gmail dot com
6 年前
這段程式碼示範了使用 fann_train_epoch 訓練 XOR,並讓您透過觀察虛擬 MSE(均方誤差)來觀察訓練過程。

其他訓練函式:fann_train_on_data、fann_train_on_file、fann_train。

當您想要在訓練時觀察人工神經網路,並且可能儲存快照或在訓練期間比較競爭網路時,fann_train_epoch 很有用。

fann_train_epoch 與 fann_train 的不同之處在於它採用資料資源(訓練檔案),而 fann_train 採用輸入陣列和單獨的輸出陣列,因此使用 fann_train_epoch 來觀察資料檔案(回呼訓練資源)上的訓練,並在觀察手動指定的資料時使用 fann_train。

程式碼範例

<?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: https://php.dev.org.tw/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;
?>
To Top