77import numpy as np
88from keras .datasets import mnist
99from keras .models import Sequential
10- from keras .layers import Dense , Dropout
10+ from keras .layers import Dense , Dropout , Flatten , Conv2D , MaxPooling2D
1111from keras .wrappers .scikit_learn import KerasClassifier
1212from modAL .models import ActiveLearner
1313
@@ -18,13 +18,30 @@ def create_keras_model():
1818 This function compiles and returns a Keras model.
1919 Should be passed to KerasClassifier in the Keras scikit-learn API.
2020 """
21+
2122 model = Sequential ()
23+ model .add (Conv2D (32 , kernel_size = (3 , 3 ), activation = 'relu' , input_shape = (28 , 28 , 1 )))
24+ model .add (Conv2D (64 , (3 , 3 ), activation = 'relu' ))
25+ model .add (MaxPooling2D (pool_size = (2 , 2 )))
26+ model .add (Dropout (0.25 ))
27+ model .add (Flatten ())
28+ model .add (Dense (128 , activation = 'relu' ))
29+ model .add (Dropout (0.5 ))
30+ model .add (Dense (10 , activation = 'softmax' ))
31+
32+ model .compile (
33+ loss = keras .losses .categorical_crossentropy ,
34+ optimizer = keras .optimizers .Adadelta (),
35+ metrics = ['accuracy' ]
36+ )
37+
38+ """model = Sequential()
2239 model.add(Dense(512, activation='relu', input_shape=(784, )))
2340 model.add(Dropout(0.2))
2441 model.add(Dense(512, activation='relu'))
2542 model.add(Dropout(0.2))
2643 model.add(Dense(10, activation='sigmoid'))
27- model .compile (loss = 'categorical_crossentropy' , optimizer = 'adadelta' , metrics = ['accuracy' ])
44+ model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])"""
2845
2946 return model
3047
@@ -41,8 +58,8 @@ def create_keras_model():
4158
4259# read training data
4360(X_train , y_train ), (X_test , y_test ) = mnist .load_data ()
44- X_train = X_train .reshape (60000 , 784 ).astype ('float32' ) / 255
45- X_test = X_test .reshape (10000 , 784 ).astype ('float32' ) / 255
61+ X_train = X_train .reshape (60000 , 28 , 28 , 1 ).astype ('float32' ) / 255
62+ X_test = X_test .reshape (10000 , 28 , 28 , 1 ).astype ('float32' ) / 255
4663y_train = keras .utils .to_categorical (y_train , 10 )
4764y_test = keras .utils .to_categorical (y_test , 10 )
4865
@@ -65,20 +82,21 @@ def create_keras_model():
6582learner = ActiveLearner (
6683 estimator = classifier ,
6784 X_training = X_initial , y_training = y_initial ,
68- verbose = 0
85+ verbose = 1
6986)
7087
7188# the active learning loop
7289n_queries = 10
7390for idx in range (n_queries ):
7491 query_idx , query_instance = learner .query (X_pool , n_instances = 200 , verbose = 0 )
92+ print (query_idx )
7593 learner .teach (
7694 X = X_pool [query_idx ], y = y_pool [query_idx ],
77- verbose = 0
95+ verbose = 1
7896 )
7997 # remove queried instance from pool
8098 X_pool = np .delete (X_pool , query_idx , axis = 0 )
8199 y_pool = np .delete (y_pool , query_idx , axis = 0 )
82100
83101# the final accuracy score
84- print (learner .score (X_test , y_test , verbose = 0 ))
102+ print (learner .score (X_test , y_test , verbose = 1 ))
0 commit comments