前提・実現したいこと
kerasでIrisデータを使ってgridsearchを試しています.
発生している問題・エラーメッセージ
Epoch1/1と出力されているのですが,epoch2以降は学習されていないのでしょうか?
該当のソースコード
Python
1import numpy as np 2from sklearn import datasets, preprocessing 3from sklearn.model_selection import train_test_split 4from sklearn.model_selection import GridSearchCV 5from keras.models import Sequential 6from keras.layers.core import Dense, Activation 7from keras.utils import np_utils 8from keras import backend as K 9from keras.wrappers.scikit_learn import KerasClassifier 10 11 12# import data and divided it into training and test purposes 13iris = datasets.load_iris() 14x = preprocessing.scale(iris.data) 15y = np_utils.to_categorical(iris.target) 16x_tr, x_te, y_tr, y_te = train_test_split(x, y, train_size = 0.7) 17num_classes = y_te.shape[1] 18 19 20# Define model for iris classification 21def iris_model(activation="relu", optimizer="adam", out_dim=100): 22 model = Sequential() 23 model.add(Dense(out_dim, input_dim=4, activation=activation)) 24 model.add(Dense(out_dim, activation=activation)) 25 model.add(Dense(num_classes, activation="softmax")) 26 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) 27 return model 28 29# Define options for parameters 30activation = ["relu", "sigmoid"] 31optimizer = ["adam", "adagrad"] 32out_dim = np.array([100, 200]) 33nb_epoch = np.array([10, 25]) 34batch_size = np.array([10, 20]) 35 36 37# Retrieve model and parameter into GridSearchCV 38model = KerasClassifier(build_fn=iris_model, verbose=1) 39param_grid = dict(activation=activation, 40 optimizer=optimizer, 41 out_dim=out_dim, 42 nb_epoch=nb_epoch, 43 batch_size=batch_size) 44grid = GridSearchCV(estimator=model, param_grid=param_grid) 45 46 47# Run grid search 48grid_result = grid.fit(x_tr, y_tr) 49 50 51# Get the best score and the optimized mode 52print (grid_result.best_score_) 53print (grid_result.best_params_) 54 55# Evaluate the model with test data 56grid_eval = grid.predict(x_te) 57def y_binary(i): 58 if i == 0: return [1, 0, 0] 59 elif i == 1: return [0, 1, 0] 60 elif i == 2: return [0, 0, 1] 61y_eval = np.array([y_binary(i) for i in grid_eval]) 62accuracy = (y_eval == y_te) 63print (np.count_nonzero(accuracy == True) / (accuracy.shape[0] * accuracy.shape[1])) 64 65 66# Now see the optimized model 67model = iris_model(activation=grid_result.best_params_['activation'], 68 optimizer=grid_result.best_params_['optimizer'], 69 out_dim=grid_result.best_params_['out_dim']) 70model.summary()
試したこと
Epochを例えば10000のように膨大な数字にして,本当に指定のエポック数の学習ができているのか試しましたが,特に膨大な時間がかかることもなく学習ができました.
補足情報(FW/ツールのバージョンなど)
Python3.6
Keras2.2.4
sklearn0.19.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/11 08:10