CSE-41XX
CS-4125 ML

Lecture 09: Neural Networks - Learning

An in-depth guide to learning parameters in Neural Networks, covering the cost function, backpropagation algorithm, parameter unrolling, gradient checking, random weight initialization, and complete workflow implementation.

Neural Network Cost Function

Neural Networks (NNs) are one of the most powerful learning algorithms. They allow fitting complex non-linear hypothesis functions given a training set. Let's first examine the setup and cost function for neural networks.

Setup and Notation

We focus on the application of NNs to classification problems. Here is the general setup:

  • Training set: {(x(1),y(1)),(x(2),y(2)),(x(3),y(3)),,(x(m),y(m))}\{(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), (x^{(3)}, y^{(3)}), \dots, (x^{(m)}, y^{(m)})\}
  • LL = total number of layers in the network (e.g., L=4L = 4 in the diagram below)
  • sls_l = number of units (not counting the bias unit) in layer ll

Neural Network architecture example

In the example network shown above:

  • L=4L = 4 (4 layers)
  • s1=3s_1 = 3 (3 input features, excluding bias x0x_0)
  • s2=5s_2 = 5 (5 hidden units in layer 2)
  • s3=5s_3 = 5 (5 hidden units in layer 3)
  • s4=4s_4 = 4 (4 output units in layer 4)

Types of Classification Problems

We distinguish two types of classification problems in neural networks:

  • Binary classification:

    • Single output (y{0,1}y \in \{0, 1\})
    • Output node is a scalar real number
    • K=1K = 1 output unit (where KK denotes the number of units in the output layer)
    • sL=1s_L = 1
  • Multi-class classification:

    • KK distinct classification categories (K3K \ge 3; if K=2K = 2, it defaults to binary classification)
    • Output yy is a KK-dimensional vector of real numbers (one-hot encoded vector representing each class)
    • sL=Ks_L = K

Multi-class classification output layer

Cost Function for Neural Networks

Recall that the regularized logistic regression cost function for binary classification is:

Regularized logistic regression cost function

J(θ)=1m[i=1my(i)log(hθ(x(i)))+(1y(i))log(1hθ(x(i)))]+λ2mj=1nθj2J(\theta) = -\frac{1}{m} \left[ \sum_{i=1}^m y^{(i)} \log(h_\theta(x^{(i)})) + (1 - y^{(i)}) \log(1 - h_\theta(x^{(i)})) \right] + \frac{\lambda}{2m} \sum_{j=1}^n \theta_j^2

For neural networks, the cost function is a generalization of this equation. Instead of a single scalar output, the hypothesis hΘ(x)h_\Theta(x) outputs a KK-dimensional vector:

Neural network cost function

J(Θ)=1m[i=1mk=1Kyk(i)log((hΘ(x(i)))k)+(1yk(i))log(1(hΘ(x(i)))k)]+λ2ml=1L1i=1slj=1sl+1(Θji(l))2J(\Theta) = -\frac{1}{m} \left[ \sum_{i=1}^m \sum_{k=1}^K y_k^{(i)} \log((h_\Theta(x^{(i)}))_k) + (1 - y_k^{(i)}) \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_{ji}^{(l)})^2

Here:

  • hΘ(x)RKh_\Theta(x) \in \mathbb{R}^K, so (hΘ(x))k(h_\Theta(x))_k refers to the kk-th element of the output vector.
  • The summation k=1K\sum_{k=1}^K accumulates the logistic regression loss over all KK output units.

Understanding the Cost Function

The neural network cost function consists of two primary components:

  1. First Half (Unregularized Cost):

First half of neural network cost function

1mi=1mk=1K[yk(i)log((hΘ(x(i)))k)+(1yk(i))log(1(hΘ(x(i)))k)]-\frac{1}{m} \sum_{i=1}^m \sum_{k=1}^K \left[ y_k^{(i)} \log((h_\Theta(x^{(i)}))_k) + (1 - y_k^{(i)}) \log(1 - (h_\Theta(x^{(i)}))_k) \right]

This term computes the average cross-entropy loss over all mm training examples and across all KK output units.

  1. Second Half (Regularization / Weight Decay):

Second half of neural network cost function (regularization term)

