Skip to content

Commit 949f571

Browse files
committed
added early stopping
1 parent d768787 commit 949f571

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

train_CNN.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from keras.preprocessing.image import ImageDataGenerator
2323
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
2424
from keras.preprocessing.image import array_to_img, img_to_array, load_img
25+
from keras.callbacks import EarlyStopping
26+
from keras.optimizers import SGD
2527
import time
2628

2729
# Set parameters
@@ -86,15 +88,21 @@
8688
model.add(Dense(64)) #64
8789
model.add(Activation('relu'))
8890
model.add(Dropout(0.5))
89-
model.add(Dense(int(classes_amount), activation='softmax')) #Output dimension
91+
model.add(Dense(int(classes_amount)) #Output dimension
92+
model.add(Activation('softmax'))
9093
#model.add(Dense(1))
9194
#model.add(Activation('sigmoid')) #only for binary classes
9295

96+
97+
#sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
98+
9399
# categorical_crossentropy for more that 2 classes. binary_crossentropy otherwise
94100
model.compile(loss='categorical_crossentropy',
95101
optimizer='rmsprop', #rmsprop
96102
metrics=['accuracy'])
97103

104+
early_stop = EarlyStopping(monitor='val_loss', patience=4)
105+
98106
batch_size = 16
99107
nb_epoch = 30
100108
nb_train_samples = 232*classes_amount # old_data == 283,data ==214
@@ -105,7 +113,8 @@
105113
steps_per_epoch=nb_train_samples / batch_size,
106114
epochs=nb_epoch,
107115
validation_data=validation_generator,
108-
validation_steps=nb_validation_samples / batch_size)
116+
validation_steps=nb_validation_samples / batch_size,
117+
callbacks=[early_stop])
109118

110119
# Save Model
111120
model_json = model.to_json()

0 commit comments

Comments
 (0)