File: onlearn.R

package info (click to toggle)
r-cran-kernlab 0.9-33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,216 kB
  • sloc: cpp: 6,011; ansic: 935; makefile: 2
file content (196 lines) | stat: -rw-r--r-- 7,615 bytes parent folder | download | duplicates (5)
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
## kernel based on-line learning algorithms for classification, novelty detection and regression. 
##
## created 15.09.04 alexandros
## updated

setGeneric("onlearn",function(obj, x, y = NULL, nu = 0.2, lambda = 1e-4) standardGeneric("onlearn"))
setMethod("onlearn", signature(obj = "onlearn"),
          function(obj , x, y = NULL, nu = 0.2, lambda = 1e-4)
          {
            if(onstart(obj) == 1 && onstop(obj) < buffer(obj))
              buffernotfull  <- TRUE
            else
              buffernotfull <- FALSE

            if(is.vector(x))
              x <- matrix(x,,length(x))  
            d <- dim(x)[2]
            
         
            for (i in 1:dim(x)[1])
              {
                xt <- x[i,,drop=FALSE]
                yt <- y[i]
            if(type(obj)=="novelty")
              {
                phi <- fit(obj)
                if(phi < 0)
                  {
                    alpha(obj) <- (1-lambda) * alpha(obj)
                    if(buffernotfull)
                      onstop(obj) <- onstop(obj) + 1
                    else{
                      onstop(obj) <- onstop(obj)%%buffer(obj) + 1
                      onstart(obj) <- onstart(obj)%%buffer(obj) +1
                    }
                    alpha(obj)[onstop(obj)] <- lambda
                    xmatrix(obj)[onstop(obj),] <- xt
                    rho(obj) <- rho(obj) + lambda*(nu-1)
                  }
                else
                  rho(obj) <- rho(obj) + lambda*nu
                
                rho(obj) <- max(rho(obj), 0)

                if(onstart(obj) == 1 && onstop(obj) < buffer(obj))
                  fit(obj) <- drop(kernelMult(kernelf(obj), xt, matrix(xmatrix(obj)[1:onstop(obj),],ncol=d), matrix(alpha(obj)[1:onstop(obj)],ncol=1)) - rho(obj)) 
                else
                  fit(obj) <- drop(kernelMult(kernelf(obj), xt, xmatrix(obj), matrix(alpha(obj),ncol=1)) - rho(obj))
              }
            if(type(obj)=="classification")
              { 
                if(is.null(pattern(obj)) && is.factor(y))
                  pattern(obj) <- yt
                if(!is.null(pattern(obj)))
                  if(pattern(obj) == yt)
                    yt <- 1
                  else yt <-  -1
                
                phi <- fit(obj)
                
                alpha(obj) <- (1-lambda) * alpha(obj)

                if(yt*phi < rho(obj))
                  {
                    if(buffernotfull)
                      onstop(obj) <- onstop(obj) + 1
                    else{
                      onstop(obj) <- onstop(obj)%%buffer(obj) + 1
                      onstart(obj) <- onstart(obj)%%buffer(obj) +1
                    }
                    alpha(obj)[onstop(obj)] <- lambda*yt
                    b(obj) <- b(obj) + lambda*yt
                    xmatrix(obj)[onstop(obj),] <- xt
                    rho(obj) <- rho(obj) + lambda*(nu-1) ## (1-nu) ??
                  }
                else
                  rho(obj) <- rho(obj) + lambda*nu
                
                rho(obj) <- max(rho(obj), 0)

                if(onstart(obj) == 1 && onstop(obj) < buffer(obj))
                  fit(obj) <- drop(kernelMult(kernelf(obj), xt, xmatrix(obj)[1:onstop(obj),,drop=FALSE], matrix(alpha(obj)[1:onstop(obj)],ncol=1)) + b(obj))
                else
                  fit(obj) <-drop(kernelMult(kernelf(obj), xt, xmatrix(obj), matrix(alpha(obj),ncol=1)) + b(obj))
          
              }

            if(type(obj)=="regression")
              {
                alpha(obj) <- (1-lambda) * alpha(obj)
                phi <- fit(obj)
                
                if(abs(-phi) < rho(obj))
                  {
                    if(buffernotfull)
                      onstop(obj) <- onstop(obj) + 1
                    else{
                      onstop(obj) <- onstop(obj)%%buffer(obj) + 1
                      onstart(obj) <- onstart(obj)%% buffer(obj) +1
                    }
                    alpha(obj)[onstop(obj)] <- sign(yt-phi)*lambda
                    xmatrix(obj)[onstop(obj),] <- xt
                    rho(obj) <- rho(obj) + lambda*(1-nu) ## (1-nu) ??
                  }
                else{
                  rho(obj) <- rho(obj) - lambda*nu
                  alpha(obj)[onstop(obj)] <- sign(yt-phi)/rho(obj)
                }
                if(onstart(obj) == 1 && onstop(obj) < buffer(obj))
                  fit(obj) <- drop(kernelMult(kernelf(obj), xt, matrix(xmatrix(obj)[1:onstop(obj),],ncol=d), matrix(alpha(obj)[1:onstop(obj)],ncol=1)) + b(obj)) 
                else
                  fit(obj) <- drop(kernelMult(kernelf(obj), xt, xmatrix(obj), matrix(alpha(obj),ncol=1)) + b(obj)) 
              }
          }
            return(obj)
          })


