Deep Learning with Keras – Training the Model The model training is done in one single method call called fit that takes few parameters as seen in the code below − history = model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=2, validation_data=(X_test, Y_test))) The first two parameters to the fit method specify the features and the output of the training dataset. The epochs is set to 20; we assume that the training will converge in max 20 epochs – the iterations. The trained model is validated on the test data as specified in the last parameter. The partial output of running the above command is shown here − Train on 60000 samples, validate on 10000 samples Epoch 1/20 – 9s – loss: 0.2488 – acc: 0.9252 – val_loss: 0.1059 – val_acc: 0.9665 Epoch 2/20 – 9s – loss: 0.1004 – acc: 0.9688 – val_loss: 0.0850 – val_acc: 0.9715 Epoch 3/20 – 9s – loss: 0.0723 – acc: 0.9773 – val_loss: 0.0717 – val_acc: 0.9765 Epoch 4/20 – 9s – loss: 0.0532 – acc: 0.9826 – val_loss: 0.0665 – val_acc: 0.9795 Epoch 5/20 – 9s – loss: 0.0457 – acc: 0.9856 – val_loss: 0.0695 – val_acc: 0.9792 The screenshot of the output is given below for your quick reference − Now, as the model is trained on our training data, we will evaluate its performance.
Category: deep Learning With Keras
Deep Learning with Keras – Compiling the Model The compilation is performed using one single method call called compile. model.compile(loss=”categorical_crossentropy”, metrics=[”accuracy”], optimizer=”adam”) The compile method requires several parameters. The loss parameter is specified to have type ”categorical_crossentropy”. The metrics parameter is set to ”accuracy” and finally we use the adam optimizer for training the network. The output at this stage is shown below − Now, we are ready to feed in the data to our network. Loading Data As said earlier, we will use the mnist dataset provided by Keras. When we load the data into our system, we will split it in the training and test data. The data is loaded by calling the load_data method as follows − (X_train, y_train), (X_test, y_test) = mnist.load_data() The output at this stage looks like the following − Now, we shall learn the structure of the loaded dataset. The data that is provided to us are the graphic images of size 28 x 28 pixels, each containing a single digit between 0 and 9. We will display the first ten images on the console. The code for doing so is given below − # printing first 10 images for i in range(10): plot.subplot(3,5,i+1) plot.tight_layout() plot.imshow(X_train[i], cmap=”gray”, interpolation=”none”) plot.title(“Digit: {}”.format(y_train[i])) plot.xticks([]) plot.yticks([]) In an iterative loop of 10 counts, we create a subplot on each iteration and show an image from X_train vector in it. We title each image from the corresponding y_train vector. Note that the y_train vector contains the actual values for the corresponding image in X_train vector. We remove the x and y axes markings by calling the two methods xticks and yticks with null argument. When you run the code, you would see the following output − Next, we will prepare data for feeding it into our network.
Evaluating Model Performance To evaluate the model performance, we call evaluate method as follows − loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2) To evaluate the model performance, we call evaluate method as follows − loss_and_metrics = model.evaluate(X_test, Y_test, verbose=2) We will print the loss and accuracy using the following two statements − print(“Test Loss”, loss_and_metrics[0]) print(“Test Accuracy”, loss_and_metrics[1]) When you run the above statements, you would see the following output − Test Loss 0.08041584826191042 Test Accuracy 0.9837 This shows a test accuracy of 98%, which should be acceptable to us. What it means to us that in 2% of the cases, the handwritten digits would not be classified correctly. We will also plot accuracy and loss metrics to see how the model performs on the test data. Plotting Accuracy Metrics We use the recorded history during our training to get a plot of accuracy metrics. The following code will plot the accuracy on each epoch. We pick up the training data accuracy (“acc”) and the validation data accuracy (“val_acc”) for plotting. plot.subplot(2,1,1) plot.plot(history.history[”acc”]) plot.plot(history.history[”val_acc”]) plot.title(”model accuracy”) plot.ylabel(”accuracy”) plot.xlabel(”epoch”) plot.legend([”train”, ”test”], loc=”lower right”) The output plot is shown below − As you can see in the diagram, the accuracy increases rapidly in the first two epochs, indicating that the network is learning fast. Afterwards, the curve flattens indicating that not too many epochs are required to train the model further. Generally, if the training data accuracy (“acc”) keeps improving while the validation data accuracy (“val_acc”) gets worse, you are encountering overfitting. It indicates that the model is starting to memorize the data. We will also plot the loss metrics to check our model’s performance. Plotting Loss Metrics Again, we plot the loss on both the training (“loss”) and test (“val_loss”) data. This is done using the following code − plot.subplot(2,1,2) plot.plot(history.history[”loss”]) plot.plot(history.history[”val_loss”]) plot.title(”model loss”) plot.ylabel(”loss”) plot.xlabel(”epoch”) plot.legend([”train”, ”test”], loc=”upper right”) The output of this code is shown below − As you can see in the diagram, the loss on the training set decreases rapidly for the first two epochs. For the test set, the loss does not decrease at the same rate as the training set, but remains almost flat for multiple epochs. This means our model is generalizing well to unseen data. Now, we will use our trained model to predict the digits in our test data.
Deep Learning with Keras Tutorial Job Search Deep Learning essentially means training an Artificial Neural Network (ANN) with a huge amount of data. In deep learning, the network learns by itself and thus requires humongous data for learning. In this tutorial, you will learn the use of Keras in building deep neural networks. We shall look at the practical examples for teaching. Audience This tutorial is prepared for professionals who are aspiring to make a career in the field of deep learning and neural network framework. This tutorial is intended to make you comfortable in getting started with the Keras framework concepts. Prerequisites Before proceeding with the various types of concepts given in this tutorial, we assume that the readers have basic understanding of deep learning framework. In addition to this, it will be very helpful, if the readers have a sound knowledge of Python and Machine Learning.
Creating Deep Learning Model Our neural network model will consist of a linear stack of layers. To define such a model, we call the Sequential function − model = Sequential() Input Layer We define the input layer, which is the first layer in our network using the following program statement − model.add(Dense(512, input_shape=(784,))) This creates a layer with 512 nodes (neurons) with 784 input nodes. This is depicted in the figure below − Note that all the input nodes are fully connected to the Layer 1, that is each input node is connected to all 512 nodes of Layer 1. Next, we need to add the activation function for the output of Layer 1. We will use ReLU as our activation. The activation function is added using the following program statement − model.add(Activation(”relu”)) Next, we add Dropout of 20% using the statement below. Dropout is a technique used to prevent model from overfitting. model.add(Dropout(0.2)) At this point, our input layer is fully defined. Next, we will add a hidden layer. Hidden Layer Our hidden layer will consist of 512 nodes. The input to the hidden layer comes from our previously defined input layer. All the nodes are fully connected as in the earlier case. The output of the hidden layer will go to the next layer in the network, which is going to be our final and output layer. We will use the same ReLU activation as for the previous layer and a dropout of 20%. The code for adding this layer is given here − model.add(Dense(512)) model.add(Activation(”relu”)) model.add(Dropout(0.2)) The network at this stage can be visualized as follows − Next, we will add the final layer to our network, which is the output layer. Note that you may add any number of hidden layers using the code similar to the one which you have used here. Adding more layers would make the network complex for training; however, giving a definite advantage of better results in many cases though not all. Output Layer The output layer consists of just 10 nodes as we want to classify the given images in 10 distinct digits. We add this layer, using the following statement − model.add(Dense(10)) As we want to classify the output in 10 distinct units, we use the softmax activation. In case of ReLU, the output is binary. We add the activation using the following statement − model.add(Activation(”softmax”)) At this point, our network can be visualized as shown in the below diagram − At this point, our network model is fully defined in the software. Run the code cell and if there are no errors, you will get a confirmation message on the screen as shown in the screenshot below − Next, we need to compile the model.
Deep Learning with Keras – Introduction Deep Learning has become a buzzword in recent days in the field of Artificial Intelligence (AI). For many years, we used Machine Learning (ML) for imparting intelligence to machines. In recent days, deep learning has become more popular due to its supremacy in predictions as compared to traditional ML techniques. Deep Learning essentially means training an Artificial Neural Network (ANN) with a huge amount of data. In deep learning, the network learns by itself and thus requires humongous data for learning. While traditional machine learning is essentially a set of algorithms that parse data and learn from it. They then used this learning for making intelligent decisions. Now, coming to Keras, it is a high-level neural networks API that runs on top of TensorFlow – an end-to-end open source machine learning platform. Using Keras, you easily define complex ANN architectures to experiment on your big data. Keras also supports GPU, which becomes essential for processing huge amount of data and developing machine learning models. In this tutorial, you will learn the use of Keras in building deep neural networks. We shall look at the practical examples for teaching. The problem at hand is recognizing handwritten digits using a neural network that is trained with deep learning. Just to get you more excited in deep learning, below is a screenshot of Google trends on deep learning here − As you can see from the diagram, the interest in deep learning is steadily growing over the last several years. There are many areas such as computer vision, natural language processing, speech recognition, bioinformatics, drug design, and so on, where the deep learning has been successfully applied. This tutorial will get you quickly started on deep learning. So keep reading!