2
0
Fork 0
CVE/test.R

84 lines
2.5 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
}
epochs <- 50L
attempts <- 25L
# library(CVEpureR)
# path <- paste0('~/Projects/CVE/tmp/logger_', method, '.R.pdf')
library(CVE)
path <- paste0('~/Projects/CVE/tmp/logger_', method, '.C.pdf')
# Define logger for `cve()` method.
logger <- function(epoch, attempt, L, V, tau) {
# Note the `<<-` assignement!
loss.history[epoch + 1, attempt] <<- mean(L)
if (epoch == 0) {
error <- NA
} else {
error <- norm(V %*% t(V) - V_last %*% t(V_last), type = 'F')
}
V_last <<- V
error.history[epoch + 1, attempt] <<- error
tau.history[epoch + 1, attempt] <<- tau
# Compute true error by comparing to the true `B`
B.est <- null(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[epoch + 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, epochs + 1, attempts)
error.history <- matrix(NA, epochs + 1, attempts)
tau.history <- matrix(NA, epochs + 1, attempts)
true.error.history <- matrix(NA, epochs + 1, attempts)
dr <- cve(Y ~ X, k = k, method = method,
momentum = momentum,
epochs = epochs, 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")