generated from jtr13/cctemplate
-
Notifications
You must be signed in to change notification settings - Fork 67
/
gradient_descent.Rmd
228 lines (180 loc) · 6.48 KB
/
gradient_descent.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Gradient Descent Algorithm in R
Cong Chen
```{r , include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Motivation
The Gradient Descent Algorithm is a popular algorithm in optimization. However, I seldom see people apply the algorithm by R, so I try to apply the Gradient Descent Algorithm to a simple optimization problem by R. And I also use three different step size rules when applying Gradient Descent Algorithm and drawing the contour map to give a more comprehensive understanding about the difference between these methods.
Through the project, I understood the principle of the algorithm more clearly and was skilled in applying the algorithm. And I also realized how important choosing initial values are. Maybe the next time, I will try other different step size rules.
## Gradient Descent Algorithm
Gradient Descent Algorithm is an algorithm used widely in optimization. For a simple optimization problem:
$$
\min_{x\in X}f(x)
$$
where $X$ is the domain of variable $x$, the algorithm's specific steps are shown below:
1. Choose the initial value of $x$ called $x^{(0)}$.
2. Choose the stopping condition value of the algorithm $\varepsilon$.
3. Select a specific step size rule to determine $\alpha^{(t)}$ for $t=0,1,...$.
4. For $t=0,1,...$, update:
$$
x^{(t+1)}=x^{(t)}-\alpha^{(t)}\nabla f(x^{(t)})
$$
5. If $\lVert x^{(t+1)}-x^{(t)} \rVert\leq \varepsilon$, stop the algorithm.
## Three common step size rules
There are three common step size rules:
1. Fixed: $\alpha^{(t)}$ is a constant.
2. Exact line search
3. Backtracking line search
Exact line search method tries to find the best $\alpha^{(t)}$ in each step $t$, that is:
$$
\alpha^{(t)}=\arg\min_{\alpha} (x^{(t)}-\alpha\nabla f(x^{(t)}))
$$
Backtracking line search method tries to reduce the step size as the iteration $t$ increases.
## A simple problem
Apply these three methods to a simple optimization problem to find its optimal solution. And also, compare the difference between these three methods based on the results.
A simple optimization problem:
$$
\min_{x=(x_1,x_2)} f(x)\\
f(x)=\frac{10x_1^2+x_2^2}{2}
$$
```{r function}
# function f(x)
f<-function(x){
return((10*x[1]^2+x[2]^2)/2)
}
# gradient of f(x)
grad<-function(x){
return(c(10*x[1],x[2]))
}
```
In all three methods, the search begins at the point $x^{(0)}=(1.5,-1.5)$.
### Fixed step size
```{r fixed}
fixstep<-function(x0, grad, tol = 1e-6, alpha = 0.01, max_iteration = 1000){
k = 1
xf = x0
xl = xf - alpha * grad(xf)
x = c(x0,xl)
while (sqrt(sum((xl - xf)^2)) > tol && k < max_iteration){
xf = xl
xl = xf - alpha * grad(xf)
k = k + 1
x = c(x, xl)
}
return(list(iteration = k - 1,
x1 = x[seq(1, 2 * k - 1, 2)],
x2 = x[seq(2, 2 * k, 2)]))
}
```
try three different fixed step size to observe their different processes:
1. Small: $\alpha = 0.01$.
2. Medium: $\alpha = 0.1$.
3. Large: $\alpha = 0.7$.
```{r plot contour for fixed step}
# implement the method
step1 = fixstep(c(1.5, -1.5), grad, alpha = 0.01)
step2 = fixstep(c(1.5, -1.5), grad, alpha = 0.1)
step3 = fixstep(c(1.5, -1.5), grad, alpha = 0.7, max_iteration = 50)
z = matrix(0, 100, 100)
x1 = seq(-1.5, 1.5, length = 100)
x2 = seq(-1.5, 1.5, length = 100)
# store function value for every grid point
for(i in 1:100){
for(j in 1:100){
z[i,j] = f(c(x1[i],x2[j]))
}
}
# plot contour map
contour(x1, x2, z, nlevels=20)
for(i in 1:step1$iteration){
segments(step1$x1[i],step1$x2[i],step1$x1[i+1],step1$x2[i+1],lty=2,col='red')
}
for(i in 1:step2$iteration){
segments(step2$x1[i],step2$x2[i],step2$x1[i+1],step2$x2[i+1],lty=2,col='blue')
}
for(i in 1:step3$iteration){
segments(step3$x1[i],step3$x2[i],step3$x1[i+1],step3$x2[i+1],lty=2,col='green')
}
```
The small $\alpha$ (red line in the graph) takes more iteration to find the optimal solution, while the large $\alpha$ (green line in the graph) leads huge fluctuations in search, which makes it difficult to find the optimal solution.
### Exact line search
```{r exact line search}
exactline<-function(x0, grad, tol = 1e-6, max_iteration = 1000){
A = matrix(c(10, 0, 0, 1), ncol = 2)
k = 1
xf = x0
r = matrix(-grad(xf), ncol = 1)
alpha = as.numeric((t(r)%*%r)/(t(r)%*%A%*%r))
xl = xf - alpha * grad(xf)
x = c(x0,xl)
while (sqrt(sum((xl - xf)^2)) > tol && k < max_iteration){
xf = xl
r = matrix(-grad(xf), ncol = 1)
alpha = as.numeric((t(r)%*%r)/(t(r)%*%A%*%r))
xl = xf - alpha * grad(xf)
k = k + 1
x = c(x, xl)
}
return(list(iteration = k - 1,
x1 = x[seq(1, 2 * k - 1, 2)],
x2 = x[seq(2, 2 * k, 2)]))
}
```
```{r plot contour for exact line search}
# implement the method
step1 = exactline(c(1.5, -1.5),grad)
z = matrix(0, 100, 100)
x1 = seq(-1.5, 1.5, length = 100)
x2 = seq(-1.5, 1.5, length = 100)
# store function value for every grid point
for(i in 1:100){
for(j in 1:100){
z[i,j] = f(c(x1[i],x2[j]))
}
}
# plot contour map
contour(x1, x2, z, nlevels=20)
for(i in 1:step1$iteration){
segments(step1$x1[i],step1$x2[i],step1$x1[i+1],step1$x2[i+1],lty=2,col='red')
}
```
Although every step of exact line search is exact, exact line search takes more time compared with others.
### Backtracking Line Search
```{r backtracking line search}
backtracking<-function(x0, grad, tol = 1e-6, alpha = 0.01, beta = 0.8,max_iteration = 1000){
k = 1
xf = x0
xl = xf - alpha * grad(xf)
x = c(x0, xl)
while (sqrt(sum((xl - xf)^2)) > tol && k < max_iteration){
xf = xl
alpha = beta * alpha
xl = xf - alpha * grad(xf)
k = k + 1
x = c(x, xl)
}
return(list(iteration = k - 1,
x1 = x[seq(1, 2 * k - 1, 2)],
x2 = x[seq(2, 2 * k, 2)]))
}
```
Choose initial $\alpha = 0.2$, and factor $\beta = 0.8$
```{r plot}
# implement the method
step1 = backtracking(c(1.5, -1.5), grad, alpha = 0.2, beta = 0.8)
z = matrix(0,100,100)
x1 = seq(-1.5, 1.5, length = 100)
x2 = seq(-1.5, 1.5, length = 100)
# store function value for every grid point
for(i in 1:100){
for(j in 1:100){
z[i,j] = f(c(x1[i], x2[j]))
}
}
# plot contour map
contour(x1, x2, z, nlevels=20)
for(i in 1:step1$iteration){
segments(step1$x1[i],step1$x2[i],step1$x1[i+1],step1$x2[i+1],lty=2,col='red')
}
```
The large initial $\alpha$ can be used in backtracking line search, since after several steps $\alpha$ becomes really small.