text_plain_output_1.png
from tensorflow import keras

x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255
x_val = x_val.reshape(-1, 28, 28, 1).astype('float32') / 255

y_train = keras.utils.to_categorical(y_train)
y_val = keras.utils.to_categorical(y_val)

model = keras.models.Sequential([keras.layers.Conv2D(32, kernel_size=3, activation='relu', input_shape=(28, 28, 1)), keras.layers.BatchNormalization(), keras.layers.Conv2D(32, kernel_size=3, activation='relu'), keras.layers.BatchNormalization(), keras.layers.Conv2D(32, kernel_size=5, strides=2, padding='same', activation='relu'), keras.layers.BatchNormalization(), keras.layers.Dropout(rate=0.4), keras.layers.Conv2D(64, kernel_size=3, activation='relu'), keras.layers.BatchNormalization(), keras.layers.Conv2D(64, kernel_size=3, activation='relu'), keras.layers.BatchNormalization(), keras.layers.Conv2D(64, kernel_size=5, strides=2, padding='same', activation='relu'), keras.layers.BatchNormalization(), keras.layers.Dropout(rate=0.4), keras.layers.Flatten(), keras.layers.Dense(128, activation='relu'), keras.layers.BatchNormalization(), keras.layers.Dropout(rate=0.4), keras.layers.Dense(10, activation='softmax')])

model.summary()

datagen = keras.preprocessing.image.ImageDataGenerator(zoom_range=0.1, height_shift_range=0.1, width_shift_range=0.1, rotation_range=10)
datagen.fit(x_train)

model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Adam(lr=0.0001), metrics=['accuracy'])

batch_size = 32
epochs = 25
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, verbose=2, validation_data=(x_val, y_val), steps_per_epoch=x_train.shape[0] // batch_size)