How to derive derivative of the logarithm of a summation? I'm currently reading the book Deep Learning (Goodfellow et al., 2015) and had a question regarding the calculation of a gradient when explaining backpropagation for a certain example. For anyone who's curious, this is from section 6.5.9: Differentiation outside the Deep Learning Community.

Suppose we have variables $p_1, p_2, ... , p_n$ representing probabilities and variables $z_1, z_2, ... , z_n$ representing unnormalized log probabilities. Suppose we define
$$q_i = \frac{e^{z_i}}{\sum_i e^{z_i}}$$
where we build the softmax function out of exponentiation, summation and division operations, and construct a cross-entropy loss $J = -\sum_i p_i \log{q_i}$. A human mathematician can observe that the derivateive of $J$ with respect to $z_i$ takes a very simple form: $q_i - p_i$.

I don't know how this result was derived, and was hoping that someone could give me some tips or advice. What I have so far is
$$\log{q_i} = \log{e^{z_i}} - \log({\sum_i e^{z_i}})$$
$$
\begin{align}
p_i\log{q_i} & = p_i \log{e^{z_i}} - p_i \log({\sum_i e^{z_i}}) \\
& = p_iz_i - p_i\log(\sum_i e^{z_i})
\end{align}$$
If we take the derivative of $J = p_i\log{q_i}$ then I can understand that $d/dz_i (p_i z_i) = p_i$, but how do we differentiate the second term that contains the logarithm of the summation?
Thank you.

Well, 
$$\frac{\partial J}{\partial z_i}  = \frac{\partial}{\partial z_i} (-\sum_j p_j\log q_j )=\frac{\partial}{\partial z_i}( -p_i\log q_i) \\= \frac{\partial}{\partial z_i} (-p_i \log (e^{z_i}/\sum_k e^{z_k}))\\ =\frac{\partial}{\partial z_i} (-p_iz_i\log e + p_i\log(\sum_k e^{z_k})) \\= \frac{\partial}{\partial z_i} (-p_iz_i) + \frac{\partial}{\partial z_i}p_i\log(\sum_k e^{z_k}) \\= -p_i + p_ie^{z_i}/\sum_k e^{z_k} \\= -p_i+p_iq_i.$$
