Code a Convolutional Neural Networks in Keras

Benjamin Cho
3 min readOct 10, 2018

I will cover a simple step by step on how to build a simple convolutional neural network from a dataset taken from Kaggle’s digit recognizer. Disclosure I will not be going over how neural networks works that is for another post, but just how to code them up.

Gather and Set up Data

Download the dataset from Kaggle and save it to your local directory.

Import Libraries

You will first need to read in some imports in order to start your coding.

import numpy as np
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split

Set up X and y

After you load the library and data you will need to setup your X and y because there are some preprocessing that needs to be done to each. For your X values, you need to normalize the pixel values. For your y values, you need to one hot encode the classes for your network to predict each class.

# normalize the data
X = X /255.0
# one hot encode y
y = to_categorical(y, num_classes=10)

In order to feed the data into the neural network, the image arrays need to be reshaped first. The shape will be: (number of images, width, height, depth). Below, the -1 value lets numpy determine for you the unknown number of columns or rows in the resulting matrix. After this you will want to train test split your data.

#reshape values to the dimension of images 
X = X.values.reshape(-1, 28, 28, 1)
#train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3 , random_state=42)

Build model

Time to build the model! I first created a function where my model was built, this is not necessary but it allows for ease and cleanliness in my code.

#build function for network
def cnn_network():
model = Sequential()

model.add(Conv2D(32, kernel_size=(5,5), activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(32, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, kernel_size=(5,5), activation='relu'))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(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'))

return model

I have bolded the necessary lines that are required for a model. The structure of the model always starts with model = Sequential(). Then you will want to use model.add for each layer that you will add to your neural network.

The first layer you add in your CNN will have to have the input shape of the image. After that it will be however you want customize your model to work, so in this case this was how I decided to build mine. Without going into how neural networks work, once you have structured your CNN you will want to use Flatten() in order to feed it into a fully connected network. Like your CNN, your dense network can be customized to your liking. Finally, you will want to have a final dense layer with the output number as your number of classes predicting, in this case its 10.

Compile and Fit

#set network to a variable
model_1 = cnn_network()
#compile network
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#fit model
cnn = model_1.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=100, verbose=1)

Next you will want to compile the model with these parameters. Multi-class problems will require categorical crossentropy with a metric of accuracy. The adam optimizer usually works the best but feel free to try other optimizers.

Finally, fit the model with your X_train and y_train. You will want to set the number of epochs which is the number runs your model will conduct. The batch size will be the the number of training examples in one epoch. The larger your epochs size the longer your model will take to run, so careful with this.

To conclude, neural networks are fairly simple to build through Keras once you have a general understanding of the concept. I would say the hardest part of building the network is fine tuning it so that your model most accurately predicts your target.

--

--

Benjamin Cho

Data Science/Business/Project Management Enthusiast