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 Conv2D , MaxPooling2D , Dropout , Flatten , Dense
1111from keras .wrappers .scikit_learn import KerasClassifier
1212from modAL .models import ActiveLearner
1313
@@ -19,11 +19,14 @@ def create_keras_model():
1919 Should be passed to KerasClassifier in the Keras scikit-learn API.
2020 """
2121 model = Sequential ()
22- model .add (Dense (512 , activation = 'relu' , input_shape = (784 , )))
23- model .add (Dropout (0.2 ))
24- model .add (Dense (512 , activation = 'relu' ))
25- model .add (Dropout (0.2 ))
26- model .add (Dense (10 , activation = 'sigmoid' ))
22+ model .add (Conv2D (32 , kernel_size = (3 , 3 ), activation = 'relu' , input_shape = (28 , 28 , 1 )))
23+ model .add (MaxPooling2D (pool_size = (2 , 2 )))
24+ model .add (Dropout (0.25 ))
25+ model .add (Flatten ())
26+ model .add (Dense (128 , activation = 'relu' ))
27+ model .add (Dropout (0.5 ))
28+ model .add (Dense (10 , activation = 'softmax' ))
29+
2730 model .compile (loss = 'categorical_crossentropy' , optimizer = 'adadelta' , metrics = ['accuracy' ])
2831
2932 return model
@@ -41,8 +44,8 @@ def create_keras_model():
4144
4245# read training data
4346(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
47+ X_train = X_train .reshape (60000 , 28 , 28 , 1 ).astype ('float32' ) / 255
48+ X_test = X_test .reshape (10000 , 28 , 28 , 1 ).astype ('float32' ) / 255
4649y_train = keras .utils .to_categorical (y_train , 10 )
4750y_test = keras .utils .to_categorical (y_test , 10 )
4851
0 commit comments