2
0
Fork 0
CVE/test.R

76 lines
2.4 KiB
R

args <- commandArgs(TRUE)
if (length(args) > 0L) {
method <- args[1]
} else {
method <- "simple"
}
if (length(args) > 1L) {
momentum <- as.double(args[2])
} else {
momentum <- 0.0
}
max.iter <- 50L
attempts <- 25L
library(CVE)
path <- paste0('~/Projects/CVE/tmp/logger_', method, '_', momentum, '.C.pdf')
# Define logger for `cve()` method.
logger <- function(iter, attempt, data) {
# Note the `<<-` assignement!
loss.history[iter + 1, attempt] <<- data$loss
error.history[iter + 1, attempt] <<- if (data$err > 0) data$err else NA
tau.history[iter + 1, attempt] <<- data$tau
# Compute true error by comparing to the true `B`
B.est <- null(data$V) # Function provided by CVE
P.est <- B.est %*% solve(t(B.est) %*% B.est) %*% t(B.est)
true.error <- norm(P - P.est, 'F') / sqrt(2 * k)
true.error.history[iter + 1, attempt] <<- true.error
}
pdf(path)
par(mfrow = c(2, 2))
for (name in paste0("M", seq(5))) {
# Seed random number generator
set.seed(42)
# Create a dataset
ds <- dataset(name)
X <- ds$X
Y <- ds$Y
B <- ds$B # the true `B`
k <- ncol(ds$B)
# True projection matrix.
P <- B %*% solve(t(B) %*% B) %*% t(B)
# Setup histories.
V_last <- NULL
loss.history <- matrix(NA, max.iter + 1, attempts)
error.history <- matrix(NA, max.iter + 1, attempts)
tau.history <- matrix(NA, max.iter + 1, attempts)
true.error.history <- matrix(NA, max.iter + 1, attempts)
dr <- cve(Y ~ X, k = k, method = method,
momentum = momentum,
max.iter = max.iter, attempts = attempts,
logger = logger)
# Plot history's
matplot(loss.history, type = 'l', log = 'y', xlab = 'i (iteration)',
main = paste('loss', name),
ylab = expression(L(V[i])))
matplot(true.error.history, type = 'l', log = 'y', xlab = 'i (iteration)',
main = paste('true error', name),
ylab = expression(group('|', B*B^T - B[i]*B[i]^T, '|')[F] / sqrt(2*k)))
matplot(error.history, type = 'l', log = 'y', xlab = 'i (iteration)',
main = paste('error', name),
ylab = expression(group('|', V[i-1]*V[i-1]^T - V[i]*V[i]^T, '|')[F]))
matplot(tau.history, type = 'l', log = 'y', xlab = 'i (iteration)',
main = paste('learning rate', name),
ylab = expression(tau[i]))
}
cat("Created plot:", path, "\n")