# Werden wir Helden für einen Tag

Posted on Dec 24, 2011 by Chung-hong Chan

[code]
## < -
## http://www.chainsawriot.com
stoGradDesc <- function(y,X, theta, alpha, rep.time=1, accumJ=100, jplot=FALSE) {
tempJ <- c()
Jhist <- c()
for (r in 1:rep.time) {
for (i in 1:length(y)) {
hx <- X[i,] %*% theta
J <- 0.5 * (hx-y[i])^2
newtheta <- theta - (alpha * t((hx-y[i]) %*% X[i,]))
theta <- newtheta
tempJ <- append(tempJ, J)
if (i %% accumJ == 0) {
aveJ <- mean(tempJ)
Jhist <- append(Jhist, aveJ)
tempJ <- c()
}
}
}
if (jplot) {
plot(Jhist, type="l", ylab="Cost", xlab="Batch")
}
return(theta)
}
cuhk.y <- cuhk$Weight cuhk.x <- scale(cuhk$Height)
cuhk.X <- matrix(c(rep(1, length(cuhk.x)), cuhk.x), byrow=FALSE, nrow=length(cuhk.x), ncol=2)
ran.order <- sample.int(length(cuhk.y))
ran.y <- cuhk.y[ran.order]
ran.X <- cuhk.X[ran.order,]
initheta <- matrix(c(0,0), byrow=FALSE)
SGDtheta <- stoGradDesc(ran.y, ran.X, initheta, 0.001, 2, 100, TRUE) #converged

epsilon.converge <- function(y, X, theta, inialpha=0.0001, epsilon=0.0001, maxround=40) {
delta <- 1
round.no <- 1
while (delta > epsilon & round.no < maxround) {
SGDtheta <- stoGradDesc(ran.y, ran.X, theta, inialpha)
theta.new <- SGDtheta
delta <- max(abs(theta - theta.new))
theta <- theta.new
round.no <- round.no+1
}
return(list(SGDtheta=SGDtheta, round.no=round.no))
}
epsilon.converge(ran.y, ran.X, initheta, inialpha=0.0001)
[/code]

line 3-24:

SGD 的 implementation 。

line 25-31:

line 32-33:

line 35-47:

Powered by Jekyll and profdr theme, a fork of true minimal theme