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
|
#Kernel Hebbian Algorithm function
setGeneric("kha",function(x, ...) standardGeneric("kha"))
setMethod("kha", signature(x = "formula"),
function(x, data = NULL, na.action = na.omit, ...)
{
mt <- terms(x, data = data)
if(attr(mt, "response") > 0) stop("response not allowed in formula")
attr(mt, "intercept") <- 0
cl <- match.call()
mf <- match.call(expand.dots = FALSE)
mf$formula <- mf$x
mf$... <- NULL
mf[[1L]] <- quote(stats::model.frame)
mf <- eval(mf, parent.frame())
na.act <- attr(mf, "na.action")
Terms <- attr(mf, "terms")
x <- model.matrix(mt, mf)
res <- kha(x, ...)
## fix up call to refer to the generic, but leave arg name as `formula'
cl[[1]] <- as.name("kha")
kcall(res) <- cl
attr(Terms,"intercept") <- 0
terms(res) <- Terms
if(!is.null(na.act))
n.action(res) <- na.act
return(res)
})
setMethod("kha",signature(x="matrix"),
function(x, kernel = "rbfdot", kpar = list(sigma = 0.1),
features = 5, eta = 0.005, th = 1e-4, maxiter = 10000, verbose = FALSE, na.action = na.omit, ...)
{
x <- na.action(x)
x <- as.matrix(x)
m <- nrow(x)
ret <- new("kha")
if(!is(kernel,"kernel"))
{
if(is(kernel,"function")) kernel <- deparse(substitute(kernel))
kernel <- do.call(kernel, kpar)
}
if(!is(kernel,"kernel")) stop("kernel must inherit from class `kernel'")
## Initialize A dual variables
A <- matrix(runif(features*m),m,features)*2 - 1
AOld <- A
## compute square norm of data
a <- rowSums(x^2)
## initialize the empirical sum kernel map
eskm <- rep(0,m)
for (i in 1:m)
eskm[i] <- sum(kernelFast(kernel,x,x[i,,drop=FALSE], a))
eks <- sum(eskm)
counter <- 0
step <- th + 1
Aold <- A
while(step > th && counter < maxiter)
{
y <- rep(0, features)
ot <- rep(0,m)
## Hebbian Iteration
for (i in 1:m)
{
## compute y output
etkm <- as.vector(kernelFast(kernel,x,x[i,,drop=FALSE], a))
sum1 <- as.vector(etkm %*% A)
sum2 <- as.vector(eskm%*%A)/m
asum <- colSums(A)
sum3 <- as.vector(eskm[i]*asum)/m
sum4 <- as.vector(eks * asum)/m^2
y <- sum1 - sum2 - sum3 + sum4
## update A
yy <- y%*%t(y)
yy[upper.tri(yy)] <- 0
tA <- t(A)
A <- t(tA - eta * yy%*%tA)
A[i,] <- A[i,] + eta * y
}
if (counter %% 100 == 0 )
{
step = mean(abs(Aold - A))
Aold <- A
if(verbose)
cat("Iteration :", counter, "Converged :", step,"\n")
}
counter <- counter + 1
}
## Normalize in Feature space
cA <- t(A) - colSums(A)
Fnorm <- rep(0,features)
for (j in 1:m)
Fnorm <- Fnorm + colSums(t(cA[,j] * cA) * as.vector(kernelFast(kernel,x,x[j,,drop=FALSE],a)))
if(any(Fnorm==0))
{
warning("Normalization vector contains zeros, replacing them with ones")
Fnorm[which(Fnorm==0)] <- 1
}
A <- t(t(A)/sqrt(Fnorm))
pcv(ret) <- A
eig(ret) <- Fnorm
names(eig(ret)) <- paste("Comp.", 1:features, sep = "")
eskm(ret) <- eskm
kcall(ret) <- match.call()
kernelf(ret) <- kernel
xmatrix(ret) <- x
return(ret)
})
## Project a new matrix into the feature space
setMethod("predict",signature(object="kha"),
function(object , x)
{
if (!is.null(terms(object)))
{
if(!is.matrix(x))
x <- model.matrix(delete.response(terms(object)), as.data.frame(x), na.action = n.action(object))
}
else
x <- if (is.vector(x)) t(t(x)) else as.matrix(x)
if (is.vector(x)||is.data.frame(x))
x<-as.matrix(x)
if (!is.matrix(x)) stop("x must be a matrix a vector or a data frame")
n <- nrow(x)
m <- nrow(xmatrix(object))
A <- pcv(object)
y <- matrix(0,n,dim(A)[2])
eks <- sum(eskm(object))
a <- rowSums(xmatrix(object)^2)
## Project data
sum2 <- as.vector(eskm(object)%*%A)/m
asum <- colSums(A)
sum4 <- as.vector(eks * asum)/m^2
for (i in 1:n)
{
## compute y output
etkm <- as.vector(kernelFast(kernelf(object),xmatrix(object),x[i,,drop=FALSE], a))
sum1 <- as.vector(etkm %*% A)
sum3 <- sum(etkm)*asum/m
y[i,] <- sum1 - sum2 - sum3 + sum4
}
return(y)
})
|