From 5356946305d01b3563ea42232e382d064b1a2358 Mon Sep 17 00:00:00 2001 From: Egill Fridgeirsson Date: Sun, 9 Oct 2022 18:52:13 +0200 Subject: [PATCH] add a defaultResNet with tests --- NAMESPACE | 1 + R/ResNet.R | 40 +++++++++++++++++++++++++++++++++++- man/setDefaultResNet.Rd | 24 ++++++++++++++++++++++ man/setResNet.Rd | 2 +- tests/testthat/test-ResNet.R | 13 ++++++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 man/setDefaultResNet.Rd diff --git a/NAMESPACE b/NAMESPACE index a102062..dd5fbf9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(Estimator) export(fitEstimator) export(gridCvDeep) export(predictDeepEstimator) +export(setDefaultResNet) export(setMultiLayerPerceptron) export(setResNet) export(setTransformer) diff --git a/R/ResNet.R b/R/ResNet.R index ac9200f..d0364a4 100644 --- a/R/ResNet.R +++ b/R/ResNet.R @@ -16,6 +16,44 @@ # See the License for the specific language governing permissions and # limitations under the License. +#' setDefaultResNet +#' +#' @description +#' Creates settings for a default ResNet model +#' +#' @details +#' Model architecture from by https://arxiv.org/abs/2106.11959 . +#' Hyperparameters chosen by a experience on a few prediction problems. +#' +#' @param device Which device to run analysis on, either 'cpu' or 'cuda', default: 'cpu' +#' @param batchSize Size of batch, default: 1024 +#' @param epochs Number of epochs to run, default: 10 +#' @param seed Random seed to use + +#' @export +setDefaultResNet <- function(device='cpu', + batchSize=1024, + epochs=10, + seed=NULL) { + + resnetSettings <- setResNet(numLayers = 6, + sizeHidden = 512, + hiddenFactor = 2, + residualDropout = 0.1, + hiddenDropout = 0.4, + sizeEmbedding = 256, + weightDecay = 1e-6, + learningRate = 0.01, + hyperParamSearch = 'random', + randomSample = 1, + device = device, + batchSize = batchSize, + seed = seed) + attr(resnetSettings, 'settings')$name <- 'defaultResnet' + return(resnetSettings) +} + + #' setResNet #' #' @description @@ -42,7 +80,7 @@ #' #' @export setResNet <- function(numLayers = c(1:8), - sizeHidden = c(2^(6:9)), + 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)), diff --git a/man/setDefaultResNet.Rd b/man/setDefaultResNet.Rd new file mode 100644 index 0000000..28c29df --- /dev/null +++ b/man/setDefaultResNet.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ResNet.R +\name{setDefaultResNet} +\alias{setDefaultResNet} +\title{setDefaultResNet} +\usage{ +setDefaultResNet(device = "cpu", batchSize = 1024, epochs = 10, seed = NULL) +} +\arguments{ +\item{device}{Which device to run analysis on, either 'cpu' or 'cuda', default: 'cpu'} + +\item{batchSize}{Size of batch, default: 1024} + +\item{epochs}{Number of epochs to run, default: 10} + +\item{seed}{Random seed to use} +} +\description{ +Creates settings for a default ResNet model +} +\details{ +Model architecture from by https://arxiv.org/abs/2106.11959 . +Hyperparameters chosen by a experience on a few prediction problems. +} diff --git a/man/setResNet.Rd b/man/setResNet.Rd index d0ced0d..5f66d45 100644 --- a/man/setResNet.Rd +++ b/man/setResNet.Rd @@ -6,7 +6,7 @@ \usage{ setResNet( numLayers = c(1:8), - sizeHidden = c(2^(6:9)), + 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)), diff --git a/tests/testthat/test-ResNet.R b/tests/testthat/test-ResNet.R index 6fb0f91..626d39d 100644 --- a/tests/testthat/test-ResNet.R +++ b/tests/testthat/test-ResNet.R @@ -111,3 +111,16 @@ test_that("ResNet nn-module works ", { # model works without numeric variables expect_equal(output$shape, 10) }) + +test_that("Default Resnet works", { + defaultResNet <- setDefaultResNet() + params <- defaultResNet$param[[1]] + + 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, 256) + +})