# How is the cost function from Logistic Regression derivated

I am doing the Machine Learning Stanford course on Coursera.

In the chapter on Logistic Regression, the cost function is this:

Then, it is derivated here:

I tried getting the derivative of the cost function but I got something completely different.

How is the derivative obtained?

Which are the intermediary steps?

• +1, check @AdamO's answer in my question here. stats.stackexchange.com/questions/229014/… – Haitao Du May 10 '17 at 18:39
• "Completely different" is not really sufficient to answer your question, besides telling you what you already know (the correct gradient). It'd be much more useful if you gave us what your calculations resulted in, then we can help you shore up where you made the mistake. – Matthew Drury May 10 '17 at 20:43
• @MatthewDrury Sorry, Matt, I had arranged the answer right before your comment came in. Octavian, did you follow all the steps? I will edit to give it some added value later... – Antoni Parellada May 10 '17 at 20:46
• when you say "derivated" do you mean "differentiated" or "derived"? – Glen_b -Reinstate Monica May 11 '17 at 3:19

Adapted from the notes in the course, which I don't see available (including this derivation) outside the notes contributed by students within the page of Andrew Ng's Coursera Machine Learning course.

In what follows, the superscript $$(i)$$ denotes individual measurements or training "examples."

$$\small \frac{\partial J(\theta)}{\partial \theta_j} = \frac{\partial}{\partial \theta_j} \,\frac{-1}{m}\sum_{i=1}^m \left[ y^{(i)}\log\left(h_\theta \left(x^{(i)}\right)\right) + (1 -y^{(i)})\log\left(1-h_\theta \left(x^{(i)}\right)\right)\right] \\[2ex]\small\underset{\text{linearity}}= \,\frac{-1}{m}\,\sum_{i=1}^m \left[ y^{(i)}\frac{\partial}{\partial \theta_j}\log\left(h_\theta \left(x^{(i)}\right)\right) + (1 -y^{(i)})\frac{\partial}{\partial \theta_j}\log\left(1-h_\theta \left(x^{(i)}\right)\right) \right] \\[2ex]\Tiny\underset{\text{chain rule}}= \,\frac{-1}{m}\,\sum_{i=1}^m \left[ y^{(i)}\frac{\frac{\partial}{\partial \theta_j}h_\theta \left(x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} + (1 -y^{(i)})\frac{\frac{\partial}{\partial \theta_j}\left(1-h_\theta \left(x^{(i)}\right)\right)}{1-h_\theta\left(x^{(i)}\right)} \right] \\[2ex]\small\underset{h_\theta(x)=\sigma\left(\theta^\top x\right)}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[ y^{(i)}\frac{\frac{\partial}{\partial \theta_j}\sigma\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} + (1 -y^{(i)})\frac{\frac{\partial}{\partial \theta_j}\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)}{1-h_\theta\left(x^{(i)}\right)} \right] \\[2ex]\Tiny\underset{\sigma'}=\frac{-1}{m}\,\sum_{i=1}^m \left[ y^{(i)}\, \frac{\sigma\left(\theta^\top x^{(i)}\right)\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} - (1 -y^{(i)})\,\frac{\sigma\left(\theta^\top x^{(i)}\right)\left(1-\sigma\left(\theta^\top x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{1-h_\theta\left(x^{(i)}\right)} \right] \\[2ex]\small\underset{\sigma\left(\theta^\top x\right)=h_\theta(x)}= \,\frac{-1}{m}\,\sum_{i=1}^m \left[ y^{(i)}\frac{h_\theta\left( x^{(i)}\right)\left(1-h_\theta\left( x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)}{h_\theta\left(x^{(i)}\right)} - (1 -y^{(i)})\frac{h_\theta\left( x^{(i)}\right)\left(1-h_\theta\left(x^{(i)}\right)\right)\frac{\partial}{\partial \theta_j}\left( \theta^\top x^{(i)}\right)}{1-h_\theta\left(x^{(i)}\right)} \right] \\[2ex]\small\underset{\frac{\partial}{\partial \theta_j}\left(\theta^\top x^{(i)}\right)=x_j^{(i)}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{(i)}\left(1-h_\theta\left(x^{(i)}\right)\right)x_j^{(i)}- \left(1-y^{i}\right)\,h_\theta\left(x^{(i)}\right)x_j^{(i)} \right] \\[2ex]\small\underset{\text{distribute}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{i}-y^{i}h_\theta\left(x^{(i)}\right)- h_\theta\left(x^{(i)}\right)+y^{(i)}h_\theta\left(x^{(i)}\right) \right]\,x_j^{(i)} \\[2ex]\small\underset{\text{cancel}}=\,\frac{-1}{m}\,\sum_{i=1}^m \left[y^{(i)}-h_\theta\left(x^{(i)}\right)\right]\,x_j^{(i)} \\[2ex]\small=\frac{1}{m}\sum_{i=1}^m\left[h_\theta\left(x^{(i)}\right)-y^{(i)}\right]\,x_j^{(i)}$$

The derivative of the sigmoid function is

\Tiny\begin{align}\frac{d}{dx}\sigma(x)&=\frac{d}{dx}\left(\frac{1}{1+e^{-x}}\right)\\[2ex] &=\frac{-(1+e^{-x})'}{(1+e^{-x})^2}\\[2ex] &=\frac{e^{-x}}{(1+e^{-x})^2}\\[2ex] &=\left(\frac{1}{1+e^{-x}}\right)\left(\frac{e^{-x}}{1+e^{-x}}\right)\\[2ex] &=\left(\frac{1}{1+e^{-x}}\right)\,\left(\frac{1+e^{-x}}{1+e^{-x}}-\frac{1}{1+e^{-x}}\right)\\[2ex] &=\sigma(x)\,\left(\frac{1+e^{-x}}{1+e^{-x}}-\sigma(x)\right)\\[2ex] &=\sigma(x)\,(1-\sigma(x)) \end{align}

• +1 for all the efforts!, may be using matrix notation could be easier? – Haitao Du May 11 '17 at 1:43
• can I say in linear regression, objective is $\|Ax-b\|^2$ and derivative is $2A^Te$, where $e=Ax-b$, in logistic regression, it is similar, the derivative is $A^Te$ where $e=p-b$, and $p=\text{sigmoid}~(Ax)$ ? – Haitao Du May 11 '17 at 1:46
• that is why I appreciate your effort. you spend time to us OP's language!! – Haitao Du May 11 '17 at 1:47
• My understanding is that there are convexity issues that make the squared error minimization undesirable for non-linear activation functions. In matrix notation, it would be $\frac{\partial J(\theta)}{\partial \theta}=\frac{1}{m}X^\top\left( \sigma(X\theta)-\mathbf y\right)$. – Antoni Parellada May 11 '17 at 1:57
• @MohammedNoureldin I just took the partial derivative in the numerators on the prior line, applying the chain rule. – Antoni Parellada Feb 25 '19 at 15:32

To avoid impression of excessive complexity of the matter, let us just see the structure of solution.

With simplification and some abuse of notation, let $G(\theta)$ be a term in sum of $J(\theta)$, and $h = 1/(1+e^{-z})$ is a function of $z(\theta)= x \theta$: $$G = y \cdot \log(h)+(1-y)\cdot \log(1-h)$$

We may use chain rule: $\frac{d G}{d \theta}=\frac{d G}{d h}\frac{d h}{d z}\frac{d z}{d \theta}$ and solve it one by one ($x$ and $y$ are constants).

$$\frac{d G}{\partial h} = \frac{y} {h} - \frac{1-y}{1-h} = \frac{y - h}{h(1-h)}$$ For sigmoid $\frac{d h}{d z} = h (1-h)$ holds, which is just a denominator of the previous statement.

Finally, $\frac{d z}{d \theta} = x$.

Combining results all together gives sought-for expression: $$\frac{d G}{d \theta} = (y-h)x$$ Hope that helps.

The credit for this answer goes to Antoni Parellada from the comments, which I think deserves a more prominent place on this page (as it helped me out when many other answers did not). Also, this is not a full derivation but more of a clear statement of $$\frac{\partial J(\theta)}{\partial \theta}$$. (For full derivation, see the other answers).

$$\frac{\partial J(\theta)}{\partial \theta} = \frac{1}{m} \cdot X^T\big(\sigma(X\theta)-y\big)$$

where

\begin{aligned} X \in \mathbb{R}^{m\times n} &= \text{Training example matrix} \\ \sigma(z) &= \frac{1}{1+e^{-z}} = \text{sigmoid function} = \text{logistic function} \\ \theta \in \mathbb{R}^{n} &= \text{weight row vector} \\ y &= \text{class/category/label corresponding to rows in X} \end{aligned}

Also, a Python implementation for those wanting to calculate the gradient of $$J$$ with respect to $$\theta$$.

import numpy
def sig(z):
return 1/(1+np.e**-(z))

"""
Compute gradient of cross entropy function with sigmoidal probabilities

Args:
X (numpy.ndarray): examples. Individuals in rows, features in columns
y (numpy.ndarray): labels. Vector corresponding to rows in X
w (numpy.ndarray): weight vector

Returns:
numpy.ndarray

"""
m = X.shape[0]
Z = w.dot(X.T)
A = sig(Z)
return  (-1/ m) * (X.T * (A - y)).sum(axis=1)


For those of us who are not so strong at calculus, but would like to play around with adjusting the cost function and need to find a way to calculate derivatives... a short cut to re-learning calculus is this online tool to automatically provide the derivation, with step by step explanations of the rule.

https://www.derivative-calculator.net