この文書の現在のバージョンと選択したバージョンの差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
6_学習結果の保存_keras [2017/10/28] adash333 [(6)学習結果の保存(Keras)] |
6_学習結果の保存_keras [2018/10/07] (現在) |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
===== (6)学習結果の保存(Keras) ===== | ===== (6)学習結果の保存(Keras) ===== | ||
- | <KerasでMNIST目次>\\ | + | <wrap hi>Keras2でMNIST目次</wrap>\\ |
- | [[Kerasプログラミングの全体図]]\\ | + | [[Kerasプログラミングの全体図]] |
- | [[(1)Kerasを使用するためのimport文]]\\ | + | -[[(1)Kerasを使用するためのimport文]] |
- | [[(2)データ準備(Keras)]]\\ | + | -[[(2)データ準備(Keras)]] |
- | [[(3)モデル設定(Keras)]]\\ | + | -[[(3)モデル設定(Keras)]] |
- | [[(4)モデル学習(Keras)]]\\ | + | -[[(4)モデル学習(Keras)]] |
- | [[(5)結果の出力(Keras)]]\\ | + | -[[(5)結果の出力(Keras)]] |
- | (6)学習結果の保存(Keras) ←いまココ | + | -[[(6)学習結果の保存(Keras)]] <wrap hi><= いまココ</wrap> |
- | \\ | + | -[[(7)推測(Keras)]] |
- | [[(7)推測(Keras)]]\\ | + | |
- | 最初に以下をコピペするだけですが、プログラムごとに、さらにimport文の追加が必要になる場合があります。 | + | 以下をコピペするだけです。 |
<code> | <code> | ||
- | import keras | + | #6 学習結果の保存(Keras) |
- | from keras.models import Sequential | + | ### save model and weights |
- | from keras.layers import Dense, Dropout | + | json_string = model.to_json() |
- | from keras.optimizers import RMSprop | + | open('apple_orange_model.json', 'w').write(json_string) |
- | from keras.utils import np_utils | + | model.save_weights('apple_orange_weights.h5') |
+ | </code> | ||
- | from sklearn.model_selection import train_test_split | + | そのままです。学習モデルをjson形式で保存し、そのモデルに対応した学習結果のパラメータをh5形式で保存します。\\ |
+ | 次のpredict.pyで、新しい画像の予測(分類)を行う際に、このモデルとパラメータを読み込む予定です。 | ||
- | import numpy as np | + | |
- | from PIL import Image | + | |
- | import os | + | ===== 開発環境 ===== |
+ | Windows 8.1\\ | ||
+ | Anaconda \\ | ||
+ | Python 3.5\\ | ||
+ | Tensorflow 1.4\\ | ||
+ | Keras 2.0.9\\ | ||
+ | |||
+ | Keras2.0のインストール方法は[[windowsにkeras2.0をインストール]]をご覧下さい。 | ||
+ | |||
+ | このページは、[[(5)結果の出力(Keras)]]の続きであり、今回は、結果の出力を行っていきます。 | ||
+ | |||
+ | ===== 手順 ===== | ||
+ | |||
+ | |||
+ | ==== 0. 前回終了時の画面 ==== | ||
+ | [[(5)結果の出力(Keras)]]終了時の、以下のような状態から始めます。 | ||
+ | |||
+ | {{:pasted:20171110-055117.png}} | ||
+ | ==== 1. 学習結果の保存 ==== | ||
+ | 以下のコードを入力して、Shift + Enterを押します。 | ||
+ | |||
+ | <code> | ||
+ | #6 学習結果の保存(Keras) | ||
+ | ### save model and weights | ||
+ | json_string = model.to_json() | ||
+ | open('apple_orange_model.json', 'w').write(json_string) | ||
+ | model.save_weights('apple_orange_weights.h5') | ||
</code> | </code> | ||
- | 始めはかなりとっつきにくいimport文ですが、pythonの勉強をしていくにつれて、徐々に、慣れてきます。 | + | 以下のような画面になります。 |
- | keras特有のもの以外のimport文については、以下のリンクをご覧ください。 | + | {{:pasted:20171110-055228.png}} |
+ | |||
+ | Jupyter Notebook上では何も起こりませんが、 | ||
+ | |||
+ | <code> | ||
+ | %ls | ||
+ | </code> | ||
+ | |||
+ | と入力して、Shift+Enterを押すと、以下のように表示されて、train_MNIST_MLP.ipynb(今、入力しているJupyter Notebookのファイル)と同じフォルダに、 | ||
+ | |||
+ | <code> | ||
+ | "apple_orange_model.json" : モデルが記載されたjsonファイル | ||
+ | "apple_orange_weights.h5" : モデルのweight(学習結果)が記載されたバイナリデータ | ||
+ | </code> | ||
+ | |||
+ | の2つのファイルが作成されていることが分かります。 | ||
+ | |||
+ | {{:pasted:20171110-055811.png}} | ||
+ | |||
+ | {{:pasted:20171110-055914.png}} | ||
+ | |||
+ | Kerasでの学習結果の出力の手順は上記でおしまいです。 | ||
+ | |||
+ | 初めての方は、次は、<wrap hi>[[(7)推測(Keras)]]</wrap>に進んでください。 | ||
- | 機械学習で用いるpythonの”import xxx”まとめ\\ | ||
- | 2017/10/25\\ | ||
- | http://twosquirrel.mints.ne.jp/?p=20344 | ||
ライン 50: | ライン 97: | ||
===== リンク ===== | ===== リンク ===== | ||
- | 目次\\ | + | 次 [[(7)推測(Keras)]] |
- | [[Kerasプログラミングの全体図]]\\ | + | |
- | [[(1)Kerasを使用するためのimport文]]\\ | + | 前 [[(5)結果の出力(Keras)]] |
- | [[(2)データ準備(Keras)]]\\ | + | |
- | [[(3)モデル設定(Keras)]]\\ | + | |
- | [[(4)モデル学習(Keras)]]\\ | + | <wrap hi>Keras2でMNIST目次</wrap>\\ |
- | [[(5)結果の出力(Keras)]]\\ | + | [[Kerasプログラミングの全体図]] |
- | [[(6)学習結果の保存(Keras)]]\\ | + | -[[(1)Kerasを使用するためのimport文]] |
- | [[(7)推測(Keras)]]\\ | + | -[[(2)データ準備(Keras)]] |
+ | -[[(3)モデル設定(Keras)]] | ||
+ | -[[(4)モデル学習(Keras)]] | ||
+ | -[[(5)結果の出力(Keras)]] | ||
+ | -[[(6)学習結果の保存(Keras)]] | ||
+ | -[[(7)推測(Keras)]] | ||