Deep Learning – Classification Example

Hello Everyone!!! Its an immense pleasure to write today as this is the first post I am able to write in 2021. Happy New Year!!! 🥳 🎂 🎉 In this article we are going to see the continuation of Deep Learning techniques. We are going to see an Deep Learning model with a Classification Example.

In our last article, we learned about use a simple Neural Network to solve regression problems – Artificial Neural Network Explained with an Regression Example.

If you missed the prequels, please check below:

Artificial Intelligence – A brief Introduction and history of Deep Learning

How Neurons work? and How Artificial Neuron mimics neurons of human brain?

Artificial Neural Network Explained with an Regression Example

Classification Example Dataset:

For easy use and easy to download, the breast cancer dataset from sklearn.datasets has been taken.

Finally we have the data set of 398 records for training and 171 records for testing with 30 features. Even though this is a very less data for deep learning, we are using this for experimental purpose.

Documentation of dataset: http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html#sklearn.datasets.load_breast_cancer

Model Creation:

A sequential model is created. Lets add deep learning layers to it.

Here it can be noticed the input layer is having input shape of 30. And also 10 is mentioned in first line itself.

Now most of us may have a question, which is the exact input shape? 30 or 10?

The first layer that gets input is the input layer which is having 30 neurons here. each 1 input node is for each feature. the next layer is the first hidden layer which is having 10 neurons.

Image by Author – Input layer and first hidden layer

Also we can see that the output layer is having only 1 neuron. Why it is not having 2 neurons as the target variable is a binary class (1- malign, 0 – benign)?

This one neuron output value will be the probability value between 0 and 1 as we are using sigmoid activation in output layer. The probability value can be determined as 0 or 1n based on the threshold of 0.5.

Image by Author – Last hidden layer and output layer

Now we have constructed the neural network. Lets compile it with optimizers and loss function.

Stochastic gradient descent is used as an optimizer which will be used when Back propagation and “binary_crossentropy” loss function is used for evaluation.

To get the entire structure as a summary, we can use summary() function of the model.

This summary helps us to understand the total number of weights and biases. Total params: 792.0

Train the Model:

The above code will train the model in 50 batches and 100 epochs.

1 iteration – A data point or a set of data points sent to a neural network once.

batch_size = 50. That means the entire training data points will be divided and for each iteration 50 data points will be given in bulk.

epochs = 100. One epoch mentions that the entire data set is sent to training 1 time. I.e. The neural network will trained with 398 datapoints for 100 times. Each time the weights will be updated using Backpropagation.

So that the final weights will be the optimized approximately. Increasing the number of epochs may or may not optimize it based on the data set, learning rate, activation functions, etc.

As we have given verbose as 1, we will be able to see the accuracy and loss in each epoch.

Image by Author – Logs of Model training

Here you can see that we higher loss and less accuracy.

Evaluation:

The model’s evaluation function. The documentation can be found here https://keras.io/metrics/.

Now we have the results. This object will have 2 metrics Loss and Accuracy.

To get a clear view about what happened in the whole training process, we can use the object received from model.fit statement. We named it as history.

History of the training process

We can use matplotlib for visualization:

If we have added validation split in fit function, we will get both training and validation results.

You can see that the validation and training accuracy is not improved in this new model. We used the same training data for validation split also. So the training data was not enough.

Conclusion:

Now you may think that, all that CPU time and GPU time was wasted for less accuracy? Even ML model gave better results!!!

Yes, I too thought the same when I see this accuracy for first time.

We haven’t done one thing yet which is the interesting part of model creation. Yes! Tuning!

This is a base model. We can still tune this model with different number of neurons, layers, epochs, different activation functions, optimizers and loss functions.

Also we can increase the data set to get better results.

I am planning to post an article to tune this model. Will see you in next post.

Thank you for reading our article and hope you enjoyed it. 😊

Like to support? Just click the like button ❤️.

Happy Learning! 👩‍💻

0

2 Replies to “Deep Learning – Classification Example”

Leave a Reply

Your email address will not be published. Required fields are marked *