内容へ移動
サルでもわかる機械学習
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
トレース:
kerasプログラミングの全体図
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
===== Kerasプログラミングの全体図 ===== <wrap hi>Keras2でMNIST目次</wrap>\\ [[Kerasプログラミングの全体図]] -[[(1)Kerasを使用するためのimport文]] -[[(2)データ準備(Keras)]] -[[(3)モデル設定(Keras)]] -[[(4)モデル学習(Keras)]] -[[(5)結果の出力(Keras)]] -[[(6)学習結果の保存(Keras)]] -[[(7)推測(Keras)]] Kerasを用いて機械学習を行う場合、以下のような流れでプログラミング文を読んだり書いたりしていくと、分かりやすいと思います。 実際に自前データを動かそうとするときは、ある程度、pythonの勉強と、機械学習の勉強が必要だと思われます。 <code> # train.py #1 Kerasを使用するためのimport文 #2 データ準備(Keras) #3 モデル設定(Keras) #4 モデル学習(Keras) #5 結果の出力(Keras) #6 学習結果の保存(Keras) # predict.py #7 推測(Keras) </code> ===== Kerasプログラミングの全体図(コード記載) ===== 上記にコードの一部を入れて、再度記載します。 <code> # train.py #1 Kerasを使用するためのimport文 import keras from keras.models import Sequential from keras.layers import Dense, Dropout from keras.optimizers import RMSprop from keras.utils import np_utils # その他、以下が必要になることが多い from sklearn.model_selection import train_test_split import numpy as np from PIL import Image import os #2 データ準備(Keras) image_list = [] label_list = [] # 画像を読み込み、リサイズ、正規化などを行い、 # Numpy配列に変換し、「学習用データ」と「テストデータ」を作成する。 X_train, X_test, y_train, y_test = train_test_split(image_list, label_list, test_size=0.33, random_state=111) #3 モデル設定(Keras) model = Sequential() model.add(Dense(512, activation='relu', input_shape=(784,))) model.add(Dropout(0.2)) (以下、model.add()でモデルを加えていき、) model.summary() # モデルの表示 model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy']) # 損失関数、最適化手法などを設定 #4 モデル学習(Keras) history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_test, y_test)) #5 結果の出力(Keras) score = model.evaluate(X_test, y_test, verbose=0) print('Test loss:', score[0]) #6 学習結果の保存(Keras) json_string = model.to_json() open('apple_orange_model.json', 'w').write(json_string) model.save_weights('apple_orange_weights.h5') # predict.py #7 推測(Keras) from keras.preprocessing import image import numpy as np import sys filepath = "(予測したいファイルのPATH)" image = np.array(Image.open(filepath).convert("L").resize((28, 28))) print(filepath) image = image.reshape(1, 784).astype("float32")[0] result = model.predict_classes(np.array([image / 255.])) print("result:", result[0]) </code> ===== Kerasプログラミングの例(train.pyとpredict.py) ===== C:/py/keras/MNIST_MLP/フォルダに、dataフォルダとtrain.py、predict.pyが入っており、dataフォルダの中身は以下のようになっているものとします。 {{:pasted:20171028-011530.png}} ==== train.py ==== <html> <script src="https://gist.github.com/adash333/5d1fe3b0a17059a3ffe3f16063c59054.js"></script> </html> Jupyter Notebookでの実行結果 {{:pasted:20171028-011921.png}} ==== predict.py ==== <html> <script src="https://gist.github.com/adash333/4812c0c0d2c2daa5db3d848456d96322.js"></script> </html> Jupyter Notebookでの実行結果 {{:pasted:20171028-012121.png}} とりあえず、コードをJupyter Notebook上でコピペして実行するところまででした。 次回から、このコードを順に解説させていただきたいと思います。 次:<wrap hi>[[(1)Kerasを使用するためのimport文]]</wrap> ===== 参考文献 ===== 初めてKerasプログラミングをやるときは、以下の2つの本がおすすめです。[[http://amzn.to/2gRFbt3|ゼロから作るDeep Learning]]でPythonの基本と理論を学び、[[http://amzn.to/2yPNyw5|詳解 ディープラーニング]]で、Kerasでの実装方法を学ぶのがよいと思われます。 \\ <html> <iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//rcm-fe.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=twosquirrel-22&o=9&p=8&l=as4&m=amazon&f=ifr&ref=as_ss_li_til&asins=4873117585&linkId=13a7db2c19cc5f40d6ab48906de8abd1"></iframe> <iframe style="width:120px;height:240px;" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" src="//rcm-fe.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=twosquirrel-22&o=9&p=8&l=as4&m=amazon&f=ifr&ref=as_ss_li_til&asins=4839962510&linkId=d722909965b5eab4196d370757843f6f"></iframe> </html> Keras公式GitHub\\ keras/examples/mnist_mlp.py\\ Branch:keras-2\\ https://github.com/fchollet/keras/blob/keras-2/examples/mnist_mlp.py kerasのmnistのサンプルを読んでみる ash8h 2017年07月29日に投稿\\ https://qiita.com/ash8h/items/29e24fc617b832fba136 ===== 次節以降 ===== [[https://gist.github.com/adash333/5d1fe3b0a17059a3ffe3f16063c59054|上記の手書き数字MNISTのMLP(Multilayer perceptron )モデルによる学習]]を例に、一つずつ解説させていただきたいと思います。 いつもあまり面白くないMNISTですが、Kerasプログラミングを理解する上で避けて通れないので、頑張ってやってみたいと思います。 ===== リンク ===== 次 [[(1)Kerasを使用するためのimport文]] 前 [[Kerasで手書き文字認識MNIST]] <wrap hi>Keras2でMNIST目次</wrap>\\ [[Kerasプログラミングの全体図]] -[[(1)Kerasを使用するためのimport文]] -[[(2)データ準備(Keras)]] -[[(3)モデル設定(Keras)]] -[[(4)モデル学習(Keras)]] -[[(5)結果の出力(Keras)]] -[[(6)学習結果の保存(Keras)]] -[[(7)推測(Keras)]]
kerasプログラミングの全体図.txt
· 最終更新: 2018/10/07 (外部編集)
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