Created
September 22, 2018 13:32
-
-
Save shiblisec/327a4a86e065ca2062247477641c3bab to your computer and use it in GitHub Desktop.
MNIST DATASET CNN KERAS
This file contains hidden or 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
import tensorflow as tf | |
batch_size = 128 | |
no_classes = 10 | |
epochs = 2 | |
image_height, image_width = 28, 28 | |
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() | |
x_train = x_train.reshape(x_train.shape[0], image_height, image_width, 1) | |
x_test = x_test.reshape(x_test.shape[0], image_height, image_width, 1) | |
input_shape = (image_height, image_width, 1) | |
x_train = x_train.astype('float32') | |
x_test = x_test.astype('float32') | |
x_train /= 255 | |
x_test /= 255 | |
y_train = tf.keras.utils.to_categorical(y_train, no_classes) | |
y_test = tf.keras.utils.to_categorical(y_test, no_classes) | |
def simple_cnn(input_shape): | |
model = tf.keras.models.Sequential() | |
model.add(tf.keras.layers.Conv2D( | |
filters=64, | |
kernel_size=(3, 3), | |
activation='relu', | |
input_shape=input_shape | |
)) | |
model.add(tf.keras.layers.Conv2D( | |
filters=128, | |
kernel_size=(3, 3), | |
activation='relu' | |
)) | |
model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2))) | |
model.add(tf.keras.layers.Dropout(rate=0.3)) | |
model.add(tf.keras.layers.Flatten()) | |
model.add(tf.keras.layers.Dense(units=1024, activation='relu')) | |
model.add(tf.keras.layers.Dropout(rate=0.3)) | |
model.add(tf.keras.layers.Dense(units=no_classes, activation='softmax')) | |
model.compile(loss=tf.keras.losses.categorical_crossentropy, | |
optimizer=tf.keras.optimizers.Adam(), | |
metrics=['accuracy']) | |
return model | |
simple_cnn_model = simple_cnn(input_shape) | |
simple_cnn_model.fit(x_train, y_train, batch_size, epochs, (x_test, y_test)) | |
train_loss, train_accuracy = simple_cnn_model.evaluate( | |
x_train, y_train, verbose=0) | |
print('Train data loss:', train_loss) | |
print('Train data accuracy:', train_accuracy) | |
test_loss, test_accuracy = simple_cnn_model.evaluate( | |
x_test, y_test, verbose=0) | |
print('Test data loss:', test_loss) | |
print('Test data accuracy:', test_accuracy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment