Keras Sequential Api



Neural Networks and Deep Learning are behind many of the major breakthroughs in the field of Computer Vision, Speech Recognition and Image Processing. Keras is a frontend framework, written in python, that was made to be used on top on other Deep Learning frameworks like Tensorflow and Theano. It is a high-level tool for coding and training neural networks. You can think of it as a set of building blocks that you can combine to create neural networks.

Keras is flexible. It lets you choose between currently Google’s TensorFlow or the University of Montreal’s Theano as the library to power your neural networks, as the backend framework.

How Keras is configured?

A Neural Network, containing two hidden layers.

A neural network is a machine-learning algorithm made up of individual nodes called neurons. These nodes, or neurons, are arranged into a series of groups called layers. Nodes in each layer are connected to nodes in the following layer. Data flows from the input to the output along these connections.

Step 1- Create a Model

The easiest way to build a neural network is to use the Sequential model Api. It’s called the sequential model API because you first create an empty model object, and then you add layers to it one after another in sequence.

Keras-Sequential-Api

In the above example, we first create a Sequential model object. Then, we can add as many layers as we want by calling model.add and passing in a new layer object. In this case, we are adding a new densely connected layer of 32 nodes to the neural network. A densely connected layer is one where every node is connected to every node in the previous layer, and since this is the very first layer in the neural network, we also have to tell it how many input nodes there are by passing in input dim=9. We can continue adding layers the same way. This line adds another layer with 128 densely connected nodes, and this line will add the final layer with one output node.

Step 2- Compile

The final step of defining a model is to compile it. That’s when Keras actually builds a tensor flow model for us behind the scenes.

Compiling our Keras Model

When we compile the model, we need to tell Keras two important things. First, we need to tell it how we want to measure the accuracy of each prediction made by the model during the training process. This is called the loss function. Keras lets us choose from several standard loss functions or define our own.

Step 3- Training

Keras lets us select from several popular optimizer algorithms. Now we’re ready to start the training phase.

Training our model

To train the model, we call model.fit and pass in the training data and the expected output for the training data. Keras will run the training process and print out the progress to the console. When training completes, it will report the final accuracy that was achieved with the training data. Once the model is trained, we’re ready for the testing phase.

step 4- Testing

Testing the Model

We can test the model by calling model.evaluate and passing in the testing data set and the expected output. When we are happy with the accuracy of the system, we can save the training model to a file. To do that, we call model.save and pass in the file name.

Saving the model

This file will contain everything we need to use our model in another program. Now that we have a trained model, we’re ready for the evaluation phase. We’ll load our previously trained model by calling the load model function and passing in a file name. And then, to use the model to make new predictions, we just call the predict function and pass in the new data we want predictions for.

And that’s the basic flow in using Keras. In the next Post, we’ll go through this flow in more detail and build a working neural network.

Source: Deep Learning on Medium