TensorFlowのEstimatorを使ってPythonでモデルを評価する方法
TensorFlowでは、Estimatorと組み合わせることでモデルの評価が可能です。評価には、classifierモジュールに用意されているevaluateメソッドを使用します。
前提知識
本記事では、KerasのSequential APIを使用します。Sequential APIは、各レイヤーがちょうど1つの入力テンソルと1つの出力テンソルを持つ、シンプルな層のスタックからなる逐次モデルを構築するのに適しています。
畳み込みニューラルネットワーク(CNN)を利用すれば、学習モデルを構築できます。
また、TensorFlow Textには、テキスト関連のクラスやオペレーションのコレクションが含まれており、TensorFlow 2.0と組み合わせて使用できます。シーケンスモデリングの前処理にも活用できます。
なお、以下のコードはGoogle Colaboratory上で実行することを想定しています。Google Colab(Colaboratory)はブラウザ上でPythonコードを実行できる環境で、設定は一切不要で、GPU(グラフィックス処理装置)にも無料でアクセスできます。ColaboratoryはJupyter Notebookをベースに構築されています。
Estimatorとは
Estimatorは、TensorFlowにおける完全なモデルの高レベルな抽象表現です。容易なスケーリングと非同期学習を実現できるよう設計されています。
ここでは、アヤメ(Iris)データセットを使用してモデルを学習させます。
コード例
eval_result = classifier.evaluate(input_fn=lambda: input_fn(test, test_y, training=False))
print('\nTest dataset accuracy is: {accuracy:0.3f}\n'.format(**eval_result))コード出典:https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
出力結果
INFO:tensorflow:Calling model_fn.
WARNING:tensorflow:Layer dnn is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2. The layer has dtype float32 because its dtype defaults to floatx.
If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.
To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2020-09-10T01:40:47Z
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Restoring parameters from /tmp/tmpbhg2uvbr/model.ckpt-5000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Inference Time : 0.21153s
INFO:tensorflow:Finished evaluation at 2020-09-10-01:40:47
INFO:tensorflow:Saving dict for global step 5000: accuracy = 0.96666664, average_loss = 0.42594802, global_step = 5000, loss = 0.42594802
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 5000: /tmp/tmpbhg2uvbr/model.ckpt-5000
Test dataset accuracy is: 0.967解説
モデルの学習が完了したら、その性能に関する情報を取得できます。
evaluate関数にはパラメータを渡す必要はありません。評価用の
input_fnは、データを1エポック分だけ生成します。eval_result辞書には、average_loss(サンプルごとの平均損失)、loss(ミニバッチごとの平均損失)、そしてEstimatorのglobal_step(学習の反復回数)の値が格納されます。
-
PythonとKerasを使ってモデル全体を保存・再利用する方法を徹底解説
TensorFlowとKerasとはTensorFlowは、Googleが提供する機械学習フレームワークです。オープンソースとして公開されており、Pythonと組み合わせて使用することで、各種アルゴリズムやディープラーニングアプリケーションの実装が可能になります。研究用途から本番環境まで幅広く活用されています。KerasはPythonで書かれたディープラーニングAPIです。高水準APIとして設計されており、生産性の高いインターフェースによって機械学習の問題を効率的に解決できます。TensorFlowフレームワーク上で動作し、素早い実験・試行を支援するために開発されました。高いスケーラビリティと
-
Kerasを使ってPythonでモデルをプロットする方法をわかりやすく解説
TensorFlowとはTensorFlowは、Googleが提供している機械学習フレームワークです。オープンソースとして公開されており、Pythonと組み合わせて使用することで、アルゴリズムの実装やディープラーニングアプリケーションの開発など、幅広い用途に活用できます。研究目的から本番環境での運用まで対応しており、複雑な数値計算を高速に実行するための最適化技術が数多く組み込まれています。TensorFlowにおける「テンソル(Tensor)」は、データを扱うための基本的なデータ構造です。テンソルは多次元配列(またはリスト)であり、データフローグラフと呼ばれる計算グラフのノード同士をエッジでつ