I'm taking Andrew Ng's machine learning course and week 5 covers the training of neural networks. The modified cost function for neural network training is derived from the logistic regression cost function, and is described as follows:
$$\begin{gather*} J(\Theta) = - \frac{1}{m} \sum_{i=1}^m \sum_{k=1}^K \left[y^{(i)}_k \log ((h_\Theta (x^{(i)}))_k) + (1 - y^{(i)}_k)\log (1 - (h_\Theta(x^{(i)}))_k)\right] + \frac{\lambda}{2m}\sum_{l=1}^{L-1} \sum_{i=1}^{s_l} \sum_{j=1}^{s_{l+1}} ( \Theta_{j,i}^{(l)})^2\end{gather*}$$
Here, K is the number of output units, L is the total number of layers in the network, and ${s_l}$ is the number of units in layer l.
I don't understand why the second half of the cost function, intended to prevent over-fitting by minimizing the values of the theta parameters, is summed across the entire network while the first half of the equation which actually determines theta is only for a specific layer. If each layer's cost function already includes a minimization parameter for that layer's theta values, why is it necessary to perform this minimization globally for every layer in the cost function J?
As I understand it, in training a neural network you mainly treat each layer as completely separate from the next (and in fact, that is one of the major selling points of the NN approach, wherein solving L layers independently of one-another lets you use a simple approach to obtain powerful results) - so why are we summing the second half of the equation over all layers?