DICEr Function for Training and Evaluating a Deep Learning Model
Description
This function orchestrates the training and evaluation of a deep learning model, specifically for autoencoder-based representation learning followed by clustering and classification. It handles data loading, model training, testing, and saving the best model based on performance criteria.
Usage
DICEr(args)
DICEr(args)
Arguments
args |
A list of arguments that configure the model training and evaluation process. The list should include:
-
seed: An integer value for setting the random seed for reproducibility.
-
input_path: A character string specifying the path to the input data directory.
-
filename_train: A character string specifying the filename of the training dataset.
-
filename_test: A character string specifying the filename of the test dataset.
-
n_input_fea: An integer specifying the number of input features.
-
n_hidden_fea: An integer specifying the number of hidden features in the model.
-
lstm_layer: An integer specifying the number of LSTM layers in the model.
-
lstm_dropout: A numeric value specifying the dropout rate for the LSTM layers.
-
K_clusters: An integer specifying the number of clusters for the k-means clustering.
-
n_dummy_demov_fea: An integer specifying the number of dummy demographic features.
-
cuda: A logical value indicating whether to use CUDA (GPU acceleration) for model training.
-
lr: A numeric value specifying the learning rate for the optimizer.
-
init_AE_epoch: An integer specifying the number of epochs for training the autoencoder.
-
iter: An integer specifying the number of iterations for the main optimization process.
-
epoch_in_iter: An integer specifying the number of epochs in each iteration of the main optimization process.
-
lambda_AE: A numeric value specifying the weight of the autoencoder loss in the overall loss function.
-
lambda_classifier: A numeric value specifying the weight of the classification loss in the overall loss function.
-
lambda_outcome: A numeric value specifying the weight of the outcome prediction loss in the overall loss function.
-
lambda_p_value: A numeric value specifying the weight of the p-value loss in the overall loss function.
|
Details
The DICEr function executes the following steps:
-
Sets the random seed for reproducibility.
-
Loads and preprocesses the training and test datasets.
-
Initializes the model, optimizer, and loss functions.
-
Trains an autoencoder for representation learning, saving the model and plotting loss curves.
-
Performs k-means clustering on the learned representations and reassigns cluster labels based on outcome ratios.
-
Iteratively trains the model for clustering, classification, and outcome prediction, optimizing the combined loss function.
-
Saves the best model based on the outcome prediction likelihood and checks for p-value significance.
-
Outputs and saves relevant training metrics, including loss curves and model checkpoints.
Value
Called for its side effects: trains the model and saves results to
disk. Returns NULL invisibly.
Examples
## Prepare data in DICErClust format (length-3 RDS list)
data_dir <- tempdir()
n <- 100L; p <- 4L; q <- 2L
set.seed(1)
saveRDS(list(matrix(runif(n * p), n, p), # data_x
matrix(as.double(rbinom(n * q, 1, 0.5)), n, q), # data_v (float)
rbinom(n, 1, 0.3)), # data_y
file.path(data_dir, "train.rds"))
saveRDS(list(matrix(runif(30L * p), 30L, p),
matrix(as.double(rbinom(30L * q, 1, 0.5)), 30L, q),
rbinom(30L, 1, 0.3)),
file.path(data_dir, "test.rds"))
args <- list(
seed = 1L, input_path = paste0(data_dir, "/"),
filename_train = "train.rds", filename_test = "test.rds",
n_input_fea = p, n_hidden_fea = 2L,
lstm_layer = 1L, lstm_dropout = 0.0, K_clusters = 2L,
n_dummy_demov_fea = q, cuda = FALSE, lr = 1e-4,
init_AE_epoch = 2L, iter = 5L, epoch_in_iter = 1L,
lambda_AE = 1.0, lambda_classifier = 1.0,
lambda_outcome = 1.0, lambda_p_value = 1.0
)
old_wd <- setwd(tempdir())
DICEr(args)
setwd(old_wd)
data_dir <- tempdir()
n <- 100L; p <- 4L; q <- 2L
set.seed(1)
saveRDS(list(matrix(runif(n * p), n, p),
matrix(as.double(rbinom(n * q, 1, 0.5)), n, q),
rbinom(n, 1, 0.3)),
file.path(data_dir, "train.rds"))
saveRDS(list(matrix(runif(30L * p), 30L, p),
matrix(as.double(rbinom(30L * q, 1, 0.5)), 30L, q),
rbinom(30L, 1, 0.3)),
file.path(data_dir, "test.rds"))
args <- list(
seed = 1L, input_path = paste0(data_dir, "/"),
filename_train = "train.rds", filename_test = "test.rds",
n_input_fea = p, n_hidden_fea = 2L,
lstm_layer = 1L, lstm_dropout = 0.0, K_clusters = 2L,
n_dummy_demov_fea = q, cuda = FALSE, lr = 1e-4,
init_AE_epoch = 2L, iter = 5L, epoch_in_iter = 1L,
lambda_AE = 1.0, lambda_classifier = 1.0,
lambda_outcome = 1.0, lambda_p_value = 1.0
)
old_wd <- setwd(tempdir())
DICEr(args)
setwd(old_wd)