Skip to content

Instantly share code, notes, and snippets.

@Mcilie
Last active December 31, 2018 02:42
Show Gist options
  • Save Mcilie/d0535b972aacbe909ac07b7ca31d98f6 to your computer and use it in GitHub Desktop.
Save Mcilie/d0535b972aacbe909ac07b7ca31d98f6 to your computer and use it in GitHub Desktop.
CNN in keras
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import *
import numpy as np
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train)
x_test = np.expand_dims(x_test, -1)
x_train = np.expand_dims(x_train, -1)
print(x_train)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', data_format = "channels_last", input_shape = (28,28,1)))
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(10, activation='softmax'))
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)
model.compile(loss = keras.losses.categorical_crossentropy, optimizer = keras.optimizers.Adadelta(), metrics = ['accuracy'])
model.fit(x_train,y_train,
batch_size = 64,
epochs = int(input("Yo fam, epochs pls: ")),
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