diff --git a/R/Estimator.R b/R/Estimator.R index cb684f9..573c6a6 100644 --- a/R/Estimator.R +++ b/R/Estimator.R @@ -39,18 +39,38 @@ #' @export setEstimator <- function(learningRate='auto', weightDecay = 0.0, - batchSize = 512L, - epochs = 30L, + batchSize = 512, + epochs = 30, device='cpu', optimizer = torch$optim$AdamW, scheduler = list(fun=torch$optim$lr_scheduler$ReduceLROnPlateau, - params=list(patience=1L)), + params=list(patience=1)), criterion = torch$nn$BCEWithLogitsLoss, earlyStopping = list(useEarlyStopping=TRUE, - params = list(patience=4L)), + params = list(patience=4)), metric = "auc", seed = NULL ) { + + checkIsClass(learningRate, c("numeric", "character")) + if (inherits(learningRate, "character")) { + if (learningRate != "auto"){ + stop(paste0('Learning rate should be either a numeric or "auto", you provided: ', learningRate)) + } + } + checkIsClass(weightDecay, "numeric") + checkHigherEqual(weightDecay, 0.0) + checkIsClass(batchSize, c("numeric", "integer")) + checkHigher(batchSize, 0) + checkIsClass(epochs, c("numeric", "integer")) + checkHigher(epochs, 0) + checkIsClass(device, c("character", "function")) + checkIsClass(scheduler, "list") + checkIsClass(earlyStopping, c("list", "NULL")) + checkIsClass(metric, c("character", "list")) + checkIsClass(seed, c("numeric", "integer", "NULL")) + + if (length(learningRate)==1 && learningRate=='auto') {findLR <- TRUE} else {findLR <- FALSE} if (is.null(seed)) { seed <- as.integer(sample(1e5, 1)) diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R index 83f2f7e..cf77cb2 100644 --- a/R/HelperFunctions.R +++ b/R/HelperFunctions.R @@ -38,4 +38,43 @@ camelCaseToSnakeCase <- function(string) { camelCaseToSnakeCaseNames <- function(object) { names(object) <- camelCaseToSnakeCase(names(object)) return(object) -} \ No newline at end of file +} + +#' helper function to check class of input +#' +#' @param parameter the input parameter to check +#' @param classes which classes it should belong to (one or more) +checkIsClass<- function(parameter,classes) { + name = deparse(substitute(parameter)) + if (!inherits(x = parameter, what = classes)) { + ParallelLogger::logError(paste0(name, ' should be of class:', classes)) + stop(paste0(name, ' is wrong class')) + } + return(TRUE) +} + +#' helper function to check that input is higher than a certain value +#' +#' @param parameter the input parameter to check, can be a vector +#' @param value which value it should be higher than +checkHigher <- function(parameter,value) { + name = deparse(substitute(parameter)) + if (!is.numeric(parameter) | all(parameter<=value)) { + ParallelLogger::logError(paste0(name, ' needs to be > ',value)) + stop(paste0(name, ' needs to be > ', value)) + } + return(TRUE) +} + +#' helper function to check that input is higher or equal than a certain value +#' +#' @param parameter the input parameter to check, can be a vector +#' @param value which value it should be higher or equal than +checkHigherEqual <- function(parameter,value) { + name = deparse(substitute(parameter)) + if (!is.numeric(parameter) | all(parameter= ',value)) + stop(paste0(name, ' needs to be >= ', value)) + } + return(TRUE) +} \ No newline at end of file diff --git a/R/MLP.R b/R/MLP.R index 175cc46..6ad35e6 100644 --- a/R/MLP.R +++ b/R/MLP.R @@ -35,20 +35,39 @@ #' @param randomSampleSeed Random seed to sample hyperparameter combinations #' #' @export -setMultiLayerPerceptron <- function(numLayers = as.integer(1:8), - sizeHidden = as.integer(2^(6:9)), +setMultiLayerPerceptron <- function(numLayers = c(1:8), + sizeHidden = c(2^(6:9)), dropout = c(seq(0, 0.3, 0.05)), - sizeEmbedding = as.integer(2^(6:9)), + sizeEmbedding = c(2^(6:9)), estimatorSettings = setEstimator( learningRate = 'auto', weightDecay = c(1e-6, 1e-3), - batchSize = 1024L, - epochs = 30L, + batchSize = 1024, + epochs = 30, device="cpu"), hyperParamSearch = "random", randomSample = 100, randomSampleSeed = NULL) { + + checkIsClass(numLayers, c("integer", "numeric")) + checkHigherEqual(numLayers, 1) + checkIsClass(sizeHidden, c("integer", "numeric")) + checkHigherEqual(sizeHidden, 1) + + checkIsClass(dropout, c("numeric")) + checkHigherEqual(dropout, 0) + + checkIsClass(sizeEmbedding, c("numeric", "integer")) + checkHigherEqual(sizeEmbedding, 1) + + checkIsClass(hyperParamSearch, "character") + + checkIsClass(randomSample, c("numeric", "integer")) + checkHigherEqual(randomSample, 1) + + checkIsClass(randomSampleSeed, c("numeric", "integer", "NULL")) + paramGrid <- list( numLayers = numLayers, sizeHidden = sizeHidden, diff --git a/R/ResNet.R b/R/ResNet.R index 8620687..d20aa1d 100644 --- a/R/ResNet.R +++ b/R/ResNet.R @@ -31,15 +31,15 @@ setDefaultResNet <- function(estimatorSettings=setEstimator(learningRate='auto', weightDecay=1e-6, device='cpu', - batchSize=1024L, - epochs=50L, + batchSize=1024, + epochs=50, seed=NULL)) { - resnetSettings <- setResNet(numLayers = 6L, - sizeHidden = 512L, - hiddenFactor = 2L, + resnetSettings <- setResNet(numLayers = 6, + sizeHidden = 512, + hiddenFactor = 2, residualDropout = 0.1, hiddenDropout = 0.4, - sizeEmbedding = 256L, + sizeEmbedding = 256, estimatorSettings = estimatorSettings, hyperParamSearch = 'random', randomSample = 1) @@ -68,22 +68,44 @@ setDefaultResNet <- function(estimatorSettings=setEstimator(learningRate='auto', #' @param randomSample How many random samples from hyperparameter space to use #' @param randomSampleSeed Random seed to sample hyperparameter combinations #' @export -setResNet <- function(numLayers = as.integer(1:8), - sizeHidden = as.integer(2^(6:10)), - hiddenFactor = as.integer(1:4), +setResNet <- function(numLayers = c(1:8), + sizeHidden = c(2^(6:10)), + hiddenFactor = c(1:4), residualDropout = c(seq(0, 0.5, 0.05)), hiddenDropout = c(seq(0, 0.5, 0.05)), - sizeEmbedding = as.integer(2^(6:9)), + sizeEmbedding = c(2^(6:9)), estimatorSettings = setEstimator(learningRate='auto', weightDecay=c(1e-6, 1e-3), device='cpu', - batchSize=1024L, - epochs=30L, + batchSize=1024, + epochs=30, seed=NULL), hyperParamSearch = "random", randomSample = 100, randomSampleSeed = NULL) { + checkIsClass(numLayers, c("integer", "numeric")) + checkHigherEqual(numLayers, 1) + + checkIsClass(sizeHidden, c("integer", "numeric")) + checkHigherEqual(sizeHidden, 1) + + checkIsClass(residualDropout, "numeric") + checkHigherEqual(residualDropout, 0) + + checkIsClass(hiddenDropout, "numeric") + checkHigherEqual(hiddenDropout, 0) + + checkIsClass(sizeEmbedding, c("integer", "numeric")) + checkHigherEqual(sizeEmbedding, 1) + + checkIsClass(hyperParamSearch, "character") + + checkIsClass(randomSample, c("numeric", "integer")) + checkHigherEqual(randomSample, 1) + + checkIsClass(randomSampleSeed, c("numeric", "integer", "NULL")) + paramGrid <- list( numLayers = numLayers, sizeHidden = sizeHidden, diff --git a/R/Transformer.R b/R/Transformer.R index b520b7c..8b0d0c4 100644 --- a/R/Transformer.R +++ b/R/Transformer.R @@ -27,19 +27,19 @@ setDefaultTransformer <- function(estimatorSettings=setEstimator( learningRate = 'auto', weightDecay = 1e-4, - batchSize=512L, - epochs=10L, + batchSize=512, + epochs=10, seed=NULL, device='cpu') ) { - transformerSettings <- setTransformer(numBlocks = 3L, - dimToken = 192L, - dimOut = 1L, - numHeads = 8L, + transformerSettings <- setTransformer(numBlocks = 3, + dimToken = 192, + dimOut = 1, + numHeads = 8, attDropout = 0.2, ffnDropout = 0.1, resDropout = 0.0, - dimHidden = 256L, + dimHidden = 256, estimatorSettings=estimatorSettings, hyperParamSearch = 'random', randomSample = 1) @@ -67,16 +67,61 @@ setDefaultTransformer <- function(estimatorSettings=setEstimator( #' @param randomSampleSeed Random seed to sample hyperparameter combinations #' #' @export -setTransformer <- function(numBlocks = 3, dimToken = 96, dimOut = 1, - numHeads = 8, attDropout = 0.25, ffnDropout = 0.25, - resDropout = 0, dimHidden = 512, dimHiddenRatio = NULL, +setTransformer <- function(numBlocks = 3, + dimToken = 96, + dimOut = 1, + numHeads = 8, + attDropout = 0.25, + ffnDropout = 0.25, + resDropout = 0, + dimHidden = 512, + dimHiddenRatio = NULL, estimatorSettings=setEstimator(weightDecay = 1e-6, batchSize=1024, epochs=10, seed=NULL), hyperParamSearch = "random", - randomSample = 1, randomSampleSeed = NULL) { + randomSample = 1, + randomSampleSeed = NULL) { + + checkIsClass(numBlocks, c("integer", "numeric")) + checkHigherEqual(numBlocks, 1) + + checkIsClass(dimToken, c("integer", "numeric")) + checkHigherEqual(dimToken, 1) + + checkIsClass(dimOut, c("integer", "numeric")) + checkHigherEqual(dimOut, 1) + + checkIsClass(numHeads, c("integer", "numeric")) + checkHigherEqual(numHeads, 1) + checkIsClass(attDropout, c("numeric")) + checkHigherEqual(attDropout, 0) + + checkIsClass(ffnDropout, c("numeric")) + checkHigherEqual(ffnDropout, 0) + + checkIsClass(resDropout, c("numeric")) + checkHigherEqual(resDropout, 0) + + checkIsClass(dimHidden, c("integer", "numeric", "NULL")) + if (!is.null(dimHidden)) { + checkHigherEqual(dimHidden, 1) + } + + checkIsClass(dimHiddenRatio, c("numeric", "NULL")) + if (!is.null(dimHiddenRatio)) { + checkHigher(dimHiddenRatio, 0) + } + + checkIsClass(hyperParamSearch, "character") + + checkIsClass(randomSample, c("numeric", "integer")) + checkHigherEqual(randomSample, 1) + + checkIsClass(randomSampleSeed, c("numeric", "integer", "NULL")) + if (any(with(expand.grid(dimToken = dimToken, numHeads = numHeads), dimToken %% numHeads != 0))) { stop(paste( "dimToken needs to divisible by numHeads. dimToken =", dimToken, diff --git a/inst/python/Estimator.py b/inst/python/Estimator.py index 5997817..f8a182f 100644 --- a/inst/python/Estimator.py +++ b/inst/python/Estimator.py @@ -28,13 +28,13 @@ def __init__(self, self.model_parameters = model_parameters self.estimator_settings = estimator_settings - self.epochs = estimator_settings.get("epochs", 5) + self.epochs = int(estimator_settings.get("epochs", 5)) self.learning_rate = estimator_settings.get("learning_rate", 3e-4) self.weight_decay = estimator_settings.get("weight_decay", 1e-5) - self.batch_size = estimator_settings.get("batch_size", 1024) + self.batch_size = int(estimator_settings.get("batch_size", 1024)) self.prefix = estimator_settings.get("prefix", self.model.name) - self.previous_epochs = estimator_settings.get("previous_epochs", 0) + self.previous_epochs = int(estimator_settings.get("previous_epochs", 0)) self.model.to(device=self.device) self.optimizer = estimator_settings["optimizer"](params=self.model.parameters(), diff --git a/inst/python/MLP.py b/inst/python/MLP.py index 278e324..3b8b03b 100644 --- a/inst/python/MLP.py +++ b/inst/python/MLP.py @@ -6,17 +6,24 @@ class MLP(nn.Module): def __init__(self, - cat_features, - num_features, - size_embedding, - size_hidden, - num_layers, + cat_features: int, + num_features: int, + size_embedding: int, + size_hidden: int, + num_layers: int, activation=nn.ReLU, normalization=nn.BatchNorm1d, dropout=None, - d_out=1): + d_out: int = 1): super(MLP, self).__init__() self.name = "MLP" + cat_features = int(cat_features) + num_features = int(num_features) + size_embedding = int(size_embedding) + size_hidden = int(size_hidden) + num_layers = int(num_layers) + d_out = int(d_out) + self.embedding = nn.EmbeddingBag(cat_features + 1, size_embedding, padding_idx=0) diff --git a/inst/python/ResNet.py b/inst/python/ResNet.py index 2ec6232..f680eb2 100644 --- a/inst/python/ResNet.py +++ b/inst/python/ResNet.py @@ -7,20 +7,29 @@ class ResNet(nn.Module): def __init__(self, - cat_features, - num_features=0, - size_embedding=256, - size_hidden=256, - num_layers=2, - hidden_factor=1, + cat_features: int, + num_features: int = 0, + size_embedding: int = 256, + size_hidden: int = 256, + num_layers: int = 2, + hidden_factor: int = 1, activation=nn.ReLU, normalization=nn.BatchNorm1d, hidden_dropout=0, residual_dropout=0, - dim_out=1, + dim_out: int = 1, concat_num=True): super(ResNet, self).__init__() self.name = 'ResNet' + cat_features = int(cat_features) + num_features = int(num_features) + size_embedding = int(size_embedding) + size_hidden = int(size_hidden) + num_layers = int(num_layers) + hidden_factor = int(hidden_factor) + dim_out = int(dim_out) + + self.embedding = nn.EmbeddingBag( num_embeddings=cat_features + 1, embedding_dim=size_embedding, diff --git a/inst/python/Transformer.py b/inst/python/Transformer.py index 395026a..5944e1b 100644 --- a/inst/python/Transformer.py +++ b/inst/python/Transformer.py @@ -20,16 +20,16 @@ def forward(self, x): class Transformer(nn.Module): def __init__(self, - cat_features, - num_features, - num_blocks, - dim_token, - num_heads, + cat_features: int, + num_features: int, + num_blocks: int, + dim_token: int, + num_heads: int, att_dropout, ffn_dropout, res_dropout, - dim_hidden, - dim_out=1, + dim_hidden: int, + dim_out: int = 1, head_activation=nn.ReLU, activation=ReGLU, ffn_norm=nn.LayerNorm, @@ -37,6 +37,14 @@ def __init__(self, att_norm=nn.LayerNorm): super(Transformer, self).__init__() self.name = "Transformer" + cat_features = int(cat_features) + num_features = int(num_features) + num_blocks = int(num_blocks) + dim_token = int(dim_token) + num_heads = int(num_heads) + dim_hidden = int(dim_hidden) + dim_out = int(dim_out) + self.categorical_embedding = nn.Embedding(cat_features + 1, dim_token, padding_idx=0) if num_features != 0 and num_features is not None: diff --git a/man/checkHigher.Rd b/man/checkHigher.Rd new file mode 100644 index 0000000..bab0d76 --- /dev/null +++ b/man/checkHigher.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/HelperFunctions.R +\name{checkHigher} +\alias{checkHigher} +\title{helper function to check that input is higher than a certain value} +\usage{ +checkHigher(parameter, value) +} +\arguments{ +\item{parameter}{the input parameter to check, can be a vector} + +\item{value}{which value it should be higher than} +} +\description{ +helper function to check that input is higher than a certain value +} diff --git a/man/checkHigherEqual.Rd b/man/checkHigherEqual.Rd new file mode 100644 index 0000000..60230e6 --- /dev/null +++ b/man/checkHigherEqual.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/HelperFunctions.R +\name{checkHigherEqual} +\alias{checkHigherEqual} +\title{helper function to check that input is higher or equal than a certain value} +\usage{ +checkHigherEqual(parameter, value) +} +\arguments{ +\item{parameter}{the input parameter to check, can be a vector} + +\item{value}{which value it should be higher or equal than} +} +\description{ +helper function to check that input is higher or equal than a certain value +} diff --git a/man/checkIsClass.Rd b/man/checkIsClass.Rd new file mode 100644 index 0000000..5af3ad8 --- /dev/null +++ b/man/checkIsClass.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/HelperFunctions.R +\name{checkIsClass} +\alias{checkIsClass} +\title{helper function to check class of input} +\usage{ +checkIsClass(parameter, classes) +} +\arguments{ +\item{parameter}{the input parameter to check} + +\item{classes}{which classes it should belong to (one or more)} +} +\description{ +helper function to check class of input +} diff --git a/man/setDefaultResNet.Rd b/man/setDefaultResNet.Rd index 2ff34c1..c26f354 100644 --- a/man/setDefaultResNet.Rd +++ b/man/setDefaultResNet.Rd @@ -6,7 +6,7 @@ \usage{ setDefaultResNet( estimatorSettings = setEstimator(learningRate = "auto", weightDecay = 1e-06, device = - "cpu", batchSize = 1024L, epochs = 50L, seed = NULL) + "cpu", batchSize = 1024, epochs = 50, seed = NULL) ) } \arguments{ diff --git a/man/setDefaultTransformer.Rd b/man/setDefaultTransformer.Rd index 049b121..e0a3a32 100644 --- a/man/setDefaultTransformer.Rd +++ b/man/setDefaultTransformer.Rd @@ -6,7 +6,7 @@ \usage{ setDefaultTransformer( estimatorSettings = setEstimator(learningRate = "auto", weightDecay = 1e-04, batchSize - = 512L, epochs = 10L, seed = NULL, device = "cpu") + = 512, epochs = 10, seed = NULL, device = "cpu") ) } \arguments{ diff --git a/man/setEstimator.Rd b/man/setEstimator.Rd index 18ed8fa..fcb33ab 100644 --- a/man/setEstimator.Rd +++ b/man/setEstimator.Rd @@ -7,14 +7,14 @@ setEstimator( learningRate = "auto", weightDecay = 0, - batchSize = 512L, - epochs = 30L, + batchSize = 512, + epochs = 30, device = "cpu", optimizer = torch$optim$AdamW, scheduler = list(fun = torch$optim$lr_scheduler$ReduceLROnPlateau, params = - list(patience = 1L)), + list(patience = 1)), criterion = torch$nn$BCEWithLogitsLoss, - earlyStopping = list(useEarlyStopping = TRUE, params = list(patience = 4L)), + earlyStopping = list(useEarlyStopping = TRUE, params = list(patience = 4)), metric = "auc", seed = NULL ) diff --git a/man/setMultiLayerPerceptron.Rd b/man/setMultiLayerPerceptron.Rd index 01418fe..f3676cd 100644 --- a/man/setMultiLayerPerceptron.Rd +++ b/man/setMultiLayerPerceptron.Rd @@ -5,12 +5,12 @@ \title{setMultiLayerPerceptron} \usage{ setMultiLayerPerceptron( - numLayers = as.integer(1:8), - sizeHidden = as.integer(2^(6:9)), + numLayers = c(1:8), + sizeHidden = c(2^(6:9)), dropout = c(seq(0, 0.3, 0.05)), - sizeEmbedding = as.integer(2^(6:9)), + sizeEmbedding = c(2^(6:9)), estimatorSettings = setEstimator(learningRate = "auto", weightDecay = c(1e-06, 0.001), - batchSize = 1024L, epochs = 30L, device = "cpu"), + batchSize = 1024, epochs = 30, device = "cpu"), hyperParamSearch = "random", randomSample = 100, randomSampleSeed = NULL diff --git a/man/setResNet.Rd b/man/setResNet.Rd index 9dcffa7..fbe3d77 100644 --- a/man/setResNet.Rd +++ b/man/setResNet.Rd @@ -5,14 +5,14 @@ \title{setResNet} \usage{ setResNet( - numLayers = as.integer(1:8), - sizeHidden = as.integer(2^(6:10)), - hiddenFactor = as.integer(1:4), + numLayers = c(1:8), + sizeHidden = c(2^(6:10)), + hiddenFactor = c(1:4), residualDropout = c(seq(0, 0.5, 0.05)), hiddenDropout = c(seq(0, 0.5, 0.05)), - sizeEmbedding = as.integer(2^(6:9)), + sizeEmbedding = c(2^(6:9)), estimatorSettings = setEstimator(learningRate = "auto", weightDecay = c(1e-06, 0.001), - device = "cpu", batchSize = 1024L, epochs = 30L, seed = NULL), + device = "cpu", batchSize = 1024, epochs = 30, seed = NULL), hyperParamSearch = "random", randomSample = 100, randomSampleSeed = NULL diff --git a/tests/testthat/test-Estimator.R b/tests/testthat/test-Estimator.R index a8711e8..3177550 100644 --- a/tests/testthat/test-Estimator.R +++ b/tests/testthat/test-Estimator.R @@ -4,16 +4,16 @@ numFeatures <- small_dataset$dataset$get_numerical_features()$shape[[1]] modelParameters <- list( cat_features = catFeatures, num_features = numFeatures, - size_embedding = 16L, - size_hidden = 16L, - num_layers = 2L, - hidden_factor = 2L + size_embedding = 16, + size_hidden = 16, + num_layers = 2, + hidden_factor = 2 ) estimatorSettings <- setEstimator(learningRate = 3e-4, weightDecay = 0.0, - batchSize = 128L, - epochs = 5L, + batchSize = 128, + epochs = 5, device = 'cpu', seed=42, optimizer=torch$optim$AdamW, @@ -93,8 +93,8 @@ test_that("estimator fitting works", { estimatorSettings <- setEstimator(learningRate = 3e-4, weightDecay = 0.0, - batchSize = 128L, - epochs = 5L, + batchSize = 128, + epochs = 5, device = 'cpu', metric= "loss") @@ -134,11 +134,11 @@ test_that("early stopping works", { }) modelSettings <- setResNet( - numLayers = 1L, sizeHidden = 16L, hiddenFactor = 1L, + numLayers = 1, sizeHidden = 16, hiddenFactor = 1, residualDropout = 0, hiddenDropout = 0, - sizeEmbedding = 16L, hyperParamSearch = "random", - randomSample = 1L, - setEstimator(epochs=1L, + sizeEmbedding = 16, hyperParamSearch = "random", + randomSample = 1, + setEstimator(epochs=1, learningRate = 3e-4) ) @@ -203,8 +203,8 @@ test_that("Estimator without earlyStopping works", { # estimator without earlyStopping estimatorSettings <- setEstimator(learningRate = 3e-4, weightDecay = 0.0, - batchSize = 128L, - epochs = 1L, + batchSize = 128, + epochs = 1, device = 'cpu', earlyStopping = NULL) @@ -223,12 +223,12 @@ test_that("Estimator without earlyStopping works", { test_that("Early stopper can use loss and stops early", { estimatorSettings <- setEstimator(learningRate = 3e-2, weightDecay = 0.0, - batchSize = 128L, - epochs = 10L, + batchSize = 128, + epochs = 10, device = 'cpu', earlyStopping =list(useEarlyStopping=TRUE, params = list(mode=c('min'), - patience=1L)), + patience=1)), metric = 'loss', seed=42) @@ -254,9 +254,9 @@ test_that('Custom metric in estimator works', { estimatorSettings <- setEstimator(learningRate = 3e-4, weightDecay = 0.0, - batchSize = 128L, + batchSize = 128, device = "cpu", - epochs = 1L, + epochs = 1, metric=list(fun=metric_fun, name="auprc", mode="max")) @@ -277,15 +277,15 @@ test_that('Custom metric in estimator works', { test_that("setEstimator with paramsToTune is correctly added to hyperparameters", { estimatorSettings <- setEstimator(learningRate=c(3e-4,1e-3), - batchSize=128L, - epochs=1L, + batchSize=128, + epochs=1, device="cpu", metric=c("auc", "auprc"), earlyStopping = list(useEarlyStopping=TRUE, - params=list(patience=c(4L,10L)))) - modelSettings <- setResNet(numLayers = 1L, sizeHidden = 64L, - hiddenFactor = 1L, residualDropout = 0.2, - hiddenDropout = 0.2,sizeEmbedding = 128L, + params=list(patience=c(4,10)))) + modelSettings <- setResNet(numLayers = 1, sizeHidden = 64, + hiddenFactor = 1, residualDropout = 0.2, + hiddenDropout = 0.2,sizeEmbedding = 128, estimatorSettings = estimatorSettings, hyperParamSearch = "grid") @@ -316,7 +316,7 @@ test_that("device as a function argument works", { learningRate = 3e-4) model <- setDefaultResNet(estimatorSettings = estimatorSettings) - model$param[[1]]$catFeatures <- 10L + model$param[[1]]$catFeatures <- 10 estimator <- createEstimator(modelType = modelType, modelParameters = model$param[[1]], @@ -330,7 +330,7 @@ test_that("device as a function argument works", { learningRate = 3e-4) model <- setDefaultResNet(estimatorSettings = estimatorSettings) - model$param[[1]]$catFeatures <- 10L + model$param[[1]]$catFeatures <- 10 estimator <- createEstimator(modelType = modelType, modelParameters = model$param[[1]], diff --git a/tests/testthat/test-MLP.R b/tests/testthat/test-MLP.R index cb6f2c9..5cbd7a5 100644 --- a/tests/testthat/test-MLP.R +++ b/tests/testthat/test-MLP.R @@ -1,15 +1,15 @@ modelSettings <- setMultiLayerPerceptron( - numLayers = 2L, - sizeHidden = 32L, + numLayers = 2, + sizeHidden = 32, dropout = c(0.1), - sizeEmbedding = 32L, + sizeEmbedding = 32, estimatorSettings = setEstimator( learningRate=c(3e-4), weightDecay = c(1e-6), seed=42, - batchSize=128L, - epochs=1L + batchSize=128, + epochs=1 ), hyperParamSearch = "random", randomSample = 1 @@ -84,11 +84,11 @@ test_that("MLP with runPlp working checks", { test_that("MLP nn-module works ", { MLP <- reticulate::import_from_path("MLP", path=path)$MLP model <- MLP( - cat_features = 5L, - num_features = 1L, - size_embedding = 5L, - size_hidden = 16L, - num_layers = 1L, + cat_features = 5, + num_features = 1, + size_embedding = 5, + size_hidden = 16, + num_layers = 1, activation = torch$nn$ReLU, normalization = torch$nn$BatchNorm1d, dropout = 0.3 @@ -131,16 +131,16 @@ test_that("Errors are produced by settings function", { randomSample <- 2 expect_error(setMultiLayerPerceptron( - numLayers = 1L, - sizeHidden = 128L, + numLayers = 1, + sizeHidden = 128, dropout = 0.0, - sizeEmbedding = 128L, + sizeEmbedding = 128, hyperParamSearch = 'random', estimatorSettings = setEstimator( learningRate = 'auto', weightDecay = c(1e-3), - batchSize = 1024L, - epochs = 30L, + batchSize = 1024, + epochs = 30, device="cpu"))) }) diff --git a/tests/testthat/test-ResNet.R b/tests/testthat/test-ResNet.R index 81b1ac7..29dfb8e 100644 --- a/tests/testthat/test-ResNet.R +++ b/tests/testthat/test-ResNet.R @@ -1,16 +1,16 @@ resSet <- setResNet( - numLayers = 2L, - sizeHidden = 32L, - hiddenFactor = 2L, + numLayers = 2, + sizeHidden = 32, + hiddenFactor = 2, residualDropout = 0.1, hiddenDropout = 0.1, - sizeEmbedding = 32L, + sizeEmbedding = 32, estimatorSettings = setEstimator(learningRate="auto", weightDecay = c(1e-6), seed=42, - batchSize = 128L, - epochs=1L), + batchSize = 128, + epochs=1), hyperParamSearch = "random", randomSample = 1, ) @@ -22,17 +22,17 @@ test_that("setResNet works", { testthat::expect_true(length(resSet$param) > 0) - expect_error(setResNet(numLayers = 2L, - sizeHidden = 32L, - hiddenFactor = 2L, + expect_error(setResNet(numLayers = 2, + sizeHidden = 32, + hiddenFactor = 2, residualDropout = 0.1, hiddenDropout = 0.1, - sizeEmbedding = 32L, + sizeEmbedding = 32, estimatorSettings = setEstimator(learningRate=c(3e-4), weightDecay = c(1e-6), seed=42, - batchSize = 128L, - epochs=1L), + batchSize = 128, + epochs=1), hyperParamSearch = "random", randomSample = 2)) }) @@ -91,12 +91,12 @@ test_that("ResNet with runPlp working checks", { test_that("ResNet nn-module works ", { ResNet <- reticulate::import_from_path("ResNet", path=path)$ResNet model <- ResNet( - cat_features = 5L, - num_features = 1L, - size_embedding = 5L, - size_hidden = 16L, - num_layers = 1L, - hidden_factor = 2L, + cat_features = 5, + num_features = 1, + size_embedding = 5, + size_hidden = 16, + num_layers = 1, + hidden_factor = 2, activation = torch$nn$ReLU, normalization = torch$nn$BatchNorm1d, hidden_dropout = 0.3, @@ -120,12 +120,12 @@ test_that("ResNet nn-module works ", { input$num <- NULL model <- ResNet( - cat_features = 5L, - num_features = 0L, - size_embedding = 5L, - size_hidden = 16L, - num_layers = 1L, - hidden_factor = 2L, + cat_features = 5, + num_features = 0, + size_embedding = 5, + size_hidden = 16, + num_layers = 1, + hidden_factor = 2, activation = torch$nn$ReLU, normalization = torch$nn$BatchNorm1d, hidden_dropout = 0.3, @@ -140,12 +140,12 @@ test_that("Default Resnet works", { defaultResNet <- setDefaultResNet() params <- defaultResNet$param[[1]] - expect_equal(params$numLayers, 6L) - expect_equal(params$sizeHidden, 512L) - expect_equal(params$hiddenFactor, 2L) + expect_equal(params$numLayers, 6) + expect_equal(params$sizeHidden, 512) + expect_equal(params$hiddenFactor, 2) expect_equal(params$residualDropout, 0.1) expect_equal(params$hiddenDropout, 0.4) - expect_equal(params$sizeEmbedding, 256L) + expect_equal(params$sizeEmbedding, 256) }) @@ -153,12 +153,12 @@ test_that("Errors are produced by settings function", { randomSample <- 2 expect_error(setResNet( - numLayers = 1L, - sizeHidden = 128L, + numLayers = 1, + sizeHidden = 128, hiddenFactor = 1, residualDropout = 0.0, hiddenDropout = 0.0, - sizeEmbedding = 128L, + sizeEmbedding = 128, estimatorSettings = setEstimator(weightDecay = 1e-6, learningRate = 0.01, seed = 42), diff --git a/tests/testthat/test-TrainingCache.R b/tests/testthat/test-TrainingCache.R index e4ba6b2..8b5d546 100644 --- a/tests/testthat/test-TrainingCache.R +++ b/tests/testthat/test-TrainingCache.R @@ -1,14 +1,14 @@ -resNetSettings <- setResNet(numLayers = c(1L, 2L, 4L), - sizeHidden = 64L, - hiddenFactor = 1L, +resNetSettings <- setResNet(numLayers = c(1, 2, 4), + sizeHidden = 64, + hiddenFactor = 1, residualDropout = 0.5, hiddenDropout = 0.5, - sizeEmbedding = 64L, + sizeEmbedding = 64, estimatorSettings = setEstimator(learningRate=3e-4, weightDecay=1e-3, device='cpu', - batchSize=64L, - epochs=1L, + batchSize=64, + epochs=1, seed=NULL), hyperParamSearch = "random", randomSample = 3, diff --git a/tests/testthat/test-Transformer.R b/tests/testthat/test-Transformer.R index 7e233e0..6148b9b 100644 --- a/tests/testthat/test-Transformer.R +++ b/tests/testthat/test-Transformer.R @@ -1,15 +1,15 @@ settings <- setTransformer( - numBlocks = 1L, - dimToken = 8L, - dimOut = 1L, - numHeads = 2L, + numBlocks = 1, + dimToken = 8, + dimOut = 1, + numHeads = 2, attDropout = 0.0, ffnDropout = 0.2, resDropout = 0.0, - dimHidden = 32L, + dimHidden = 32, estimatorSettings = setEstimator(learningRate = 3e-4, - batchSize=64L, - epochs=1L), + batchSize=64, + epochs=1), randomSample = 1 ) @@ -18,16 +18,16 @@ test_that("Transformer settings work", { testthat::expect_equal(settings$fitFunction, "fitEstimator") testthat::expect_true(length(settings$param) > 0) testthat::expect_error(setTransformer( - numBlocks = 1L, dimToken = 50L, - numHeads = 7L + numBlocks = 1, dimToken = 50, + numHeads = 7 )) testthat::expect_error(setTransformer( - numBlocks = 1L, dimToken = c(2L, 4L), - numHeads = c(2L, 4L) + numBlocks = 1, dimToken = c(2, 4), + numHeads = c(2, 4) )) testthat::expect_error(setTransformer( - numBlocks = 1L, dimToken = c(4L, 6L), - numHeads = c(2L, 4L) + numBlocks = 1, dimToken = c(4, 6), + numHeads = c(2, 4) )) }) @@ -46,15 +46,15 @@ test_that("fitEstimator with Transformer works", { test_that("transformer nn-module works", { Transformer <- reticulate::import_from_path("Transformer", path=path)$Transformer model <- Transformer( - cat_features = 5L, - num_features = 1L, - num_blocks = 2L, - dim_token = 16L, - num_heads = 2L, + cat_features = 5, + num_features = 1, + num_blocks = 2, + dim_token = 16, + num_heads = 2, att_dropout = 0, ffn_dropout = 0, res_dropout = 0, - dim_hidden = 32L + dim_hidden = 32 ) pars <- sum(reticulate::iterate(model$parameters(), function(x) x$numel())) @@ -74,15 +74,15 @@ test_that("transformer nn-module works", { input$num <- NULL model <- Transformer( - cat_features = 5L, - num_features = 0L, - num_blocks = 2L, - dim_token = 16L, - num_heads = 2L, + cat_features = 5, + num_features = 0, + num_blocks = 2, + dim_token = 16, + num_heads = 2, att_dropout = 0, ffn_dropout = 0, res_dropout = 0, - dim_hidden = 32L + dim_hidden = 32 ) output <- model(input) expect_equal(output$shape[0], 10L) @@ -92,9 +92,9 @@ test_that("Default Transformer works", { defaultTransformer <- setDefaultTransformer() params <- defaultTransformer$param[[1]] - expect_equal(params$numBlocks, 3L) - expect_equal(params$dimToken, 192L) - expect_equal(params$numHeads, 8L) + expect_equal(params$numBlocks, 3) + expect_equal(params$dimToken, 192) + expect_equal(params$numHeads, 8) expect_equal(params$resDropout, 0.0) expect_equal(params$attDropout, 0.2) @@ -111,8 +111,8 @@ test_that("Errors are produced by settings function", { test_that("dimHidden ratio works as expected", { randomSample <- 4 - dimToken <- c(64L, 128L, 256L, 512L) - dimHiddenRatio <- 2L + dimToken <- c(64, 128, 256, 512) + dimHiddenRatio <- 2 modelSettings <- setTransformer(dimToken = dimToken, dimHiddenRatio = dimHiddenRatio, dimHidden = NULL,