Skip to content

Instantly share code, notes, and snippets.

@chiral
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiral/49c20238704cec2937ca to your computer and use it in GitHub Desktop.
Save chiral/49c20238704cec2937ca to your computer and use it in GitHub Desktop.
An implementation in R for "Exact Soft Confidence-Weighted Learning" ( http://icml.cc/2012/papers/86.pdf )
library("rjson")
scw <- function(D,eta,verbose=F) {
phi <- qnorm(eta)
psi <- 1+phi^2/2
zeta <- 1+phi^2
mu <- rep(0,D)
sigma <- diag(D)
predict <- function(x) {
tmp <- sign(sum(mu*x))
if (verbose) print(paste("y=",tmp))
return(tmp)
}
loss <- function(m,v) {
return(max(0,phi*sqrt(v)-m))
}
alpha <- function(m,v) {
tmp <- -m*psi+sqrt(m^2*phi^4/4+v*phi^2*zeta)
tmp <- tmp/(v*zeta)
return(max(0,tmp))
}
calc_u <- function(a,v) {
avphi <- a*v*phi
tmp <- -avphi + sqrt(avphi^2+4*v)
tmp <- tmp^2/4
return(tmp)
}
beta <- function(a,u,v) {
tmp <- a*phi/(sqrt(u)+v*a*phi) # vector
tmp <- sum(tmp) # scalar
return(tmp)
}
step <- function(x,y) {
y1 <- predict(x)
m <- y * sum(mu*x)
v <- sum(t(x) %*% sigma %*% x)
if (loss(m,v)>=0) {
a <- alpha(m,v)
u <- calc_u(a,v)
b <- beta(a,u,v)
x1 <- sigma %*% x
mu <<- mu+a*y*x1
sigma <<- sigma-b*(x1 %*% t(x1))
}
return(y1)
}
return(list(step=step,predict=predict))
}
test1 <- function(eta) {
print("***test1***")
f <- scw(2,eta,verbose=T)
f$step(c(1,1),1)
f$step(c(0.3,0.4),1)
f$step(c(-1,-1),-1)
f$step(c(-1,-1),-1)
f$step(c(0.1,0.1),-1)
f$predict(c(0.1,0.1))
}
test2 <- function(eta) {
print("***test2***")
# data set is here.
# https://github.com/IshitaTakeshi/Hackathon/tree/master/MLAkiba2/Code
f <- scw(64,eta)
train <- fromJSON(paste(readLines("digits_train.json"), collapse=""))
train.result <- c()
for (i in 1:length(train$images)) {
tmp <- f$step(train$images[[i]],train$labels[i])
train.result <- c(train.result,tmp)
}
print(table(data.frame(train=train$labels,result=train.result)))
test <- fromJSON(paste(readLines("digits_test.json"), collapse=""))
test.result <- c()
for (i in 1:length(test$images)) {
tmp <- f$predict(test$images[[i]])
test.result <- c(test.result,tmp)
}
print(table(data.frame(test=test$labels,result=test.result)))
}
test1(0.9)
test2(0.9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment