-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradDescent.R
42 lines (37 loc) · 846 Bytes
/
gradDescent.R
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
#create some values
xs <- seq(0,4,len=20)
#define the function we want to optimize
f<- function(x)
{
1.2 * (x-2)^2 + 3.2
}
#Plot the function
plot(xs, f(xs), type="l", xlab="x",ylab=expression(1.2 * (x-2)^2 + 3.2))
#Calculate the gradient of the function
grad <- function(x)
{
1.2*2*(x-2)
}
#Closed form solution
lines(c(2,2), c(3,8), col="red",lty=2)
text(2.1, 7, "Closed Form Solution", col="red", pos=4)
#Implementation of gradient descent
#Initialize the first guess
x<-0.1
#store initial x values
xtrace<-x
#store the y-values
ftrace<-f(x)
# The learning rate alpha
alp <- 0.6
for (step in 1: 100)
{
#gradient descent step
x <- x - alp * grad(x)
xtrace <- c(xtrace,x)
ftrace <- c(ftrace, f(x))
}
lines(xtrace, ftrace, type = "b", col="blue")
text(0.5, 6, "Gradient Descent", col = "blue", pos=4)
#print final value of x
print(x)