0% found this document useful (0 votes)
16 views3 pages

Gradient Descent and Cost Function Analysis

This tutorial covers several topics related to gradient descent: 1. It provides the gradient descent update for minimizing the function f(x) = x^4 and calculates the value of x after one iteration with a learning rate of 0.1. 2. It describes fitting an exponential model to education expenditure data using gradient descent, plotting the cost over iterations and predicted expenditures from 1981-2023. 3. It derives the gradient of the cost function C(w) for linear regression with mean-squared error loss and other activation functions like sigmoid and ReLU.

Uploaded by

Evan Duh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Gradient Descent and Cost Function Analysis

This tutorial covers several topics related to gradient descent: 1. It provides the gradient descent update for minimizing the function f(x) = x^4 and calculates the value of x after one iteration with a learning rate of 0.1. 2. It describes fitting an exponential model to education expenditure data using gradient descent, plotting the cost over iterations and predicted expenditures from 1981-2023. 3. It derives the gradient of the cost function C(w) for linear regression with mean-squared error loss and other activation functions like sigmoid and ReLU.

Uploaded by

Evan Duh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EE2211: Spring 2023

Tutorial 8

1. Suppose we are minimizing f (x) = x4 with respect to x. We initialize x to be 2. We perform gradient


descent with learning rate 0.1. What is the value of x after the first iteration?
2. Please consider the csv file ([Link]), which depicts the govern-
ment’s educational expenditure over the years. We would like to predict expenditure as a function of
year. To do this, fit an exponential model f (x, w) = exp(−x> w) withPsquared error loss to estimate
m
w based on the csv file and gradient descent. In other words, C(w) = i=1 (f (xi , w) − yi )2 .
Note that even though year is one dimensional, we should add the bias term, so x = [1 year]> .
Furthermore, optimizing the exponential function is tricky (because a small change in w can lead to
large change in f ). Therefore for the purpose of optimization, divide the “year” variable by the largest
year (2018) and divide the “expenditure” by the largest expenditure, so that the resulting normalized
year and normalized expenditure variables have maximum values of 1. Use a learning rate of 0.03 and
run gradient descent for 2, 000, 000 iterations.

(a) Plot the cost function C(w) as a function of the number of iterations.
(b) Use the fitted parameters to plot the predicted educational expenditure from year 1981 to year
2023.
(c) Repeat (a) using a learning rate of 0.1 and learning rate of 0.001. What do you observe relative
to (a)?

The goal of this question is for you to code up gradient descent, so I will provide you with the gradient
derivation. First, please note that in general, ∇w (x> w) = x. To see this:
 >  
∂(x w) ∂(w1 x1 +w2 x2 +···+wd xd )
  
