iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 28
0
AI & Data

學習PHP Machine Learning的冒險歷程系列 第 28

[2020鐵人賽Day28]邂逅PHP Machine Learning-儲存模型

前言

到了資料數量爆炸的年代,訓練一個機器學習的模型時間是以幾何方式去增長,因此如果像傳統一樣,每次分類都要重複訓練模型,需要的時間非常的可觀。
PHP-ML有提供模型儲存的功能,所以可以讓我們訓練的模型回收再利用(?)

儲存/載入模型

若要深入,請參考文件

儲存模型

// 建立一個模型並訓練,不再贅述
use Phpml\Classification\KNearestNeighbors; 
use Phpml\ModelManager; 
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$labels = ['a', 'a', 'a', 'b', 'b', 'b'];
$classifier = new KNearestNeighbors(); 
$classifier->train($samples, $labels);

// 設定儲存檔案之位置 
$filepath = '/path/to/store/the/model';
// 建立一個模型的管理函式
$modelManager = new ModelManager();
//利用saveToFile函式,將$classifier模型儲存到filepath路徑
$modelManager->saveToFile($classifier, $filepath);

載入模型

// 設定儲存檔案之位置 
$filepath = '/path/to/store/the/model';
// 建立一個模型的管理函式
$modelManager = new ModelManager();
//將模型載入,由filepath
$restoredClassifier = $modelManager->restoreFromFile($filepath); 
$restoredClassifier->predict([3, 2]); // return 'b'

結語

如此一來,就可以將訓練好的模型重複利用了。


上一篇
[2020鐵人賽Day27]邂逅PHP Machine Learning-pipline
下一篇
[2020鐵人賽Day29]邂逅PHP Machine Learning-資料清洗
系列文
學習PHP Machine Learning的冒險歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言