函式名稱:fann_cascadetrain_on_file()
適用版本:FANN >= 2.1.0
函式描述:用於使用級聯訓練演演演演算法對神經網路進行訓練的函式。此函式基於提供的訓練檔案來訓練網路,使其能夠根據輸入資料進行預測。
用法: bool fann_cascadetrain_on_file(resource $ann, string $filename, int $max_neurons, int $neurons_between_reports, float $desired_error):
引數說明:
- $ann:FANN神經網路資源(透過fann_create_standard()或fann_create_from_file()等函式建立)。
- $filename:訓練檔案的路徑和檔名。
- $max_neurons:最大神經元數。每層能使用的最大神經元數。
- $neurons_between_reports:報告之間的神經元數。在訓練過程中,每隔指定數量的神經元數,會列印一次訓練報告。
- $desired_error:期望的誤差。一旦神經網路的誤差達到或小於此值,訓練過程將停止。
返回值: 如果訓練成功,則返回 true。如果訓練失敗,則返回 false。
示例: 下面是一個使用fann_cascadetrain_on_file()函式的簡單示例:
<?php
// 建立一個神經網路
$ann = fann_create_standard(3, 2, 3, 1);
// 設定訓練演演演演算法為級聯訓練
fann_set_training_algorithm($ann, FANN_TRAIN_CASCADE);
// 設定訓練引數
fann_set_cascade_activation_functions($ann, array(FANN_SIGMOID_SYMMETRIC, FANN_SIGMOID_SYMMETRIC, FANN_LINEAR));
fann_set_cascade_activation_steepnesses($ann, array(0.5, 0.5, 0.5));
fann_set_cascade_min_num_candidates($ann, 8);
fann_set_cascade_max_num_candidates($ann, 1000);
fann_set_cascade_candidate_change_fraction($ann, 0.01);
fann_set_cascade_candidate_stagnation_epochs($ann, 10);
fann_set_cascade_weight_multiplier($ann, 0.4);
// 訓練神經網路
if (fann_cascadetrain_on_file($ann, "training_data.txt", 100, 10, 0.001)) {
echo "訓練成功!\n";
} else {
echo "訓練失敗!\n";
}
// 儲存訓練好的網路
fann_save($ann, "trained_network.net");
在上述示例中,我們首先建立了一個具有3個層次結構(2個輸入神經元,3個隱藏層神經元和1個輸出神經元)的神經網路。然後,我們設定訓練演演演演算法為級聯訓練,並透過fann_set_cascade_系列函式設定了級聯訓練的相關引數。最後,我們使用fann_cascadetrain_on_file()函式對神經網路進行了訓練,並儲存了訓練好的網路。