deep-learning-model-header

Building a Deep Learning Model

Building a Deep Learning Model using Keras

In this post, let us see how to build a deep learning model using Keras. If you haven’t installed Tensorflow and Keras, I will show the simple way to install these two modules.

1. Installing Tensorflow and Keras

  • Open Anaconda Navigator. Under Environments, create new environment in Python 3.6.

    deep-learning-model-1

    Create a new environment in Anaconda- Python 3.6

    deep-learning-model-2

    Created a new environment

  • Open terminal and pip install Tensorflow and Keras

    deep-learning-model-3

    Open Terminal

    deep-learning-model-4

    Use the appropriate option for your computer (From Tensorflow website)

  • pip install tensorflow

    deep-learning-model-5

    install Tensorflow

  • then, pip install keras

    deep-learning-model-6

    install Keras

  • Now install Jupyter Notebook on this environment and launch

    deep-learning-model-7

    Now install Jupyter Notebook and launch

  • import tensorflow and keras

    deep-learning-model-8

    Mission successful!

2. Building Model using Keras

2.1 Four steps

Keras is simple to use. There are four steps in building a neural network model in Keras.

  • Define the architecture
  • Compile
  • Fit
  • Predict
deep-learning-model-9

2.2 Four steps in detail

As you can see, first we have imported libraries.

a) Define the architecture

In sequential model, we can stack the layers. In the above example, we have added a hidden layer with 64 nodes and ReLU activation function. In addition, we also specify number of inputs or the input dimension (input_dim=20). If you are using input_shape then it will be (20,). And both denote the same thing. We have added output layer with 10 output nodes-one for each class of the output with softmax activation function as this is a multiple classification model.

b) Compile

Once the model has been specified, we need to compile the model. There are three arguments in compiling step:

  • Loss function,
  • Optimizer and
  • Metrics

b1) Loss function

In case of Loss function, commonly used are the

  • mean_squared_error: commonly used for regression tasks
  • mean_absolute_percentage_error
  • categorical_crossentropy (Used for classification problems where y is one-hot-encoded, if not we can use to_categorical option to one-hot-encode y)

For more loss functions, refer this page.

b2) Optimizer

Commonly used optimizers are

  • ‘sgd’ (Stochastic gradient descent)
  • ‘adam’ (adam stands for adaptive moment estimation)

We can tune the learning rates, decay, momentum etc for better performance.

b3) Metrics

And in case metrics to measure the performance of the model, commonly used is the ‘accuracy’

c) Fit

In the third step, we are fitting this model to the data. We can specify validation splits, epochs, early stopping monitor as shown below.

model.fit(X, target, validation_split=0.3, epochs=30, callbacks=[early_stopping_monitor])

d) Predict

And finally we are predicting the classes for new test data.

3. References

  1. https://keras.io/ has all the resources to learn Keras.
  2. For installing Tensorflow and Keras, refer https://towardsdatascience.com/python-environment-setup-for-deep-learning-on-windows-10-c373786e36d1
  3. Also you may refer this page for installing Keras
  4. Excellent blogpost series on training neural networks https://towardsdatascience.com/how-do-we-train-neural-networks-edd985562b73
Scroll to Top