2021-09-07 11:00:52 +00:00
|
|
|
#!/usr/bin/env Rscript
|
|
|
|
|
|
|
|
Sys.setenv(TF_CPP_MIN_LOG_LEVEL = "3") # Suppress `tensorflow` notes/warnings
|
|
|
|
suppressPackageStartupMessages({
|
2021-09-10 12:58:47 +00:00
|
|
|
library(dr)
|
2021-09-07 11:00:52 +00:00
|
|
|
library(NNSDR)
|
|
|
|
})
|
|
|
|
|
|
|
|
## Parse script parameters
|
|
|
|
args <- parse.args(defaults = list(
|
|
|
|
# Simulation configuration
|
|
|
|
reps = 100, # Number of replications
|
2021-09-10 12:58:47 +00:00
|
|
|
dataset = 'B1', # Name ('B' for Binary) of the data set
|
2021-09-07 11:00:52 +00:00
|
|
|
# Neuronal Net. structure/definitions
|
|
|
|
hidden_units = 512L,
|
|
|
|
activation = 'relu',
|
|
|
|
trainable_reduction = TRUE,
|
|
|
|
# Neuronal Net. training
|
2021-09-10 12:58:47 +00:00
|
|
|
epochs = c(3L, 5L), # Number of training epochs for (`OPG`, Refinement)
|
2021-09-07 11:00:52 +00:00
|
|
|
batch_size = 32L,
|
|
|
|
initializer = 'fromOPG',
|
|
|
|
seed = 956294L
|
|
|
|
))
|
|
|
|
|
|
|
|
## Generate reference data (gets re-sampled for each replication)
|
2021-09-10 12:58:47 +00:00
|
|
|
# Generates a list with `X`, `Y`, `B` and `name`
|
|
|
|
ds <- dataset(args$dataset, n = 1000)
|
2021-09-07 11:00:52 +00:00
|
|
|
|
|
|
|
## Build Dimension Reduction Neuronal Network model (matching the data)
|
|
|
|
nn <- nnsdr$new(
|
|
|
|
input_shapes = list(x = ncol(ds$X)),
|
|
|
|
d = ncol(ds$B),
|
|
|
|
hidden_units = args$hidden_units,
|
|
|
|
activation = args$activation,
|
|
|
|
trainable_reduction = args$trainable_reduction,
|
|
|
|
output_activation = 'sigmoid',
|
|
|
|
loss = 'binary_crossentropy',
|
|
|
|
# metrics = list('accuracy')
|
2021-09-10 12:58:47 +00:00
|
|
|
metrics = list(
|
|
|
|
'accuracy',
|
|
|
|
metric.subspace(ds$B, ds$X, ds$Y, type = "OPG", normalize = TRUE),
|
|
|
|
metric.subspace(ds$B, type = "Refinement", normalize = TRUE)
|
|
|
|
)
|
2021-09-07 11:00:52 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 12:58:47 +00:00
|
|
|
## Open simulation log file, write simulation configuration and header
|
|
|
|
log <- file(format(Sys.time(), "results/sim_binary_%Y%m%d_%H%M.csv"), "w", blocking = FALSE)
|
|
|
|
cat(paste('#', names(args), args, sep = ' ', collapse = '\n'), '\n',
|
|
|
|
'method,replication,dist.subspace,dist.grassmann,accuracy\n',
|
|
|
|
sep = '', file = log, append = TRUE)
|
|
|
|
|
|
|
|
## Set seed for sampling simulation data (NOT effecting the `NN` initialization)
|
|
|
|
set.seed(args$seed)
|
|
|
|
|
|
|
|
## Repeated simulation runs
|
|
|
|
for (rep in seq_len(args$reps)) {
|
|
|
|
## Re-sample seeded data for each simulation replication
|
|
|
|
with(dataset(ds$name), {
|
|
|
|
## Sample test dataset
|
|
|
|
ds.test <- dataset(ds$name, n = 1000)
|
|
|
|
|
|
|
|
## Starting with the reference methods `SIR`, `SAVE` and `PHD`
|
|
|
|
for (method in c("sir", "save", "phdy")) {
|
|
|
|
fit <- dr(Y ~ X, method = method)
|
|
|
|
d.sub <- dist.subspace(B, dr.basis(fit, ncol(B)), normalize = TRUE)
|
|
|
|
d.gra <- dist.grassmann(B, dr.basis(fit, ncol(B)))
|
|
|
|
accuracy <- NA
|
|
|
|
cat('"', method, '",', rep, ',', d.sub, ',', d.gra, ',', accuracy,
|
|
|
|
'\n', sep = '', file = log, append = TRUE)
|
|
|
|
}
|
|
|
|
|
|
|
|
## Fit `NNSDR` model
|
|
|
|
nn$fit(X, Y, epochs = args$epochs,
|
|
|
|
batch_size = args$batch_size, initializer = args$initializer)
|
|
|
|
# Model evaluation (with metrics)
|
|
|
|
eval <- nn$evaluate(ds.test$X, ds.test$Y)
|
|
|
|
# `OPG` estimate
|
|
|
|
d.sub <- dist.subspace(B, coef(nn, 'OPG'), normalize = TRUE)
|
|
|
|
d.gra <- dist.grassmann(B, coef(nn, 'OPG'))
|
|
|
|
accuracy <- eval[["OPG", "accuracy"]]
|
|
|
|
cat('"nn.opg",', rep, ',', d.sub, ',', d.gra, ',', accuracy,
|
|
|
|
'\n', sep = '', file = log, append = TRUE)
|
|
|
|
# Refinement estimate
|
|
|
|
d.sub <- dist.subspace(B, coef(nn), normalize = TRUE)
|
|
|
|
d.gra <- dist.grassmann(B, coef(nn))
|
|
|
|
accuracy <- eval[["Refinement", "accuracy"]]
|
|
|
|
cat('"nn.ref",', rep, ',', d.sub, ',', d.gra, ',', accuracy,
|
|
|
|
'\n', sep = '', file = log, append = TRUE)
|
|
|
|
})
|
|
|
|
|
|
|
|
## Reset model
|
2021-09-07 11:00:52 +00:00
|
|
|
nn$reset()
|
2021-09-10 12:58:47 +00:00
|
|
|
}
|
2021-09-07 11:00:52 +00:00
|
|
|
|
2021-09-10 12:58:47 +00:00
|
|
|
## Finished, close simulation log file
|
|
|
|
close(log)
|