Created
August 4, 2017 14:58
-
-
Save bkj/b6efca1a9acc7dafe27d1688d617eecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
mnist_cnn.py | |
""" | |
from __future__ import print_function | |
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras import backend as K | |
# -- | |
# Params | |
batch_size = 128 | |
num_classes = 10 | |
epochs = 12 | |
img_rows, img_cols = 28, 28 | |
# -- | |
# IO | |
(X_train, y_train), (X_test, y_test) = mnist.load_data() | |
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) | |
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) | |
input_shape = (img_rows, img_cols, 1) | |
X_train = X_train.astype('float32') / 255 | |
X_test = X_test.astype('float32') / 255 | |
y_train = keras.utils.to_categorical(y_train, num_classes) | |
y_test = keras.utils.to_categorical(y_test, num_classes) | |
# -- | |
# Define model | |
model = Sequential() | |
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape)) | |
model.add(Conv2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(num_classes, activation='softmax')) | |
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) | |
fitist = model.fit( | |
X_train, y_train, | |
batch_size=batch_size, | |
epochs=epochs, | |
verbose=1, | |
validation_data=(X_test, y_test) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment