12
12
from modAL .models import ActiveLearner
13
13
14
14
15
+ # build function for the Keras' scikit-learn API
15
16
def create_keras_model ():
16
17
"""
17
18
This function compiles and returns a Keras model.
18
- Should be passed for the KerasClassifier in the
19
- Keras scikit-learn API.
20
- :return: Keras model
19
+ Should be passed to KerasClassifier in the Keras scikit-learn API.
21
20
"""
22
21
model = Sequential ()
23
22
model .add (Dense (512 , activation = 'relu' , input_shape = (784 , )))
@@ -30,6 +29,9 @@ def create_keras_model():
30
29
return model
31
30
32
31
32
+ # create the classifier
33
+ classifier = KerasClassifier (create_keras_model )
34
+
33
35
"""
34
36
Data wrangling
35
37
1. Reading data from Keras
@@ -38,59 +40,45 @@ def create_keras_model():
38
40
"""
39
41
40
42
# read training data
41
- (x_train , y_train ), (x_test , y_test ) = mnist .load_data ()
42
- x_train = x_train .reshape (60000 , 784 ).astype ('float32' )/ 255
43
- x_test = x_test .reshape (10000 , 784 ).astype ('float32' )/ 255
43
+ (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
44
46
y_train = keras .utils .to_categorical (y_train , 10 )
45
47
y_test = keras .utils .to_categorical (y_test , 10 )
46
48
47
- # select the first example from each category
48
- initial_idx = list ()
49
- for label in range (10 ):
50
- for elem_idx , elem in enumerate (x_train ):
51
- if y_train [elem_idx ][label ] == 1.0 :
52
- initial_idx .append (elem_idx )
53
- break
54
-
55
49
# assemble initial data
56
- x_initial = x_train [initial_idx ]
50
+ n_initial = 1000
51
+ initial_idx = np .random .choice (range (len (X_train )), size = n_initial , replace = False )
52
+ X_initial = X_train [initial_idx ]
57
53
y_initial = y_train [initial_idx ]
58
54
59
55
# generate the pool
60
56
# remove the initial data from the training dataset
61
- x_train = np .delete (x_train , initial_idx , axis = 0 )
62
- y_train = np .delete (y_train , initial_idx , axis = 0 )
63
- # sample random elements from x_train
64
- pool_size = 10000
65
- pool_idx = np .random .choice (range (len (x_train )), pool_size )
66
- x_pool = x_train [pool_idx ]
67
- y_pool = y_train [pool_idx ]
57
+ X_pool = np .delete (X_train , initial_idx , axis = 0 )
58
+ y_pool = np .delete (y_train , initial_idx , axis = 0 )
68
59
69
60
"""
70
61
Training the ActiveLearner
71
62
"""
72
63
73
- # create the classifier
74
- classifier = KerasClassifier (create_keras_model )
75
-
76
64
# initialize ActiveLearner
77
65
learner = ActiveLearner (
78
66
predictor = classifier ,
79
- X_initial = x_initial , y_initial = y_initial ,
67
+ X_initial = X_initial , y_initial = y_initial ,
80
68
verbose = 0
81
69
)
82
70
83
71
# the active learning loop
84
72
n_queries = 10
85
73
for idx in range (n_queries ):
86
- query_idx , query_instance = learner .query (x_pool , n_instances = 200 , verbose = 0 )
74
+ query_idx , query_instance = learner .query (X_pool , n_instances = 200 , verbose = 0 )
87
75
learner .teach (
88
- X = x_pool [query_idx ], y = y_pool [query_idx ],
76
+ X = X_pool [query_idx ], y = y_pool [query_idx ],
89
77
verbose = 0
90
78
)
91
79
# remove queried instance from pool
92
- x_pool = np .delete (x_pool , query_idx , axis = 0 )
80
+ X_pool = np .delete (X_pool , query_idx , axis = 0 )
93
81
y_pool = np .delete (y_pool , query_idx , axis = 0 )
94
82
95
83
# the final accuracy score
96
- print (learner .score (x_test , y_test , verbose = 0 ))
84
+ print (learner .score (X_test , y_test , verbose = 0 ))
0 commit comments