2019-09-16 09:28:06 +00:00
|
|
|
|
2019-09-25 11:53:45 +00:00
|
|
|
args <- commandArgs(TRUE)
|
2019-11-20 18:03:21 +00:00
|
|
|
if (length(args) > 0L) {
|
2019-09-25 11:53:45 +00:00
|
|
|
method <- args[1]
|
|
|
|
} else {
|
|
|
|
method <- "simple"
|
|
|
|
}
|
2019-11-22 08:32:14 +00:00
|
|
|
if (length(args) > 1L) {
|
2019-11-20 18:03:21 +00:00
|
|
|
momentum <- as.double(args[2])
|
|
|
|
} else {
|
|
|
|
momentum <- 0.0
|
|
|
|
}
|
2019-11-22 08:32:14 +00:00
|
|
|
max.iter <- 50L
|
2019-09-25 11:53:45 +00:00
|
|
|
attempts <- 25L
|
2019-09-16 09:28:06 +00:00
|
|
|
|
2019-09-25 11:53:45 +00:00
|
|
|
library(CVE)
|
2019-11-25 19:49:43 +00:00
|
|
|
path <- paste0('~/Projects/CVE/tmp/logger_', method, '_', momentum, '.C.pdf')
|
2019-09-16 09:28:06 +00:00
|
|
|
|
2019-09-25 11:53:45 +00:00
|
|
|
# Define logger for `cve()` method.
|
2019-11-25 19:49:43 +00:00
|
|
|
logger <- function(iter, attempt, data) {
|
2019-09-16 09:28:06 +00:00
|
|
|
# Note the `<<-` assignement!
|
2019-11-25 19:49:43 +00:00
|
|
|
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
|
2019-09-16 09:28:06 +00:00
|
|
|
# Compute true error by comparing to the true `B`
|
2019-11-25 19:49:43 +00:00
|
|
|
B.est <- null(data$V) # Function provided by CVE
|
2019-09-16 09:28:06 +00:00
|
|
|
P.est <- B.est %*% solve(t(B.est) %*% B.est) %*% t(B.est)
|
|
|
|
true.error <- norm(P - P.est, 'F') / sqrt(2 * k)
|
2019-11-25 19:49:43 +00:00
|
|
|
true.error.history[iter + 1, attempt] <<- true.error
|
2019-09-16 09:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2019-09-25 11:53:45 +00:00
|
|
|
V_last <- NULL
|
2019-11-22 08:32:14 +00:00
|
|
|
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)
|
2019-09-16 09:28:06 +00:00
|
|
|
|
2019-09-25 11:53:45 +00:00
|
|
|
dr <- cve(Y ~ X, k = k, method = method,
|
2019-11-20 18:03:21 +00:00
|
|
|
momentum = momentum,
|
2019-11-22 08:32:14 +00:00
|
|
|
max.iter = max.iter, attempts = attempts,
|
2019-09-25 11:53:45 +00:00
|
|
|
logger = logger)
|
2019-09-16 09:28:06 +00:00
|
|
|
|
|
|
|
# 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),
|
2019-09-25 11:53:45 +00:00
|
|
|
ylab = expression(group('|', B*B^T - B[i]*B[i]^T, '|')[F] / sqrt(2*k)))
|
2019-09-16 09:28:06 +00:00
|
|
|
matplot(error.history, type = 'l', log = 'y', xlab = 'i (iteration)',
|
|
|
|
main = paste('error', name),
|
2019-09-25 11:53:45 +00:00
|
|
|
ylab = expression(group('|', V[i-1]*V[i-1]^T - V[i]*V[i]^T, '|')[F]))
|
2019-09-16 09:28:06 +00:00
|
|
|
matplot(tau.history, type = 'l', log = 'y', xlab = 'i (iteration)',
|
|
|
|
main = paste('learning rate', name),
|
|
|
|
ylab = expression(tau[i]))
|
2019-09-25 11:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cat("Created plot:", path, "\n")
|