∂w1  ∂w1 x1
 > w)   ∂(w1 x1 +w2 x2 +···+wd xd ) 
 ∂(x
∂w2   x2 

∇w (x> w) =  ∂w. 2  = 
  = .  =x

 ..   .
.  .
   .  .
∂(x> w) ∂(w1 x1 +w2 x2 +···+wd xd ) xd
∂wd ∂wd

The above equality will be very useful for the other questions as well. Now, going back to our question,
m
X
∇w C(w) = ∇w (f (xi , w) − yi )2
i=1
m
X
= ∇w (f (xi , w) − yi )2
i=1
m
X
= 2(f (xi , w) − yi )∇w f (xi , w) chain rule
i=1
Xm
= 2(f (xi , w) − yi )∇w exp(−x>
i w)
i=1

1
m
X
=− 2(f (xi , w) − yi ) exp(−x> >
i w)∇w (xi w) chain rule
i=1
Xm
=− 2(f (xi , w) − yi ) exp(−x>
i w)xi
i=1
m
X
=− 2(f (xi , w) − yi )f (xi , w)xi
i=1

3. Given the linear learning model f (x, w) = x> w, where x ∈ Rd . Consider the loss function
L(f (xi , w), yi ) = (f (xi , w) − yi )4 ,
where i indexes the i-th training sample. The final cost function is
m
X
C(w) = L(f (xi , w), yi ),
i=1

where m is the total number of training samples. Derive the gradient of the cost function with respect
to w.
4. Repeat Question 3 using f (x, w) = σ(x> w), where σ(a) = 1
1+exp(−βa)

5. Repeat Question 3 using f (x, w) = σ(x> w), where σ(a) = max(0, a)


Remark 1. Strictly speaking, the function σ as defined in this question is not differentiable at zero.
This function is, however, used widely used as the so-called ReLU (Rectified Linear Unit) activation
function in deep neural networks. Hence, it is of great importance in machine learning. Cavalierly,
you may take the “derivative” of σ here to be

dσ(a) 1 a≥0
σ 0 (a) = = .
dx 0 a<0

6. Consider the univariate (one-input, one-output) function C(w) = 5w2 and the initial point w0 = 2.
Find the learning rate η so that gradient descent converges in one step. Find the set of learning rates
such that gradient descent diverges. Finally, find the set of learning rates such that gradient descent
converges.
7. (Optional) The logistic regression model (a generalized linear model) posits that the target yi ∈
{−1, +1} is related to the feature vector xi as follows
1
Pr(yi | xi , w) = g yi x>

i w where g(z) = .
1 + exp(z)
Suppose we have a set of training samples {(xi , yi )}m
i=1 . The loss we want to minimize is
m
X
min − log Pr(yi | xi , w)
w
i=1

This is the same as minimizing the logistic loss


m
X
Logistic yi x>

min i w where Logistic(z) = log[1 + exp(−z)].
w
i=1

In stochastic gradient descent, which is used to train large-scale networks, we randomly choose one
sample, say (xi , yi ) and take a step in the negative gradient direction associated to this sample. Show
that if we use a learning rate of η, the update can be expressed as follows:
 
wk+1 = wk + ηyi xi 1 − Pr(yi | xi , wk ) .

2
Remark 2. Note that Pr(yi | xi , w) is the probability that we predict the training label yi using the
features xi correctly under parameters w and 1 − Pr(yi | xi , w) is the probability of making a mistake.
Hence, we make a larger correction to the current weight vector when the mistake made on the i-th
sample is “larger” in the sense that 1 − Pr(yi | xi , w) is larger. Hence, this update makes sense. If you
are interested in this, a good course to take is MA4270.
8. (Optional) In this problem, we want to show some properties of gradient descent on “nice” functions.
First, we have to make a few definitions.
• We say that f : Rd → R is convex if
f (tx + (1 − t)y) ≤ tf (x) + (1 − t)f (y) for all x, y ∈ Rd , t ∈ [0, 1].
• We say that f is L-smooth if
f (y) − (f (x) + (∇f (x))> (y − x)) ≤ L kx − yk2 for all x, y ∈ Rd ,

2
2
P 2
where k · k denotes the usual `2 norm (i.e., kzk = i zi ).
• We say that f is µ-strongly convex if
µ
f (y) ≥ f (x) + (∇f (x))> (y − x) + ky − xk2 for all x, y ∈ Rd .
2
It can be shown that if f is µ-strongly convex for some µ ≥ 0, it is convex.
(a) Show that if f is L-smooth, then for all x ∈ Rd ,
 1  1
f x − ∇f (x) − f (x) ≤ − k∇f (x)k2
L 2L
and
1
f (x∗ ) − f (x) ≤ − k∇f (x)k2
2L
where x∗ is a global minimizer of f .
(b) Recall that in gradient descent, we implement
xk+1 = xk − η∇f (xk ).
Show that if f is L-smooth and µ-strongly convex, the learning rate η ∈ (0, 1/L], and we start
the iterative procedure at x0 , then
kxk+1 − x∗ k2 ≤ (1 − ηµ)k+1 kx0 − x∗ k2
In particular, if we choose the learning rate η = 1/L, the iterates converge geometrically fast at
a rate µ/L.
Remark 3. This result says that if we want to get ε-close to the optimal solution x∗ (i.e., kxk − x0 k <
ε), we need to use roughly

2 log( kx0 −x
ε
k
)
µ
− log(1 − L )
iterations. The numerator can be interpreted as the log of the ratio of the initial suboptimality (i.e., gap
between x0 and x∗ ), to the final suboptimality (i.e., less than ε). The denominator is − log(1 − µ/L) ≈
µ/L. When µ/L is small, it takes many iterations to get to an ε-close solution. Thus, we favor
situations in which µ/L is large. In fact, L/µ is known as the condition number; the smaller it is, the
better! If you are interested in this, a good course to take is EE5138.
Remark 4. In fact, among the class of all L-smooth, µ-strongly convex functions, the “contraction
factor” (1 − µ/L) is the best possible. See the following:
Drori, Y., Teboulle, M.: Performance of first-order methods for smooth convex minimization: A novel
approach. Math. Program. 145(1), 451–482 (2014)

Common questions

Powered by AI

An L-smooth and µ-strongly convex function assures that gradient descent converges geometrically fast, specifically with the learning rate η = 1/L. The µ-strong convexity ensures a unique global minimizer and L-smoothness guarantees bounded gradients, facilitating efficient convergence without instability. The smaller the condition number L/µ, the faster the convergence, emphasizing the importance of these properties for optimization algorithms .

The condition number L/µ impacts the convergence rate significantly; a smaller L/µ implies faster convergence. It dictates the number of iterations required to achieve a given accuracy in minimizing strongly convex functions. A large condition number can slow convergence, necessitating many iterations to reach an optimal solution, highlighting the need for a well-conditioned problem in gradient descent .

The stochastic gradient descent (SGD) update for logistic regression, using a single randomly selected sample, modifies the weights as wk+1 = wk + ηyixi(1 - Pr(yi | xi, wk)). This update accounts for the probability of a mistake on that sample, ensuring larger corrections when errors are significant. In contrast, batch gradient descent uses the entire dataset, potentially leading to fewer, but more computationally intensive, updates .

Increasing the learning rate from 0.03 to 0.1 might cause the gradient descent to overshoot or become unstable, leading to divergence, while lowering it to 0.001 would slow down convergence significantly. The original learning rate of 0.03 is likely to offer a balance, ensuring convergence without overshooting, as observed in changes of the cost function over iterations .

For the function C(w) = 5w^2, the gradient is 10w. At w0 = 2, the gradient is 20. The learning rate η should satisfy 0 < η < 2/5 for convergence. To converge in one step, η must be 1/5. Divergence occurs when η > 2/5 .

The ReLU activation function is not differentiable at zero, a point of concern theoretically, but this discontinuity is usually ignored in practical deep learning implementations. It is widely used due to its simplicity and efficiency in mitigating the vanishing gradient problem in neural networks, offering sparsity and allowing the model to explore complex representations .

To derive the gradient of C(w) = Σ(f(xi, w) - yi)^4, use the chain rule. The gradient with respect to w is given by ∇w = Σ 4(f(xi, w) - yi)^3 xi, which follows from differentiating the fourth power and applying the chain rule to f(x, w) = x⊤w .

Normalizing the variables is crucial because the exponential function is sensitive to large changes, which can lead to instability and poor convergence. By ensuring the maximum values of the normalized variables are 1, the optimization process is more stable and scales more effectively, avoiding large gradients that can distort updates during gradient descent .

Dividing the 'year' and 'expenditure' variables by their maximum values normalizes the data, which helps stabilize the gradient descent process, preventing extreme swings in parameter updates. This normalization also aligns different temporal scales, improving model accuracy and ensuring predictions are influenced by relative, not absolute discrepancies between scale magnitudes .

The gradient of f(x) = x^4 is f'(x) = 4x^3. With x initialized to 2, f'(2) = 4(2)^3 = 32. The update rule for gradient descent is x_new = x_old - learning_rate * gradient, so x_new = 2 - 0.1 * 32 = 2 - 3.2 = -1.2 .

You might also like