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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
### tests for stochastic gradient ascent
###
### do not run unless 'NOT_CRAN' explicitly defined
### (Suggested by Sebastian Meyer and others)
if(!identical(Sys.getenv("NOT_CRAN"), "true")) {
message("We are on CRAN: skipping slow optimizer tests")
q("no")
}
if(!requireNamespace("tinytest", quietly = TRUE)) {
message("These tests require 'tinytest' package\n")
q("no")
}
library(maxLik)
### Test the following things:
###
### 1. basic 2-D SGA
### SGA without function, only gradient
### SGA neither function nor gradient
### SGA in 1-D case
### 2. SGA w/momentum
### 3. SGA full batch
### 4. SGA, no gradient supplied
### SGA, return numeric hessian, gradient provided
### SGA, return numeric hessian, no gradient provided
### SGA, printlevel 1, storeValues
### SGA, NA as iterlim: should give informative error
### SGA, storeValues but no fn (should fail)
###
### using highly unequally scaled data
### SGA without gradient clipping (fails)
### SGA with gradient clipping (works, although does not converge)
## ---------- OLS
## log-likelihood function(s):
## return log-likelihood on validation data
loglik <- function(beta, index) {
e <- yValid - XValid %*% beta
-crossprod(e)/length(y)
}
## gradlik: work on training data
gradlik <- function(beta, index) {
e <- yTrain[index] - XTrain[index,,drop=FALSE] %*% beta
g <- t(-2*t(XTrain[index,,drop=FALSE]) %*% e)
-g/length(index)
}
### create random data
set.seed(1)
N <- 1000
x <- rnorm(N)
X <- cbind(1, x)
y <- 100 + 100*x + rnorm(N)
## training-validation
iTrain <- sample(N, 0.8*N)
XTrain <- X[iTrain,,drop=FALSE]
XValid <- X[-iTrain,,drop=FALSE]
yTrain <- y[iTrain]
yValid <- y[-iTrain]
## Analytic solution (training data):
start <- c(const=10, x=10)
b0 <- drop(solve(crossprod(XTrain)) %*% crossprod(XTrain, yTrain))
names(b0) <- names(start)
tol <- 1e-3 # coefficient tolerance
## ---------- 1. working example
res <- maxSGA(loglik, gradlik, start=start,
control=list(printLevel=0, iterlim=200,
SG_batchSize=100, SG_learningRate=0.1,
storeValues=TRUE),
nObs=length(yTrain))
expect_equal(coef(res), b0, tolerance=tol)
# SGA usually ends with gradient not equal to 0 so we don't test that
## ---------- store parameters
res <- maxSGA(loglik, gradlik, start=start,
control=list(printLevel=0, iterlim=20,
SG_batchSize=100, SG_learningRate=0.1,
storeParameters=TRUE),
nObs=length(yTrain))
expect_equal(dim(storedParameters(res)), c(1 + nIter(res), 2))
## ---------- no function, only gradient
expect_silent(
res <- maxSGA(grad=gradlik, start=start,
control=list(printLevel=0, iterlim=10, SG_batchSize=100),
nObs=length(yTrain))
)
## ---------- neither function nor gradient
expect_error(
res <- maxSGA(start=start,
control=list(printLevel=0, iterlim=10, SG_batchSize=100),
nObs=length(yTrain))
)
## ---------- 1D case
N1 <- 1000
t <- rexp(N1, 2)
loglik1 <- function(theta, index) sum(log(theta) - theta*t[index])
gradlik1 <- function(theta, index) sum(1/theta - t[index])
expect_silent(
res <- maxSGA(loglik1, gradlik1, start=1,
control=list(iterlim=300, SG_batchSize=20), nObs=length(t))
)
expect_equal(coef(res), 1/mean(t), tolerance=0.2)
expect_null(hessian(res))
## ---------- 2. SGA with momentum
expect_silent(
res <- maxSGA(loglik, gradlik, start=start,
control=list(printLevel=0, iterlim=200,
SG_batchSize=100, SG_learningRate=0.1, SGA_momentum=0.9),
nObs=length(yTrain))
)
expect_equal(coef(res), b0, tolerance=tol)
## ---------- 3. full batch
expect_silent(
res <- maxSGA(loglik, gradlik, start=start,
control=list(printLevel=0, iterlim=200,
SG_batchSize=NULL, SG_learningRate=0.1),
nObs=length(yTrain))
)
expect_equal(coef(res), b0, tolerance=tol)
## ---------- 4. no gradient
expect_silent(
res <- maxSGA(loglik, start=start,
control=list(iterlim=1000, SG_learningRate=0.02), nObs=length(yTrain))
)
expect_equal(coef(res), b0, tolerance=tol)
## ---------- return Hessian, gradient provided
expect_silent(
res <- maxSGA(loglik, gradlik, start=start,
control=list(iterlim=1000, SG_learningRate=0.02),
nObs=length(yTrain),
finalHessian=TRUE)
)
expect_equal(coef(res), b0, tolerance=tol)
expect_equal(dim(hessian(res)), c(2,2))
## ---------- return Hessian, no gradient
expect_silent(
res <- maxSGA(loglik, start=start,
control=list(iterlim=1000, SG_learningRate=0.02),
nObs=length(yTrain),
finalHessian=TRUE)
)
expect_equal(coef(res), b0, tolerance=tol)
expect_equal(dim(hessian(res)), c(2,2))
### ---------- SGA, printlevel 1, storeValues ----------
### it should just work
expect_silent(
res <- maxSGA(loglik, gradlik, start=start,
control=list(iterlim=2, storeValues=TRUE, printLevel=1),
nObs=length(yTrain),
finalHessian=TRUE)
)
### ---------- SGA, NA as iterlim ----------
### should give informative error
expect_error(
res <- maxSGA(loglik, gradlik, start=start,
control=list(iterlim=NA),
nObs=length(yTrain),
finalHessian=TRUE),
pattern = "invalid class \"MaxControl\" object: NA in 'iterlim'"
)
### ---------- SGA, fn missing but storeValues=TRUE
### should give informative error
expect_error(
res <- maxSGA(grad=gradlik, start=start,
control=list(iterlim=10, storeValues=TRUE),
nObs=length(yTrain)),
pattern = "Cannot compute the objective function value: no objective function supplied"
)
## ---------- gradient by observations
gradlikO <- function(beta, index) {
e <- yTrain[index] - XTrain[index,,drop=FALSE] %*% beta
g <- -2*drop(e)*XTrain[index,,drop=FALSE]
-g/length(index)
}
expect_silent(
res <- maxSGA(grad=gradlikO, start=start,
control=list(printLevel=0, iterlim=100,
SG_batchSize=100),
nObs=length(yTrain))
)
expect_equal(coef(res), b0, tolerance=tol)
## ---------- 0 iterations
expect_silent(
res <- maxSGA(grad=gradlik, start=start,
control=list(iterlim=0),
nObs=length(yTrain))
)
expect_equal(coef(res), start)
# should return start values exactly
### -------------------- create unequally scaled data
set.seed(1)
N <- 1000
x <- rnorm(N, sd=100)
XTrain <- cbind(1, x)
yTrain <- 1 + x + rnorm(N)
start <- c(const=10, x=10)
## ---------- no gradient clipping:
## should fail with informative "NA/Inf in gradient" message
expect_error(
res <- maxSGA(loglik, gradlik, start=start,
control=list(iterlim=100, SG_learningRate=0.5),
nObs=length(yTrain)),
pattern = "NA/Inf in gradient"
)
## ---------- gradient clipping: should not fail
expect_silent(
res <- maxSGA(loglik, gradlik, start=start,
control=list(iterlim=100, SG_learningRate=0.5,
SG_clip=1e6),
nObs=length(yTrain)
)
)
|