OSDN Git Service

データのセーブ、ロード機能を追加
authorshuepluter <shupeluter@hotmail.com>
Sat, 29 Sep 2018 12:37:11 +0000 (21:37 +0900)
committershuepluter <shupeluter@hotmail.com>
Sat, 29 Sep 2018 12:37:11 +0000 (21:37 +0900)
src/main/Python/Learning.py

index 7fb8f78..d203e5e 100644 (file)
@@ -4,20 +4,26 @@ from DataReader import DataReader
 
 from keras.utils.np_utils import to_categorical
 from keras.models import Model
+from keras.models import model_from_yaml
 from keras.layers import Dense, Input, Dropout
+import yaml
+import os.path
 
 def generate_data():
+
     reader = DataReader()
     __x_train, __y_train, __x_test, __y_test = reader.get_learning_data()
 
     __x_train = __x_train.reshape(len(__x_train), 500).astype(float)
-    __x_test  = __x_test.reshape((len(__x_test)), 500).astype(float)
+    __x_test = __x_test.reshape((len(__x_test)), 500).astype(float)
 
     __y_train = to_categorical(__y_train.astype('int32'), 11)
     __y_test  = to_categorical(__y_test.astype('int32'), 11)
     return __x_train, __y_train, __x_test, __y_test
 
+
 def create_neural_network():
+
     inputs = Input(shape=(500,))
     nw = Dense(200, activation='relu')(inputs)
     nw = Dropout(.5)(nw)
@@ -32,13 +38,55 @@ def create_neural_network():
     return model
 
 
-def main():
+def lean(model):
+
+    # データの作成
     x_train, y_train, x_test, y_test = generate_data()
-    model = create_neural_network()#type Model
 
+    # 学習と評価
     history = model.fit(x_train,y_train, batch_size=100, epochs=20, verbose=1,
                         validation_data=(x_test, y_test))
-    #Label 4 data
+
+    return model
+
+
+def persist_leaning_modle(model, model_path='data.yml', param_path='param.hdf5' ):
+
+    # モデルの保存
+    with open(model_path,mode='w') as f:
+        f.write(model.to_yaml())
+
+    # パラメータの保存
+    model.save_weights(param_path)
+
+
+def generate_leaning_model():
+    model = create_neural_network()  # type Model
+    model = lean(model)
+    # モデルデータの保存
+    persist_leaning_modle(model)
+
+
+def regenerate_leaning_model(model_path='data.yml', param_path='param.hdf5'):
+
+
+    with open(model_path) as model_yaml_file:
+        model_yaml = model_yaml_file.read()
+
+    model = model_from_yaml(model_yaml)
+
+    model.load_weights(
+        filepath=param_path
+    )
+    model.compile
+    return model
+
+
+def main():
+    model = regenerate_leaning_model()
+
+    # Label 4 data
+    ''' 以下分類実行例'''
     sample = numpy.array(
         [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
@@ -71,11 +119,12 @@ def main():
     result = model.predict(sample2,batch_size=1).astype(float)
     numpy.set_printoptions(precision=3, suppress=True)
     print(result)
-
+'''
     sample2 = sample2.reshape(1,500).astype(float)
     result = model.predict(sample2,batch_size=1).astype(float)
     numpy.set_printoptions(precision=3, suppress=True)
     print(result)
+    '''
 
 
 main()