2023/07/30

FLAMLとは

 Microsoft社製のAutoMLツールで、複数の機械学習アルゴリズムの中から最適なモデルとそのモデルの最適なパラメーターを自動でチューニングしてくれる機能を提供してくれるらしい。

A Fast Library for Automated Machine Learning & Tuning
これを利用する為にPython環境整備をして実際に動かしてみました。まあ、そんなに難しい作業ではなくサクッと出来るのは確かなんですが、そもそものゴールはあくまでも今作ってるアプリで利用する事なので、学習モデルをC#のアプリで利用出来なきゃ意味がない。って事で、散々調べました。まあ、結論から言えば、まだ自分には出来ない。

PythonでFLAML使って学習させて簡単にモデルを保存は出来ます。ただ、保存形式はPickle形式とかいわれるものです。で、C#側なんですが、このPickle形式は読めない。そこで実現出来そうな方法はPythonのFLAMLにて学習済みのモデルをOnnx形式で保存してC#側で読み込む。ここで問題があるんです。Python側にFLAMLを動かす機能は当然有ります。

import pickle
from sklearn.model_selection import train_test_split
from flaml import AutoML
import pandas as pd

targetName = 'ShinbaTime'
csvFile = targetName + '.csv'
modelFile = targetName + '.pkl'
onnxFile = targetName + '.onnx'
targetColumn = 'Souha'
df = pd.read_csv(csvFile)
X = df.drop(columns=[targetColumn])
y = df[targetColumn]
X_train, X_test, y_train, y_test = train_test_split(X, y)

automl = AutoML(task="regression", time_budget=10)
automl.fit(X_train, y_train)

まあ、ザックリですが、こんな感じにすれば、これまで使っていたCSVファイルから学習させる事は簡単に出来ます。本来はどのカラムが的な支持を与えるんですが、それ無しでも学習自体は問題なく(?)出来ます。

with open(modelFile, 'wb') as f:
    pickle.dump(automl.model, f)

とすればPickle形式での保存も簡単です。しかし、ここで欲しいのはOnnx形式なのでモデルをコンバートする必要があるんですが、これがなかなか難しい。

from onnxmltools.convert import convert_lightgbm

onnx_model = convert_lightgbm(automl.model, initial_types=initial_type)
onnxmltools.utils.save_model(onnx_model, onnxFile)

としても、

となり

from onnxmltools.convert import convert_sklearn

onnx_model = convert_sklearn(automl.model, initial_types=initial_type)
onnxmltools.utils.save_model(onnx_model, onnxFile)

としても

Unable to find a shape calculator for type '<class 'flaml.automl.model.XGBoostSklearnEstimator'>'.
It usually means the pipeline being converted contains a transformer or a predictor with no corresponding converter implemented in sklearn-onnx. If the converted is implemented in another library, you need to register the converted so that it can be used by sklearn-onnx (function update_registered_converter). If the model is not yet covered by sklearn-onnx, you may raise an issue to https://github.com/onnx/sklearn-onnx/issues to get the converter implemented or even contribute to the project. If the model is a custom model, a new converter must be implemented. Examples can be found in the gallery.

となり、お手上げな感じです。

0 件のコメント:

コメントを投稿