

It allows you to build a model layer by layer. Sequential is the easiest way to build a model in Keras. The model type that we will be using is Sequential. Here is the code: from keras.models import Sequential from keras.layers import Dense, Conv2D, Flatten #create model model = Sequential() #add model layers model.add(Conv2D(64, kernel_size=3, activation=’relu’, input_shape=(28,28,1))) model.add(Conv2D(32, kernel_size=3, activation=’relu’)) model.add(Flatten()) model.add(Dense(10, activation=’softmax’)) from keras.utils import to_categorical #one-hot encode target column y_train = to_categorical(y_train) y_test = to_categorical(y_test) y_train This means that the sixth number in our array will have a 1 and the rest of the array will be filled with 0. For example, we saw that the first image in the dataset is a 5. This means that a column will be created for each output category and a binary variable is inputted for each category.

We need to ‘one-hot-encode’ our target variable. The last number is 1, which signifies that the images are greyscale. Then comes the shape of each image (28x28). The first number is the number of images (60,000 for X_train and 10,000 for X_test). Next, we need to reshape our dataset inputs (X_train and X_test) to the shape that our model expects when we train the model. 28 x 28 is also a fairly small size, so the CNN will be able to run over each image pretty quickly. When using real-world datasets, you may not be so lucky. import matplotlib.pyplot as plt #plot the first image in the dataset plt.imshow(X_train)īy default, the shape of every image in the mnist dataset is 28 x 28, so we will not need to check the shape of all the images. We will plot the first image in our dataset and check its size using the ‘shape’ function. Now let’s take a look at one of the images in our dataset to see what we are working with. from keras.datasets import mnist #download mnist data and split into train and test sets (X_train, y_train), (X_test, y_test) = mnist.load_data() Exploratory data analysis When we load the dataset below, X_train and X_test will contain the images, and y_train and y_test will contain the digits that those images represent.

Out of the 70,000 images provided in the dataset, 60,000 are given for training and 10,000 are given for testing. The mnist dataset is conveniently provided to us as part of the Keras library, so we can easily load the dataset. We will attempt to identify them using a CNN. This dataset consists of 70,000 images of handwritten digits from 0–9. In this tutorial, we will use the popular mnist dataset.