λ2ml=1L1i=1slj=1sl+1(Θji(l))2\frac{\lambda}{2m} \sum_{l=1}^{L-1} \sum_{i=1}^{s_l} \sum_{j=1}^{s_{l+1}} (\Theta_{ji}^{(l)})^2

This is a triple nested summation that sums the square of every parameter weight in the network. Notice that:

  • We do not regularize the bias terms (Θj0(l)\Theta_{j0}^{(l)}), hence the index ii starts from 1. (Even if bias terms were regularized, it wouldn't make much difference, but omitting them is standard).
  • This term is often called the weight decay term.
  • As before, λ\lambda balances fitting the training data versus keeping weight parameters small to prevent overfitting.

Now that we have defined the cost function J(Θ)J(\Theta), how do we minimize it?


Overview of Training & Backpropagation

The backpropagation algorithm is one of the more involved concepts in machine learning, so let's first outline the high-level roadmap of what we are doing.

Forward Propagation vs. Backpropagation

  • Forward Propagation: Takes the neural network parameters Θ\Theta and an input vector xx, feeding it forward through each layer to compute activations up to the output hypothesis hΘ(x)h_\Theta(x) (which can be a scalar or a KK-dimensional vector).
  • Backpropagation:
    1. Takes the network's output hypothesis hΘ(x)h_\Theta(x) and compares it against the true label yy to compute the error of the output layer.
    2. Back-calculates the error associated with each unit in preceding hidden layers (from layer L1L-1 down to layer 2).
    3. Uses these error terms to compute the partial derivatives of the cost function Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta).
    4. Feeds these partial derivatives into gradient descent (or advanced optimization algorithms like BFGS, L-BFGS, conjugate gradient) to minimize J(Θ)J(\Theta) and iteratively update Θ\Theta.

Key Matrices to Keep in Mind

  • For each layer ll, there is a parameter matrix Θ(l)\Theta^{(l)} mapping from layer ll to layer l+1l+1. The size of Θ(l)\Theta^{(l)} is sl+1×(sl+1)s_{l+1} \times (s_l + 1).
  • There is an error matrix Δ(l)\Delta^{(l)} associated with each layer ll, which serves as an accumulator for computing partial derivatives across all mm training examples.

Backpropagation Algorithm

The backpropagation algorithm allows us to efficiently calculate the partial derivatives Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta) so we can minimize J(Θ)J(\Theta).

Neural network architecture for backpropagation

Given the cost function J(Θ)J(\Theta), our goal is to find parameters Θ\Theta to minimize J(Θ)J(\Theta):

minΘJ(Θ)\min_\Theta J(\Theta)

To use optimization algorithms (such as gradient descent or advanced optimizers), we need code that computes two things:

  1. J(Θ)J(\Theta): The cost value for a given set of parameters Θ\Theta.
  2. Partial derivative terms: Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta) for every parameter Θij(l)R\Theta_{ij}^{(l)} \in \mathbb{R}.

Recall the index notation for Θij(l)\Theta_{ij}^{(l)}:

  • ll: origin layer (layer mapping from)
  • jj: origin node in layer ll (node mapping from)
  • ii: destination node in layer l+1l+1 (node mapping to)

Partial derivative term formula

Each partial derivative term Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta) is a single real number.

Vectorized Forward Propagation (Single Training Example)

For a single training example (x,y)(x, y), forward propagation proceeds as follows:

Forward propagation vectorized steps

  • Layer 1 (Input): a(1)=xa^{(1)} = x
  • Layer 2 (Hidden): z(2)=Θ(1)a(1)    a(2)=g(z(2))z^{(2)} = \Theta^{(1)} a^{(1)} \implies a^{(2)} = g(z^{(2)}) (add bias unit a0(2)=1a_0^{(2)} = 1)
  • Layer 3 (Hidden): z(3)=Θ(2)a(2)    a(3)=g(z(3))z^{(3)} = \Theta^{(2)} a^{(2)} \implies a^{(3)} = g(z^{(3)}) (add bias unit a0(3)=1a_0^{(3)} = 1)
  • Layer 4 (Output): z(4)=Θ(3)a(3)    a(4)=g(z(4))=hΘ(x)z^{(4)} = \Theta^{(3)} a^{(3)} \implies a^{(4)} = g(z^{(4)}) = h_\Theta(x)

