Skip to content

Commit

Permalink
Modified all tests to not depend on data generated in "auxiliar" vign…
Browse files Browse the repository at this point in the history
…ettes.

This allowed to delete said vignettes the 4 data examples stored that were not being used. To remplace this data in tests, helper-testing_data.R has been with 2 functions to generate data for tests.
  • Loading branch information
moralapablo committed Nov 3, 2023
1 parent 9c15f91 commit 9407f50
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 758 deletions.
13 changes: 0 additions & 13 deletions R/data.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/nn2poly_algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ nn2poly_algorithm <- function(weights_list,
all_partitions = NULL
) {

if (!check_weights_dimensions(object)) {
if (!check_weights_dimensions(weights_list)) {
stop("The list of weights has incorrect dimensions.
Please, check the right dimmensions in the documentation.",
call. = FALSE
Expand Down
Binary file removed data/nn2poly_example0.rda
Binary file not shown.
Binary file removed data/nn2poly_example1.rda
Binary file not shown.
Binary file removed data/nn2poly_example2.rda
Binary file not shown.
Binary file removed data/nn2poly_example3.rda
Binary file not shown.
32 changes: 0 additions & 32 deletions man/datasets.Rd

This file was deleted.

4 changes: 2 additions & 2 deletions tests/testthat/helper-luz.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ luz_test_data <- function(example) {
data_val <- torch::dataset_subset(data_full, val_indices)

list(
train = torch::dataloader(data_train, batch_size = 32, shuffle = TRUE),
valid = torch::dataloader(data_val, batch_size = 32)
train = torch::dataloader(data_train, batch_size = 5, shuffle = TRUE),
valid = torch::dataloader(data_val, batch_size = 5)
)
}

Expand Down
36 changes: 36 additions & 0 deletions tests/testthat/helper-testing_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

# Testing data 1 ----
# Created to test with all weights equal to 1, not training any NN.
# With these values we have some tests with manually computed coefficients.
testing_helper_1 <- function(){
testing_data_1 <- vector(mode="list", length= 0)
testing_data_1$weights_list <- vector(mode="list", length= 3)
testing_data_1$weights_list[[1]] <- matrix(1,3,2)
testing_data_1$weights_list[[2]] <- matrix(1,3,2)
testing_data_1$weights_list[[3]] <- matrix(1,3,1)
testing_data_1$af_string_list <- list("softplus", "softplus", "linear")
testing_data_1$taylor_orders <- c(2, 2, 1)
return(testing_data_1)
}


# Testing data 2 ----
# In this case we will generate some polynomial data with linear output
# This data will be used in testing keras/tensorflow and luz/torch
# Note that the data is random so the NN will not learn properly but
# we can still test if the coefficients are as expected by nn2poly.

testing_helper_2 <- function(){
set.seed(42)
n_sample <- 100
p <- 2
testing_data_2 <- vector(mode="list", length= 0)
testing_data_2$train_x <- matrix(rnorm(n_sample*p),ncol = p)
testing_data_2$train_y <- matrix(rnorm(n_sample),ncol = 1)
testing_data_2$taylor_orders <- c(2, 2, 1)
return(testing_data_2)
}




15 changes: 9 additions & 6 deletions tests/testthat/test-add_constraints.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ test_that("The function works over an already trained network and the connstrain
skip_if_not_installed("tensorflow")
skip_on_cran()

testing_data <- testing_helper_2()
testing_data

nn <- keras_test_model()

# compile the model
Expand All @@ -60,10 +63,10 @@ test_that("The function works over an already trained network and the connstrain

# train the model
keras::fit(nn,
nn2poly_example0$train_x,
nn2poly_example0$train_y,
x = testing_data$train_x,
y = testing_data$train_y,
verbose = 0,
epochs = 5,
epochs = 3,
validation_split = 0.2
)

Expand All @@ -79,10 +82,10 @@ test_that("The function works over an already trained network and the connstrain

# train the model
keras::fit(constrained_nn,
nn2poly_example0$train_x,
nn2poly_example0$train_y,
testing_data$train_x,
testing_data$train_y,
verbose = 0,
epochs = 5,
epochs = 3,
validation_split = 0.2
)

Expand Down
8 changes: 5 additions & 3 deletions tests/testthat/test-luz_constraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ test_that("The constranied training works using the l1 and l2 constraints", {
skip_if_not_installed("torch")
skip_on_cran()

data <- luz_test_data(nn2poly_example0)
testing_data <- testing_helper_2()

data <- luz_test_data(testing_data)

# Do the constrained training with the l1 constraint
fitted <- luz_test_model() %>%
Expand All @@ -12,7 +14,7 @@ test_that("The constranied training works using the l1 and l2 constraints", {
optimizer = torch::optim_adam,
) %>%
add_constraints("l1_norm") %>%
fit(data$train, epochs = 3, valid_data = data$valid)
fit(data$train, epochs = 3, valid_data = data$valid, verbose = FALSE)

wb <- torch::torch_tensor(
rbind(
Expand All @@ -36,7 +38,7 @@ test_that("The constranied training works using the l1 and l2 constraints", {
optimizer = torch::optim_adam,
) %>%
add_constraints("l2_norm") %>%
fit(data$train, epochs = 3, valid_data = data$valid)
fit(data$train, epochs = 3, valid_data = data$valid, verbose = FALSE)

wb <- torch::torch_tensor(
rbind(
Expand Down
31 changes: 15 additions & 16 deletions tests/testthat/test-nn2poly.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
test_that("nn2poly with list input against precomputed example with single
output, check also that nn2poly class is given to the output", {
nn2poly_example <- nn2poly_example0
testing_data <- testing_helper_1()

# Get the needed data
object <- nn2poly_example$weights_list
names(object) <- nn2poly_example$af_string_list

taylor_orders <- nn2poly_example$q_taylor_vector

object <- testing_data$weights_list
names(object) <- testing_data$af_string_list
taylor_orders <- testing_data$taylor_orders

result <- nn2poly(
object = object,
Expand All @@ -34,11 +32,11 @@ test_that("nn2poly with list input against precomputed example with single

test_that("nn2poly with list input against precomputed example with
default options", {
nn2poly_example <- nn2poly_example0
testing_data <- testing_helper_1()

# Get the needed data
object <- nn2poly_example$weights_list
names(object) <- nn2poly_example$af_string_list
object <- testing_data$weights_list
names(object) <- testing_data$af_string_list

result <- nn2poly(
object = object
Expand Down Expand Up @@ -96,7 +94,9 @@ test_that("nn2poly for a nn_module object", {
skip_if_not_installed("torch")
skip_on_cran()

data <- luz_test_data(nn2poly_example0)
testing_data <- testing_helper_2()

data <- luz_test_data(testing_data)

fitted <- luz_test_model() %>%
luz::setup(
Expand All @@ -109,21 +109,20 @@ test_that("nn2poly for a nn_module object", {
luz::fit(data$train, epochs = 5, valid_data = data$valid, verbose = FALSE)

result <- nn2poly(fitted,
taylor_orders = nn2poly_example0$q_taylor_vector,
taylor_orders = testing_data$taylor_orders,
max_order = 3)

expect_equal(round(result$values[1,1],2), 0.11)
# expect_equal(result$values[2,1], -0.45410551)
expect_equal(round(result$values[1,1],2), 0.06)
expect_equal(result$labels[[7]], c(1,1,1))

})

test_that("nn2poly error when wrong dimensions are given in weights", {
nn2poly_example <- nn2poly_example0
testing_data <- testing_helper_1()

# Get the needed data
object <- nn2poly_example$weights_list
names(object) <- nn2poly_example$af_string_list
object <- testing_data$weights_list
names(object) <- testing_data$af_string_list

# Add weights to one layer to create the problem
object[[2]] <- rbind(object[[2]], c(1,1))
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-nn2poly_algorithm.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

test_that("nn2poly_algorithm against precomputed example", {
# Load the example:
nn2poly_example <- nn2poly_example0
testing_data <- testing_helper_1()

# Get the needed data
weights_list <- nn2poly_example$weights_list
af_string_list <- nn2poly_example$af_string_list
taylor_orders <- nn2poly_example$q_taylor_vector
weights_list <- testing_data$weights_list
af_string_list <- testing_data$af_string_list
taylor_orders <- testing_data$taylor_orders

result <- nn2poly_algorithm(
weights_list = weights_list,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ test_that("The get_parameters function works for a torch model (nn_module)
nn <- luz_test_model()

params <- get_parameters(nn)
expect_equal(params$af_string_list, nn2poly_example0$af_string_list)
expect_equal(params$af_string_list, list("softplus", "softplus", "linear"))
})
16 changes: 8 additions & 8 deletions tests/testthat/test-plot_n_important_coeffs.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test_that("Test the plot for a polynomial generated `keep_layers = FALSE`", {
# loading the example
nn2poly_example <- nn2poly_example0
object <- nn2poly_example$weights_list
names(object) <- nn2poly_example$af_string_list
taylor_orders <- nn2poly_example$q_taylor_vector
testing_data <- testing_helper_1()
object <- testing_data$weights_list
names(object) <- testing_data$af_string_list
taylor_orders <- testing_data$taylor_orders

# computing the polynomial
result <- nn2poly(
Expand All @@ -21,10 +21,10 @@ test_that("Test the plot for a polynomial generated `keep_layers = FALSE`", {

test_that("Test the plot for a polynomial generated `keep_layers = TRUE`", {
# loading the example
nn2poly_example <- nn2poly_example0
object <- nn2poly_example$weights_list
names(object) <- nn2poly_example$af_string_list
taylor_orders <- nn2poly_example$q_taylor_vector
testing_data <- testing_helper_1()
object <- testing_data$weights_list
names(object) <- testing_data$af_string_list
taylor_orders <- testing_data$taylor_orders

# computing the polynomial
result <- nn2poly(
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-plot_taylor_and_activation_potentials.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ test_that("The function works as expected", {
skip_on_covr()
skip_on_ci()

testing_data <- testing_helper_2()

nn <- keras_test_model()

# compile the model
Expand All @@ -16,8 +18,8 @@ test_that("The function works as expected", {

# train the model
keras::fit(nn,
nn2poly_example0$train_x,
nn2poly_example0$train_y,
testing_data$train_x,
testing_data$train_y,
verbose = 0,
epochs = 30,
validation_split = 0.3
Expand All @@ -26,8 +28,8 @@ test_that("The function works as expected", {
# compute the different plots
plots <- plot_taylor_and_activation_potentials(
object = nn,
data = cbind(nn2poly_example0$train_x, nn2poly_example0$train_y),
taylor_orders = nn2poly_example0$q_taylor_vector,
data = cbind(testing_data$train_x, testing_data$train_y),
taylor_orders = testing_data$taylor_orders,
max_order = 3,
constraints = FALSE)

Expand Down
Loading

0 comments on commit 9407f50

Please sign in to comment.