Building deep learning model

In this tutorial I am going to teach you how you can building deep learning model. The tutorial includes, the concept of feature extraction, padding, classifier layer, convolution, optimizer, batch sizes, learning rates and more concepts related to deep learning model. Before proceeding to this tutorial, I recommend you to visit my previous post related to Deep learning, back propagation

concepts related to deep learning model

Data annotation in building deep learning model

Annotation using flow_from_directory()

Data labeling in deep learning

This method is useful when the images are sorted and placed in there respective class/label folders. This method will identify classes automatically from the folder name

Feature extraction

Feature extraction in deep learning

Learning rate in building deep learning model

In machine learning and statistics, the learning rate is a tuning parameter in an optimization algorithm that determines the step size at each iteration while moving toward a minimum of a loss function.

Learning rate in building deep learning model
Different value of learning rate
  • Learning rate may b
  • Moderate
  • Too high

Too small learning rate slows down training speed. We use it when our sample batch size is small. Moderate learning rate can be used for small batch size and bigger batch size. Too high learning rate speed up training speed. We use this if our sample batch is to high

Type of learning rate in building deep learning model

Optimizes in building deep learning model

•RMSProp

•Adagrad

•Adam

•Adamax

optimizer in building deep learning model

Model evaluation metrics in building deep learning model

Let us consider a task to classify whether a person is pregnant or not pregnant. If the test for pregnancy is positive (+ve ), then the person is pregnant. On the other hand, if the test for pregnancy is negative (-ve) then the person is not pregnant.

A person who is actually pregnant (positive) and classified as pregnant (positive). This is called TRUE POSITIVE (TP).

True positive pregnant

A person who is actually not pregnant (negative) and classified as not pregnant (negative). This is called TRUE NEGATIVE (TN)

TN pregnant

A person who is actually not pregnant (negative) and classified as pregnant (positive). This is called FALSE POSITIVE (FP).

FP pregnant

A person who is actually pregnant (positive) and classified as not pregnant (negative). This is called FALSE NEGATIVE (FN).

FN pregnant

Confusion matrix in building deep learning model

For imbalanced classification problems, the majority class is typically referred to as the negative outcome (e.g. such as “no change” or “negative test result“), and the minority class is typically referred to as the positive outcome (e.g. “change” or “positive test result”).

The confusion matrix provides more insight into not only the performance of a predictive model, but also which classes are being predicted correctly, which incorrectly, and what type of errors are being made.

The simplest confusion matrix is for a two-class classification problem, with negative (class 0) and positive (class 1) classes.

Confusion matrix in deep learning model

What we desire is TRUE POSITIVE and TRUE NEGATIVE but due to the misclassifications, we may also end up in FALSE POSITIVE and FALSE NEGATIVE. So there is a confusion in classifying whether a person is pregnant or not. This is because no machine learning algorithm is perfect. Soon we will describe this confusion in classifying the data in a matrix called confusion matrix.

Confusion matrix
result of confusion matrix
confusion matrix for pregnant identification

Accuracy

Accuracy represents the number of correctly classified data instances over the total number of data instances. Accuracy is not a good metric when the data set is unbalanced. Using accuracy in such scenarios can result in misleading interpretation of results.

In this example, Accuracy = (55 + 30)/(55 + 5 + 30 + 10 ) = 0.85 and in percentage the accuracy will be 85%.

Accuracy may not be a good measure if the dataset is not balanced (both negative and positive classes have different number of data instances).

Precision

It should ideally be 1 (high) for a good classifier. Precision becomes 1 only when the numerator and denominator are equal i.e TP = TP +FP, this also means FP is zero. As FP increases the value of denominator becomes greater than the numerator and precision value decreases (which we don’t want).

precision

So in the pregnancy example, precision = 30/(30+ 5) = 0.857

Recall

It is also known as sensitivity or true positive rate and is defined as follows:

recall

Recall should ideally be 1 (high) for a good classifier. Recall becomes 1 only when the numerator and denominator are equal i.e TP = TP +FN, this also means FN is zero. As FN increases the value of denominator becomes greater than the numerator and recall value decreases (which we don’t want).

So in the pregnancy example let us see what will be the recall.

Recall = 30/(30+ 10) = 0.75

F1-Score

Ideally in a good classifier, we want both precision and recall to be one which also means FP and FN are zero. Therefore we need a metric that takes into account both precision and recall. F1-score is a metric which takes into account both precision and recall and is defined as follows:

fscore in deep learning

F1 score becomes high only when both precision and recall are high. F1 score is the harmonic mean of precision and recall and is a better measure than accuracy.

In the pregnancy example, F1 Score = 2* ( 0.857 * 0.75)/(0.857 + 0.75) = 0.799.

Macro, Micro and Weighted average

Consider the following classification report and confusion matrix

Per class f-score in deep learning model
Confusion matrix of a model

In the case of multi-class classification, we adopt averaging methods for F1 score calculation, resulting in a set of different average scores (macro, weighted, micro) in the classification report.

Macro average

The macro-averaged F1 score (or macro F1 score) is computed using the arithmetic mean (unweighted mean) of all the per-class F1 scores. This method treats all classes equally regardless of their support values.

Macro average
Macro average in classification report

Micro Average

Micro averaging computes a global average F1 score by counting the sums of the True Positives (TP), False Negatives (FN), and False Positives (FP). We first sum the respective TP, FP, and FNvalues across all classes and then plug them into the F1 equation to get our micro F1 score.

Micro Average
micro average of the f score

Weighted average

The weighted-averaged F1 score is calculated by taking the mean of all per-class F1 scores while considering each class’s support. Support refers to the number of actual occurrences of the class in the dataset. For example, the support value of 1 in Boat means that there is only one observation with an actual label of Boat.

weighrted average of deep learning model
weighted average

Thank you for reading. For more information about our service visit this site.

Leave a Reply

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