Forward propagation activations diagram

What is Backpropagation?

Backpropagation calculates an error term δj(l)\delta_j^{(l)} for each node jj in layer ll. Intuitively, δj(l)\delta_j^{(l)} measures the "error" in the activation aj(l)a_j^{(l)} of unit jj in layer ll.

Since aj(l)a_j^{(l)} is an activation computed by the network, we evaluate how far off it is relative to the target:

  • The only ground truth target we have is the true label yy at the output layer LL.
  • For the output layer (L=4L = 4 in our example), the error vector δ(4)\delta^{(4)} is simply:
δj(4)=aj(4)yj=(hΘ(x))jyj\delta_j^{(4)} = a_j^{(4)} - y_j = (h_\Theta(x))_j - y_j

In vectorized form:

δ(4)=a(4)y\delta^{(4)} = a^{(4)} - y

Next, we propagate these errors backward to compute the error vectors for hidden layers:

Backpropagation delta error equations

δ(3)=(Θ(3))Tδ(4)g(z(3))\delta^{(3)} = (\Theta^{(3)})^T \delta^{(4)} \odot g'(z^{(3)}) δ(2)=(Θ(2))Tδ(3)g(z(2))\delta^{(2)} = (\Theta^{(2)})^T \delta^{(3)} \odot g'(z^{(2)})

Where:

  • \odot denotes element-wise (pairwise) multiplication (in Octave/MATLAB, .*).
  • g(z(l))g'(z^{(l)}) is the derivative of the sigmoid activation function evaluated at z(l)z^{(l)}.
  • Calculus shows that g(z(l))=a(l)(1a(l))g'(z^{(l)}) = a^{(l)} \odot (1 - a^{(l)}).

Thus:

δ(3)=(Θ(3))Tδ(4)(a(3)(1a(3)))\delta^{(3)} = (\Theta^{(3)})^T \delta^{(4)} \odot (a^{(3)} \odot (1 - a^{(3)})) δ(2)=(Θ(2))Tδ(3)(a(2)(1a(2)))\delta^{(2)} = (\Theta^{(2)})^T \delta^{(3)} \odot (a^{(2)} \odot (1 - a^{(2)}))

Note that there is no δ(1)\delta^{(1)} term because layer 1 is the input layer, which corresponds to actual features with zero error.

Analyzing Vector Dimensions

Let's check the matrix dimensions in our 4-layer example network (s1=3,s2=5,s3=5,s4=4s_1=3, s_2=5, s_3=5, s_4=4):

Analyzing vector dimensions in backpropagation

  • Θ(3)\Theta^{(3)} has size [4×5][4 \times 5] (excluding bias column; [4×6][4 \times 6] including bias column).
  • (Θ(3))T(\Theta^{(3)})^T has size [5×4][5 \times 4].
  • δ(4)\delta^{(4)} has size [4×1][4 \times 1].
  • Multiplying (Θ(3))Tδ(4)(\Theta^{(3)})^T \delta^{(4)} yields a [5×1][5 \times 1] vector.
  • This matches the dimension of a(3)a^{(3)} ([5×1][5 \times 1]), allowing element-wise multiplication \odot.

Why Do We Compute Delta Terms?

Through mathematical derivation, if we ignore regularization (λ=0\lambda = 0), the partial derivative for a single training example is simply:

Partial derivative relation to delta terms

Θij(l)J(Θ)=aj(l)δi(l+1)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta) = a_j^{(l)} \delta_i^{(l+1)}

By computing the activation values aj(l)a_j^{(l)} during forward propagation and error terms δi(l+1)\delta_i^{(l+1)} during backpropagation, we get the partial derivative terms needed for gradient descent!


Complete Algorithm: Computing Partial Derivatives for mm Examples

Given a training set of mm examples:

Training set definition

{(x(1),y(1)),(x(2),y(2)),,(x(m),y(m))}\{(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), \dots, (x^{(m)}, y^{(m)})\}

We compute partial derivatives using an accumulator matrix Δ(l)\Delta^{(l)} for each layer ll:

  1. Initialize Accumulators:

Delta accumulator initialization

