LoginSignup
0
0

AutoKeras Install 備忘録

Last updated at Posted at 2023-08-31

概要

少し古いがAutoKerasを使ってみたかったのでインストールについて備忘録に残す。
さらっと調べた感じAutoKerasはENAS (Efficient Neural Architecture Search)という探索アルゴリズムを使いNeural Networkの最適化を行ってくれるパケージのよう。
ENASのpaperにはRNNやLSTMへの適用についても記載されているので、時系列系NN modelの精度改善も期待できる。

  • 実施期間: 2023年8月
  • 環境:Ubuntu20.04LTS
  • GPU: GeForce RTX3070
  • Python: condaのPython3.9

1.TensorFlow

AutoKerasのオフィシャルによるとTFが予め入っていることを前提にしているように読めるので、AutoKerasインストールの前にTFをインストールする。
下記公式サイトの「3. conda環境を作成する」以降を実行する。
AutoKerasの要件よりcondaの仮想環境はpython=3.9で作成する。

仮想環境のactivate後、conda listでpipが見当たらなければ下記を行う。

conda install -c conda-forge pip

上記公式サイトの最後で下記を実行することになっており、現時点では2.13.0が入る。

pip install tensorflow==2.13.*

AutoKerasだけをインストールするとGPUを使ってくれなかった。

TF単体のインストールの場合、conda install -c conda-forge tensorflowコマンドでインストールする。cuda関連も自動で仮想環境に入れてくれるからだが、AutoKerasとの相性が悪いのかfit()で大量のエラーを吐いたので上記の方法でインストールした。

2. AutoKeras

つまり、

pip install git+https://github.com/keras-team/keras-tuner.git
pip install autokeras

頻繁にメンテナンスわれているのかわからないがAutoKerasのインストール中でも最新版の同TF 2.13.0を入れようとする。今回はバージョンが同じなので先に入れたTF 2.13.0はアンインストールされなかったが、今後AutoKerasについているTFのバージョンが2.14以上になった場合、そのTFが正しく現行のcudaで動作するか不安。

3. 動作確認

つまり、

import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import mnist

import autokeras as ak

(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train.shape)  # (60000, 28, 28)
print(y_train.shape)  # (60000,)
print(y_train[:3])  # array([7, 2, 1], dtype=uint8)

# Initialize the image classifier.
clf = ak.ImageClassifier(overwrite=True, max_trials=1)
# Feed the image classifier with training data.
clf.fit(x_train, y_train, epochs=10)


# Predict with the best model.
predicted_y = clf.predict(x_test)
print(predicted_y)

# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))

GPUを使っているなら1epochは5秒程度のはず。fit()中、別ターミナルでnvidia-smiコマンドでGPUメモリ使用量を確認してもOK。

番外 Windowsへのインストール

流れは上記のUbuntu向けと同じ。
下記のWindows向け手順に沿うこと。

AutoKerasと関係ないが、VSCodeでconda仮想環境をインタプリタに選択し実行するとPowerShellがターミナルとして起動し、condaコマンドを知らないとエラーを吐く。
「conda:用語 'conda'は、コマンドレット、関数、どうのこうの…」
確かにconda仮想環境の設定はcommand promptで行ったため、PowerShellはcondaを認知していないだろう。

解決方法はココに書かれているが、
Anacondaのcommand promptで下記を実行

conda init powershell

管理者権限でPowerShellを起動し、

Set-ExecutionPolicy RemoteSigned

これでVSCodeで起動するPowerShellはcondaを認識するようになる。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0