AutoML-Zero のビルドとインストール(google-research のソースコードを使用)(Ubuntu 上)

前準備

Ubuntu のシステム更新

Ubuntu で OS のシステム更新を行うときは, 次のコマンドを実行.

# パッケージリストの情報を更新
sudo apt update
# インストール済みのパッケージを包括的に更新 (依存関係も考慮)
sudo apt full-upgrade
# 変更をシステム全体に確実に反映させるために再起動
sudo shutdown -r now

Git のインストール

次のコマンドを実行.

# パッケージリストの情報を更新
sudo apt update
sudo apt -y install git

bazel のインストール

AutoML-Zero のビルドには bazel を用いる. 次のコマンドを実行.

# パッケージリストの情報を更新
sudo apt update
sudo apt -y install apt-transport-https curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update
sudo apt -y install bazel

C/C++ コンパイラと Make とビルドツールのインストール

インストールするには,次のコマンドを実行.

# パッケージリストの情報を更新
sudo apt update
sudo apt -y install build-essential gcc g++ make libtool texinfo dpkg-dev pkg-config

AutoML-Zero(google-research)のビルドとインストール

  • google-research のダウンロード
    cd /usr/local/
    sudo rm -rf google-research
    sudo git clone https://github.com/google-research/google-research.git
    sudo chown -R ${USER} google-research
    
  • AutoML-Zero のビルドと実行
    cd /usr/local/google-research/automl_zero
    ./run_demo.sh
    

    * 上記のデモは,線形回帰タスクを解くプログラムを進化的探索によって自動的に発見するものである. 探索終了後,発見されたアルゴリズムのコードが表示される.


    Python による AutoML の簡易例

    AutoML の考え方(モデルやハイパーパラメータの探索を自動化する)を体験するため, scikit-learn を用いてハイパーパラメータの簡易な探索を行う Python コード例を示す.

    import numpy as np
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.metrics import accuracy_score
    
    X, y = load_iris(return_X_y=True)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
    
    n_estimators_candidates = [10, 50, 100, 200]
    max_depth_candidates = [None, 2, 4, 6]
    
    best_score = 0.0
    best_params = None
    
    for n_estimators in n_estimators_candidates:
        for max_depth in max_depth_candidates:
            model = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=0)
            model.fit(X_train, y_train)
            y_pred = model.predict(X_test)
            score = accuracy_score(y_test, y_pred)
            if score > best_score:
                best_score = score
                best_params = (n_estimators, max_depth)
    
    print("Best params (n_estimators, max_depth):", best_params)
    print("Best accuracy:", best_score)
    

    * このコードは,決定木の本数(n_estimators)と木の最大深さ(max_depth)の組み合わせを総当たりで試し, テストデータに対する正解率が最も高くなる組み合わせを選び出す,探索型のハイパーパラメータ最適化(AutoML の基本的な考え方の一つ)を示すものである.