Set Δij(l)=0\Delta_{ij}^{(l)} = 0 for all l,i,jl, i, j. (These matrices will accumulate gradients across examples).

  1. Loop Over Training Examples:

Training set loop for backpropagation

For i=1i = 1 to mm:

  • Set input activation a(1)=x(i)a^{(1)} = x^{(i)}.
  • Run forward propagation to compute activations a(l)a^{(l)} for l=2,3,,Ll = 2, 3, \dots, L.
  • Compute output error vector: δ(L)=a(L)y(i)\delta^{(L)} = a^{(L)} - y^{(i)}.
  • Run backpropagation to compute δ(L1),δ(L2),,δ(2)\delta^{(L-1)}, \delta^{(L-2)}, \dots, \delta^{(2)}.
  • Accumulate the gradients:

Delta accumulation equation

Δij(l):=Δij(l)+aj(l)δi(l+1)\Delta_{ij}^{(l)} := \Delta_{ij}^{(l)} + a_j^{(l)} \delta_i^{(l+1)}

In vectorized form:

Vectorized delta accumulation equation

Δ(l):=Δ(l)+δ(l+1)(a(l))T\Delta^{(l)} := \Delta^{(l)} + \delta^{(l+1)} (a^{(l)})^T
  1. Compute Final Gradient Matrices D(l)D^{(l)}:

After exiting the loop over all mm examples, compute the gradient terms Dij(l)D_{ij}^{(l)}:

D matrix computation equation

  • For j=0j = 0 (bias term, no regularization):
Dij(l)=1mΔij(l)D_{ij}^{(l)} = \frac{1}{m} \Delta_{ij}^{(l)}
  • For j1j \ge 1 (with regularization):
Dij(l)=1mΔij(l)+λmΘij(l)D_{ij}^{(l)} = \frac{1}{m} \Delta_{ij}^{(l)} + \frac{\lambda}{m} \Theta_{ij}^{(l)}

D matrix partial derivative equality

Once computed, each term Dij(l)D_{ij}^{(l)} is exact:

Dij(l)=Θij(l)J(Θ)D_{ij}^{(l)} = \frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta)

These values are then passed into gradient descent or advanced optimization routines!


Backpropagation Intuition

Let's break down backpropagation step-by-step to gain visual intuition for how errors flow backward through the network.

Visualizing Forward Propagation

Forward propagation input visualization

Consider a training example (x(i),y(i))(x^{(i)}, y^{(i)}) with 2 input features (x1,x2x_1, x_2). Forward propagation passes activations forward:

Forward propagation hidden layer 2 visualization

Each hidden node calculates a weighted sum of inputs zj(l)z_j^{(l)}:

z1(2)=Θ10(1)1+Θ11(1)x1+Θ12(1)x2z_1^{(2)} = \Theta_{10}^{(1)} \cdot 1 + \Theta_{11}^{(1)} x_1 + \Theta_{12}^{(1)} x_2

Applying the sigmoid function g(z)g(z) yields the activation a1(2)=g(z1(2))a_1^{(2)} = g(z_1^{(2)}):

Forward propagation hidden layer 3 and output visualization

Visualizing Backpropagation

Cost function for single example

For a single output binary classification problem, the cost for a single example (x(i),y(i))(x^{(i)}, y^{(i)}) simplifies to:

Single example cost formula

cost(i)=y(i)log(hΘ(x(i)))+(1y(i))log(1hΘ(x(i)))\text{cost}(i) = y^{(i)} \log(h_\Theta(x^{(i)})) + (1 - y^{(i)}) \log(1 - h_\Theta(x^{(i)}))

Conceptually, this measures how well the network performs on example ii. You can think of it as a sigmoidal version of squared error (hΘ(x)y)2(h_\Theta(x) - y)^2.

Formally, the delta term δj(l)\delta_j^{(l)} represents the partial derivative of the cost with respect to the weighted input sum zj(l)z_j^{(l)}:

Delta error definition derivative

δj(l)=zj(l)cost(i)\delta_j^{(l)} = \frac{\partial}{\partial z_j^{(l)}} \text{cost}(i)

Error Propagation