setGeneric("inlearn",function(d, kernel = "rbfdot", kpar = list(sigma=0.1), type = "novelty", buffersize = 1000) standardGeneric("inlearn"))
setMethod("inlearn", signature(d = "numeric"),
          function(d ,kernel = "rbfdot", kpar = list(sigma=0.1), type = "novelty", buffersize = 1000)
          {
            obj <- new("onlearn")

            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'")

            type(obj) <- match.arg(type,c("novelty","classification","regression"))
            xmatrix(obj) <- matrix(0,buffersize,d)
            kernelf(obj) <- kernel
            onstart(obj) <- 1
            onstop(obj) <- 1
            fit(obj) <- 0 
            b(obj) <- 0
            alpha(obj) <- rep(0, buffersize)
            rho(obj) <- 0
            buffer(obj) <- buffersize
            return(obj)
          })


setMethod("show","onlearn",
function(object){
  cat("On-line learning object of class \"onlearn\"","\n")
  cat("\n")
  cat(paste("Learning problem :", type(object), "\n"))
  cat
  cat(paste("Data dimensions :", dim(xmatrix(object))[2], "\n"))
  cat(paste("Buffersize :", buffer(object), "\n"))
  cat("\n")
 show(kernelf(object))
})


setMethod("predict",signature(object="onlearn"),
function(object, x)
  {
    if(is.vector(x))
      x<- matrix(x,1)

    d <- dim(xmatrix(object))[2]
          
    if(type(object)=="novelty")
      {
        if(onstart(object) == 1 && onstop(object) < buffer(object))
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object)[1:onstop(object),],ncol= d), matrix(alpha(object)[1:onstop(object)],ncol=1)) - rho(object)) 
        else
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object),ncol=d), matrix(alpha(object),ncol=1)) - rho(object))
      }

    if(type(object)=="classification")
      {
        if(onstart(object) == 1 && onstop(object) < buffer(object))
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object)[1:onstop(object),],ncol=d), matrix(alpha(object)[1:onstop(object)],ncol=1)) + b(object))
        else
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object),ncol=d), matrix(alpha(object),ncol=1)) + b(object))
       
      }

    if(type(object)=="regression")
      {
        if(onstart(object) == 1 && onstop(object) < buffer(object))
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object)[1:onstop(object),],ncol=d), matrix(alpha(object)[1:onstop(object)],ncol=1)) + b(object)) 
        else
          res <- drop(kernelMult(kernelf(object), x, matrix(xmatrix(object),ncol=d), matrix(alpha(object),ncol=1)) + b(object)) 
      }

    return(res)
    
  })