tests/testthat/test.funKerasGeneric.R

context("FunKerasGeneric")
skip_on_cran()

test_that("check funKerasCensus: does model work as the default model conf?", {
  # library("keras")
  # library(tensorflow)
  # library("tfdatasets")
  # library("SPOT")
  # library("rsample")
  set.seed(1)
  target <- "age"
  nobs <- 1000
  batch_size <- 32
  prop <- 0.5
  cachedir <- "oml.cache"
  dfCensus <- getDataCensus(target = target, nobs=nobs, cachedir=cachedir)
  data <- getGenericTrainValTestData(dfGeneric = dfCensus,
                                     prop = prop)
  specList <- genericDataPrep(data=data, batch_size = batch_size)
  kerasConf <- getKerasConf()
  kerasConf$verbose <- 0
  x <- NULL
  set.seed(1)
  res0 <- evalKerasGeneric(x,
                          kerasConf,
                          specList)
  cfg <- getModelConf(list(model="dl"))
  # default <- c(0, 0,   5,  0.5, 1e-3, 4, 0.9,  0.999,  1, 1e-7, 5)
  default <- cfg$defaults
  # types <- c("numeric" "numeric" "integer" "numeric" "numeric" "integer"
  #              "numeric" "numeric" "integer" "numeric", "factor")
  types <- cfg$type
  x <- matrix(default, 1,)
  transformFun <- cfg$transformations
  if (length(transformFun) > 0) {  x <- transformX(xNat=x, fn=transformFun)}
  set.seed(1)
  res1 <- evalKerasGeneric(x,
                           kerasConf = kerasConf,
                           specList = specList)
  ## FIXME:
  ## res0 and res1 should be similar
  ## this is not always the case and should be fixed
  message("res0")
  print(res0)
  message("res1")
  print(res1)
  ## as a substitute for the real test, we test only for equal dimensions:
  ## expect_equal(res0,res1, tolerance = 1e-1)
  expect_equal(dim(res0), dim(res1))
}
)


test_that("check selectKerasOptimizer", {
  # library("keras")
  # library("tensorflow")
  # library("reticulate")
  # library("tfruns")
  # library("GGally")
  # library("rsample")
  # library("tfdatasets")
  # library("mlr")
  # library("plotly")
  # library("SPOT")
  # library("farff")
  # library(Metrics)
  # library("dplyr")
  # library(SPOTMisc)
  # # FIXME: remove after testing:
  # source("~/workspace/spotmisc/R/kerasOptimizer.R")
  # source("~/workspace/spotmisc/R/funKerasGeneric.R")

  k = 1
  nobs = 1e2
  task.type <- "classif"
  target <- "age"
  nobs <- nobs
  nfactors <- "high"
  nnumericals <- "high"
  cardinality <- "high"
  cachedir <- "oml.cache"
  prop <- c(2/3)
  verbosity <- 1
  score <- matrix(NA, nrow = 7, ncol=8)
  for (i in 1:7){
  x <- matrix(c(0, 0,  5,  0.5, 1e-3, 3, 0.9,  0.999,  1, 1e-7, i), ncol = 11, nrow =1)
  val <- predDlCensus(
    x = x,
    target = target,
    task.type = task.type ,
    nobs = nobs,
    nfactors = nfactors,
    nnumericals = nnumericals,
    cardinality = cardinality,
    cachedir = cachedir,
    k = k,
    prop = prop,
    verbosity = verbosity
  )
  score[i,] <- scorePredictions(val)
  }
   expect_equal(all(is.na(score)), FALSE)
})


test_that("check possibly problematic hyperparameter settings. Part 1", {
target <- "age"
batch_size <- 32
prop <- 2/3
cachedir <- "oml.cache"
dfGeneric <- getDataCensus(target = target, nobs = 1000, cachedir = cachedir)
data <- getGenericTrainValTestData(dfGeneric = dfGeneric, prop = prop)
specList <- genericDataPrep(data=data, batch_size = batch_size)

kerasConf <- getKerasConf()

## model configuration:
cfg <-  getModelConf(list(model="dl"))
transformFun <- cfg$transformations
types <- cfg$type
lower <- cfg$lower
upper <- cfg$upper
## values are already transformed:
x <- matrix( c(0.389134979, 0.9171664,   16, 0.2993133, 0.0028106961,   64, 0.9478681, 0.9977315, 1, 4.564494e-09, 5), nrow=1)

message("objectiveFunctionEvaluation(): x after transformX().")
print(x)
res <- funKerasGeneric(x, kerasConf = kerasConf, specList = specList)
expect_equal(dim(res)[1], 1)
})

Try the SPOTMisc package in your browser

Any scripts or data that you put into this service are public.

SPOTMisc documentation built on Sept. 5, 2022, 5:06 p.m.