Backpropagation error flow visualization

  1. At the output layer, δ1(4)=a1(4)y(1)\delta_1^{(4)} = a_1^{(4)} - y^{(1)} is the difference between predicted activation and actual target.
  2. Going backward, node error δj(l)\delta_j^{(l)} is calculated as the weighted sum of errors from the next layer's nodes, weighted by the parameter links connecting them:

Backpropagation detailed node error computation

δ1(3)=Θ11(3)δ1(4)+Θ21(3)δ2(4)\delta_1^{(3)} = \Theta_{11}^{(3)} \delta_1^{(4)} + \Theta_{21}^{(3)} \delta_2^{(4)} δ2(3)=Θ12(3)δ1(4)+Θ22(3)δ2(4)\delta_2^{(3)} = \Theta_{12}^{(3)} \delta_1^{(4)} + \Theta_{22}^{(3)} \delta_2^{(4)}

Thus, forward propagation computes activation values moving left-to-right, while backpropagation computes error terms moving right-to-left.


Implementation Details: Unrolling Parameters

When using advanced optimization routines in MATLAB/Octave (such as fminunc), the cost function and initial parameter values must be passed as 1D vectors:

Unrolling parameters in Octave

However, in neural networks, our parameters Θ(1),Θ(2),\Theta^{(1)}, \Theta^{(2)}, \dots and gradients D(1),D(2),D^{(1)}, D^{(2)}, \dots are matrices:

Matrix parameter unrolling and reshaping

Unrolling and Reshaping Matrices in Octave

Suppose we have a 3-layer neural network with:

  • s1=10s_1 = 10 input units
  • s2=10s_2 = 10 hidden units
  • s3=1s_3 = 1 output unit

The parameter matrix dimensions are:

  • Θ(1)R10×11\Theta^{(1)} \in \mathbb{R}^{10 \times 11}
  • Θ(2)R10×11\Theta^{(2)} \in \mathbb{R}^{10 \times 11}
  • Θ(3)R1×11\Theta^{(3)} \in \mathbb{R}^{1 \times 11}

Unrolling example Octave commands

To convert these matrices into a single unrolled vector for fminunc:

% Unroll matrices into a single vector
thetaVec = [ Theta1(:); Theta2(:); Theta3(:) ];
DVec     = [ D1(:);     D2(:);     D3(:) ];

To reshape the vector back into matrices inside your cost function:

% Reshape unrolled vector back into parameter matrices
Theta1 = reshape(thetaVec(1:110), 10, 11);
Theta2 = reshape(thetaVec(111:220), 10, 11);
Theta3 = reshape(thetaVec(221:231), 1, 11);

Gradient Checking

Backpropagation is complex and subtle. A small bug in backpropagation can lead to a situation where J(Θ)J(\Theta) appears to decrease during gradient descent, but fails to reach a true optimum.

To ensure our backpropagation implementation is 100% bug-free, we use gradient checking.

Numerical Gradient Estimation

Consider a scalar function J(θ)J(\theta) where θR\theta \in \mathbb{R}. We can estimate the derivative numerically using the two-sided difference:

Numerical gradient checking slope estimation

ddθJ(θ)J(θ+ϵ)J(θϵ)2ϵ\frac{d}{d\theta} J(\theta) \approx \frac{J(\theta + \epsilon) - J(\theta - \epsilon)}{2\epsilon}

Typically, ϵ\epsilon is chosen to be small, e.g., ϵ=104\epsilon = 10^{-4}.

For a parameter vector ΘRn\Theta \in \mathbb{R}^n, we approximate the partial derivative with respect to each parameter θi\theta_i:

Partial derivative numerical approximation formulas

θiJ(Θ)J(θ1,,θi+ϵ,,θn)J(θ1,,θiϵ,,θn)2ϵ\frac{\partial}{\partial \theta_i} J(\Theta) \approx \frac{J(\theta_1, \dots, \theta_i + \epsilon, \dots, \theta_n) - J(\theta_1, \dots, \theta_i - \epsilon, \dots, \theta_n)}{2\epsilon}

Octave Implementation of Gradient Checking

Gradient checking Octave implementation

for i = 1:n
  thetaPlus = theta;
  thetaPlus(i) = thetaPlus(i) + EPSILON;
  
  thetaMinus = theta;
  thetaMinus(i) = thetaMinus(i) - EPSILON;
  
  gradApprox(i) = (J(thetaPlus) - J(thetaMinus)) / (2 * EPSILON);
