kerasプログラミングの全体図

差分

この文書の現在のバージョンと選択したバージョンの差分を表示します。

この比較画面にリンクする

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
kerasプログラミングの全体図 [2017/10/27]
adash333
kerasプログラミングの全体図 [2018/10/07] (現在)
ライン 1: ライン 1:
 ===== 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の勉強と、機械学習の勉強が必要だと思われます。 実際に自前データを動かそうとするときは、ある程度、pythonの勉強と、機械学習の勉強が必要だと思われます。
ライン 7: ライン 16:
 <​code>​ <​code>​
 # train.py # train.py
- 
 #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)
ライン 27: ライン 30:
 # predict.py # predict.py
 #7 推測(Keras) #7 推測(Keras)
-(1)#1 Chainerを使用するためのimport文+</​code>​
  
-(2)#2 tuple_datasetによるデータ準備・設定+===== Kerasプログラミング全体図(コード記載) =====
  
-(3)#3 モデルの述 +にコ一部を入れて、再度記載します。
-class MyModel(Chain):​ +
-    def __init__(self):​ +
-        super(MyModel,​ self).__init__( +
-        # パラメタを含む関数宣言 +
-    )+
  
-    def __call__(self,​ x,t): +<​code>​ 
-       ​モデルを記述+train.py
  
-(4)#4 モデルと最適化アルゴリズム設定(ほぼお約束の3行) +#1 Kerasを使用するためimport文 
-model = MyModel() +import keras 
-optimizer = optimizers.Adam() +from keras.models import Sequential 
-optimizer.setup(model)+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
  
-(5)#学習(Trainer利用する場合) +#2 データ準備(Keras) 
-iterator ​iterators.SerialIterator(tdatabsize) +image_list = [] 
-updater ​training.StandardUpdater(iteratoroptimizer) +label_list = [] 
-trainer ​training.Trainer(updater,​ (ep, ‘epoch’)) +# 画像を読み込み、リサイズ、正規化などを行い、 
-trainer.extend(extensions.ProgressBar())+# Numpy配列に変換し、「学習用データ」と「テストデータ」作成する 
 +X_train, X_test, y_train, y_test ​train_test_split(image_listlabel_list, test_size=0.33random_state=111)
  
-trainer.run()+#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
  
-6)#6 結果出力+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>​ </​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>​
 +
 +&nbsp;
  
 +<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
 ===== 次節以降 ===== ===== 次節以降 =====
  
-MNISTを例に、一つずつ解説させていただきたいと思います。+[[https://​gist.github.com/​adash333/​5d1fe3b0a17059a3ffe3f16063c59054|上記の手書き数字MNISTのMLP(Multilayer perceptron )モデルによる学習]]を例に、一つずつ解説させていただきたいと思います。
  
 いつもあまり面白くないMNISTですが、Kerasプログラミングを理解する上で避けて通れないので、頑張ってやってみたいと思います。 いつもあまり面白くないMNISTですが、Kerasプログラミングを理解する上で避けて通れないので、頑張ってやってみたいと思います。
ライン 70: ライン 157:
 ===== リンク ===== ===== リンク =====
  
-目次 +次 [[(1)Kerasを使用するためのimport文]] 
-  -[[Kerasプログラミングの全体図]]+ 
 +前 [[Kerasで手書き文字認識MNIST]] 
 + 
 + 
 +<wrap hi>​Keras2でMNIST目次</​wrap>​\\ 
 +[[Kerasプログラミングの全体図]]
   -[[(1)Kerasを使用するためのimport文]]   -[[(1)Kerasを使用するためのimport文]]
   -[[(2)データ準備(Keras)]]   -[[(2)データ準備(Keras)]]
ライン 79: ライン 171:
   -[[(6)学習結果の保存(Keras)]]   -[[(6)学習結果の保存(Keras)]]
   -[[(7)推測(Keras)]]   -[[(7)推測(Keras)]]
 +
 +

kerasプログラミングの全体図.1509107095.txt.gz · 最終更新: 2018/10/07 (外部編集)