### Blog: Logistic Regression as a Neural Network

Logistic regression is a statistical method which is used for prediction when the dependent variable or the output is categorical. It is used when we want to know whether a particular data point belongs to class 0 or class 1. In logistic regression, we need to find* the *probability that the output will be *y=1* given an input vector *x**.* ** y’** is the predicted value when the input is

**. Mathematically it can be defined as :**

*x*### Mathematical Model

**Input**: *X** *is an input matrix of dimensions* **n** *x *m** *where *n* is the number of features in *X** *and ** m** is the number of training examples.

**Parameters**: *W** *is a Weight Matrix of dimensions ** n **x

**where**

*1**n*is the number of features in

**. Bias**

*X***helps in controlling the value at which the activation function will trigger.**

*b***Output**:

### Activation Function

Activation functions are really important for an Artificial Neural Network to learn and make sense of something really complicated. They introduce non-linear properties to the network. Their main purpose is to convert an input signal of a node into an output signal. That output signal now is used as an input in the next layer of the Neural Network. The activation used above is the *sigmoid** *activation function. Mathematically, it can be defined as:

### Loss Function

Loss can be defined as the error that is present between the actual output and the predicted output. We want the value of loss function to be as low as possible as it would reduce the loss and the predicted value would be close to the actual value. The loss function that we use to train the neural network varies from case to case. Therefore it is important to select a proper loss function for our use case so that the neural network is trained properly. The loss function which we are going to use for logistic regression can be mathematically defined as:

Let us study why this loss function is good for logistic regression,

- When
*y=1**L(y’,y) = -log y’**.*As we want the value of loss function to be less, the value of*log y’*be more, which will be more whenwill be more i.e close to 1 and therefore the predicted value and actual value will be similar.*y’* - When
the loss function equates to*y=0*. As we want the value of loss function to be less, the value of*L(y’,y) = -log(1-y’)*should be more, which will be more when*log (1-y’)*will be less i.e close to 0 and therefore the predicted value and actual value will be similar.*y’* - The above loss function is convex which means that it has a single global minimum and the network won’t be stuck in local minimum(s) which are present in non-convex loss functions.

### Cost Function

The loss function is used for each and every input training example during the training process whereas the cost function is used for the whole training dataset in one iteration. So basically, the cost function is an average of all the loss values over the whole training dataset. Mathematically it can be defined as:

In the above equation, ** m** is the total number of training examples. The objective of training the network is to find Weight matrix

**and Bias**

*W***such that the value of cost function**

*b***is minimized.**

*J*### Gradient Descent

The Weight Matrix ** W** is randomly initialized. We use gradient descent to minimize the Cost Function

**and obtain the optimal Weight Matrix**

*J***and Bias**

*W***. Gradient descent is a first-order iterative optimization algorithm for finding the minimum of a function. We apply gradient descent to the Cost Function**

*b***to minimize the cost. Mathematically it can be defined as:**

*J*The first equation represents the change in Weight Matrix ** W** whereas the second equation represents the change in Bias

**. The change in the values is determined by learning rate**

*b***and the derivatives of the cost**

*alpha***with respect to the Weight Matrix**

*J***and Bias**

*W***. We repeat the updation of**

*b***and**

*W***until the Cost Function**

*b***has been minimized. Now lets us understand how Gradient Descent works with the help of the following graph:**

*J***Case 1**. Let us assume that ** W **was initialized with values less than the values at which it achieves global minimum, then the slope at that point i.e partial derivative of

**with respect to**

*J***is negative and hence, the weight values will**

*W***according to the Gradient Descent’s equation.**

*increase***Case 2**. Let us assume that ** W **was initialized with values more than the values at which it achieves global minimum, then the slope at that point i.e partial derivative of

**with respect to**

*J***is positive and hence, the weight values will**

*W***according to the Gradient Descent’s equation.**

*decrease*Accordingly, both ** W** and

**will achieve their optimal value and the value of cost function**

*b***will be minimized.**

*J*### Logistic Regression using Gradient Descent

Till now, we have understood the mathematical model of both logistic regression and gradient descent. In this section, we will see how we can use gradient descent for learning the Weight Matrix ** W **and Bias

**in the context of logistic regression. Let us summarize all the equations that we know so far.**

*b*- The first equation denotes the product of an input
with the Weight Matrix*X*and Bias*W*.*b* - The second equation is the
activation function which introduces non-linearity.*sigmoid* - The third equation is the loss function which calculates the loss between a given
and predicted*Y*.*Y’*

These equations can be modelled using a graph which is known as ** Computation Graph**. For simplicity let us consider that there are 2 features

**and**

*x1***in a given input matrix**

*x2***. Accordingly there will be 2 weights**

*X***and**

*w1***in the Weight Matrix**

*w2***. The Computation Graph for the above scenario can be defined as:**

*W*In the above figure, the forward propagation(black arrows) is used to predict ** y’ **and the backward propagation(red arrows) is used to update the weights

**and**

*w1***and bias**

*w2***. As we saw in gradient descent, we need to calculate the derivatives of weights and bias to update them. Using the computation graph makes it easy to calculate these derivates. As the loss**

*b***,**

*L***depends on**

**, first**

*a***we calculate the derivative**

**which represents the derivative of**

*da***with respect to**

*L***using equation 3 as follows :**

*a*After that, we calculate the derivative ** dz **which represents the derivative of

**with respect to**

*L***using equation 2. This can be done using the chain rule as follows :**

*z*Similarly, we can find all derivatives ** dw1**,

**and**

*dw2***using equation 1 and the chain rule. The value of these derivatives are as follows:**

*db*Now that we have found out all the derivatives, we need to update the weight and bias values with the help of gradient descent as follows:

With this, we have applied gradient descent to logistic regression and studied how the Weight Matrix ** W** and Bias

**are updated to that the loss**

*b***is minimized.**

*L*I suggest to the readers that if they have the knowledge of calculus, they should do the math and figure out the above equations themselves for proper understanding of the topic.

Note that all the above calculations are implemented for a single training example but during the training process we have to apply the above steps to all the training examples in a single iteration. Let’s figure out how we do that. We know that Cost Function ** J **is mathematically defined as follows:

During the training process, we need to minimize the cost ** J **. This can be done by minimizing the loss

**for each training example**

*L***over all the**

*i***training examples. As the loss function**

*m***itself depends on the weights**

*L***,**

*w1***and**

*w2***, we have to minimize**

*b***with respect to the weights**

*J***and**

*w1***and bias**

*w2***as follows:**

*b*### Summarization

The above few sections explain the math behind the working of neural networks with the example of logistic regression. Let us sum up how we can implement logistic regression as a neural network in a few lines as follows:

This is the computation done in a single step of training over all the training examples. During training, we need to perform all the above steps for many iterations which can range from 1000 to 1000k depending on the task. We can stop the training process when accuracy does not improve or the cost ** J** is minimized thus dropping the need to specify the number of steps. With improvements in hardware and software, the time needed for such a huge amount of computation has reduced drastically owing to the rise of Deep Learning.

Thus, in this story, we studied the math behind neural networks in the context of logistic regression.

#### References

*I would like to thank the readers for reading the story. If you have any questions or doubts, feel free to ask them in the comments section below. I’ll be more than happy to answer them and help you out. If you like the story, please follow me to get regular updates when I publish a new story. I welcome any suggestions that will improve my stories.*

## Leave a Reply