end

Gradient Checking Protocol

  1. Implement backpropagation to compute the analytical gradient vector DVec.
  2. Implement numerical gradient checking to compute gradApprox.
  3. Verify that gradApprox \approx DVec to several decimal places.
  4. IMPORTANT: Turn off gradient checking before running learning algorithms to train the model!

Warning: Numerical gradient checking is computationally very expensive because it requires evaluating J(Θ)J(\Theta) twice for every single parameter. Backpropagation is far faster and should be used for actual training.


Random Initialization

For linear regression or logistic regression, initializing parameters Θ=0\Theta = \mathbf{0} (all zeros) works fine. However, zero initialization fails completely for neural networks.

The Symmetry Problem

If all weights are initialized to zero:

  • Every hidden unit in layer ll will compute the exact same activation aj(l)a_j^{(l)}.
  • Every hidden unit will receive the exact same error δj(l)\delta_j^{(l)}.
  • Every weight will undergo identical updates during gradient descent (Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta) will be identical across units).
  • The hidden units remain symmetrical and fail to learn distinct features.

Random Initialization Solution (Symmetry Breaking)

To break symmetry, initialize each weight Θij(l)\Theta_{ij}^{(l)} randomly to a small value in [ϵinit,ϵinit][-\epsilon_{init}, \epsilon_{init}]:

% Initialize weights randomly in [-epsilon_init, epsilon_init]
Theta1 = rand(10, 11) * (2 * EPSILON_INIT) - EPSILON_INIT;
Theta2 = rand(10, 11) * (2 * EPSILON_INIT) - EPSILON_INIT;
Theta3 = rand(1, 11)  * (2 * EPSILON_INIT) - EPSILON_INIT;

Putting It All Together

Here is a step-by-step guide to designing and training a neural network.

Neural network architecture setup

Step 1: Select Network Architecture

Pick a network layout (connectivity structure between neurons):

  • Input units: n=dimension of feature vector x(i)n = \text{dimension of feature vector } x^{(i)}.
  • Output units: K=number of classesK = \text{number of classes}.
    • Binary classification: 1 output unit.
    • Multi-class classification: KK output units (one-hot vector).
  • Hidden layers:
    • Default: 1 hidden layer.
    • If using multiple hidden layers, recommended to have the same number of hidden units in each hidden layer.
    • Number of hidden units: usually equal to or 1.5–2×\times the number of input features. More hidden units generally improve representation capacity but increase computational cost.

Step 2: Training a Neural Network

  1. Randomly initialize weights: Small random values near zero (e.g., in [ϵinit,ϵinit][-\epsilon_{init}, \epsilon_{init}]).
  2. Implement forward propagation: Compute hΘ(x(i))h_\Theta(x^{(i)}) for any input x(i)x^{(i)}.
  3. Implement cost function code: Compute J(Θ)J(\Theta).
  4. Implement backpropagation: Compute partial derivative terms Θij(l)J(Θ)\frac{\partial}{\partial \Theta_{ij}^{(l)}} J(\Theta).
% General loop over training set for backpropagation
for i = 1:m
  % 1. Forward propagation on (x^i, y^i) to get activation (a) terms
  % 2. Backpropagation on (x^i, y^i) to get error (delta) terms
  % 3. Accumulate delta: Delta^(l) := Delta^(l) + delta^(l+1) * (a^(l))'
end

% Compute final D matrix partial derivatives with regularization
  1. Perform gradient checking: Compare D(l)D^{(l)} with numerical gradient approximation gradApprox. Disable gradient checking once confirmed working!
  2. Minimize J(Θ)J(\Theta): Use gradient descent or advanced optimization algorithms (e.g., fminunc, L-BFGS) with backpropagation to minimize J(Θ)J(\Theta) as a function of parameters Θ\Theta.

Non-convex cost function surface visualization

Note on Non-Convexity: For neural networks, the cost function J(Θ)J(\Theta) is non-convex and can theoretically get stuck in local minima. In practice, gradient descent algorithms initialized with small random weights find very effective local (or global) minima.

On this page