Deep Learning is easy, do it!

Mokhles El Heni
GoMyCode
Published in
3 min readMar 25, 2018

In this post we will discover how to use Keras to create a Deep Neural Network in few lines. Yes a “Deep neural Network” that buzzy topic that everyone is talking about these days.

“Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano*. It was developed with a focus on enabling fast experimentation. Being able to go from idea to result with the least possible delay is key to doing good research.”

*TensorFlow, CNTK, and Theano are other Machine Learning libraries and the fact that Keras is built on top of them means that it is easier to use.

So as Keras website said, let’s start with the idea (The problem that we want to solve) and use Keras to get results.

In this post we’ll try to solve the famous problem of identifing hand-written digits.

In fact what we will do here is applicable for most problems that have data as input and a goal of predicting which class this input belongs to.

MNIST Dataset

Let’s code

First of all we need to setup Tensorflow and Keras (and some other libraries). Follow these links:

Don’t forget to setup Python3 first.

Read data

Every ML problem starts with reading data. Keras comes with a built-in function to load many famous datasets (MNIST, IRIS ..)

We need to divide our data into training data and testing data. The first is for training our neural net and the second for testing it later.

from keras.datasets import mnist(X_train, y_train), (X_test, y_test) = mnist.load_data()

We devide our data to Xs and Ys: X are the images’ pixels and Y are the targets (0, 1, 2 … 9)

As our problem is a classification, it’s better to transform our Ys to vectors of 10 columns [Xi] where Xi = 1 if i == Y, 0 otherwise.

For example 2 will be transformed to [0, 0, 1, 0, 0 … ]

from keras.utils import np_utilsY_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

Create the Neural Network

from keras.models import Sequential
from keras.layers import Dense, Flatten
model = Sequential()model.add(Flatten(input_shape=(28,28))) # Flatten the image to a vector of 784 itemmodel.add(Dense(100, activation=’relu’)) # layer 1
model.add(Dense(50, activation=’relu’)) # layer 2
model.add(Dense(10, activation=’sigmoid’)) # layer 3
model.compile(optimizer=’rmsprop’, loss=’binary_crossentropy’, metrics=[‘accuracy’])model.fit(X_train, Y_train, epochs=10, batch_size=32)

What we did here is stacking layers, first one transform the 28*28 pixel image to a vector of 784 columns.

Then we create a neural net with 3 layers (that’s why in fact it called deep NN, because we have a “depth” of 3 (and 3> 1) so yeah it’s deep we can add more and make it deeper)

The .fit() function train our network to make it predict future inputs.

Predict

Now we can use our model to predict what an image stands for.

If you noticed we devided our data to _train and _test, now we will use the test data to make sure that our model works fine.

We will select a random item of the test data and check if the model returns the write digit.

select = 5321 # a random number of the test data
x = X_test[select].reshape(1, 28, 28)
plt.imshow(X_test[select], cmap=plt.get_cmap('gray'))
model.predict(x)
Some code to print the digit image and the pridiction result

We can see that the first column of our vector has the biggest value which is close to 1 (0, 9837…). The other ones are really close to 0.

What’s next

After trying this code you should start trying to understand whats happing by studying Neural Networks, python and discover more type of layers that Keras provides.

At @GoMyCode we are using AI to disrupt the way we learn and teach Computer science. We are always looking for passionate developers to grow our team. Send us an email to yahya@gomycode.tn if you are interested in joining our team.

--

--