Skip to content

Commit

Permalink
Simplified constraints examples to avoid training, added requireNames…
Browse files Browse the repository at this point in the history
…pace when using dependecies from Suggests.
  • Loading branch information
moralapablo committed Jan 15, 2024
1 parent 93383e6 commit 269aee7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 129 deletions.
106 changes: 38 additions & 68 deletions R/constraints.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,74 +23,44 @@
#' @seealso [luz_model_sequential()]
#'
#' @examples
#' # Generate random data:
#' train_x = matrix(rnorm(200), ncol=2)
#' train_y = rnorm(100)
#' # ---- Example with a keras/tensorflow network ----
#' # Build a small nn:
#' nn <- keras::keras_model_sequential()
#' nn <- keras::layer_dense(nn, units = 10, activation = "tanh", input_shape = 2)
#' nn <- keras::layer_dense(nn, units = 1, activation = "linear")
#'
#' # Add constraints
#' nn_constrained <- add_constraints(nn, constraint_type = "l1_norm")
#'
#' # Compile and train (2 epochs )
#' keras::compile(nn_constrained,
#' loss = "mse",
#' optimizer = keras::optimizer_adam(),
#' metrics = "mse")
#'
#' history <- keras::fit(nn_constrained,
#' train_x,
#' train_y,
#' verbose = 0,
#' epochs = 2,
#' batch_size = 50,
#' validation_split = 0.2
#' )
#'
#' #' # ---- Example with a luz/torch network ----
#' # Create a torch data loader to be able to train the NN:
#' # Divide in only train and validation
#' all_indices <- 1:nrow(train_x)
#' only_train_indices <- sample(all_indices, size = round(nrow(train_x)) * 0.8)
#' val_indices <- setdiff(all_indices, only_train_indices)
#'
#' # Create lists with x and y values to feed luz::as_dataloader()
#' only_train_x <- as.matrix(train_x[only_train_indices,])
#' only_train_y <- as.matrix(train_y[only_train_indices])
#' val_x <- as.matrix(train_x[val_indices,])
#' val_y <- as.matrix(train_y[val_indices])
#'
#' only_train_list <- list(x = only_train_x, y = only_train_y)
#' val_list <- list(x = val_x, y = val_y)
#'
#' torch_data <- list(
#' train = luz::as_dataloader(only_train_list, batch_size = 50, shuffle = TRUE),
#' valid = luz::as_dataloader(val_list, batch_size = 50)
#' )
#'
#' # Build a small nn:
#' nn <- luz_model_sequential(
#' torch::nn_linear(2,10),
#' torch::nn_tanh(),
#' torch::nn_linear(10,1)
#' )
#'
#' # Train the model adding the constraints inside the pipe
#' # (equivalent to the approach used in the previous example)
#' fitted <- nn |>
#' luz::setup(
#' loss = torch::nn_mse_loss(),
#' optimizer = torch::optim_adam,
#' metrics = list(
#' luz::luz_metric_mse()
#' )
#' ) |>
#' luz::fit(torch_data$train, epochs = 2, valid_data = torch_data$valid)
#'
#'
#' \dontrun{
#' if (requireNamespace("keras", quietly=TRUE)) {
#' # ---- Example with a keras/tensorflow network ----
#' # Build a small nn:
#' nn <- keras::keras_model_sequential()
#' nn <- keras::layer_dense(nn, units = 10, activation = "tanh", input_shape = 2)
#' nn <- keras::layer_dense(nn, units = 1, activation = "linear")
#'
#' # Add constraints
#' nn_constrained <- add_constraints(nn, constraint_type = "l1_norm")
#'
#' # Check that class of the constrained nn is "nn2poly"
#' class(nn_constrained)[1]
#' }
#' }
#'
#' if (requireNamespace("luz", quietly=TRUE)) {
#' # ---- Example with a luz/torch network ----
#'
#' # Build a small nn
#' nn <- luz_model_sequential(
#' torch::nn_linear(2,10),
#' torch::nn_tanh(),
#' torch::nn_linear(10,1)
#' )
#'
#' # With luz/torch we need to setup the nn before adding the constraints
#' nn <- luz::setup(module = nn,
#' loss = torch::nn_mse_loss(),
#' optimizer = torch::optim_adam,
#' )
#'
#' # Add constraints
#' nn <- add_constraints(nn)
#'
#' # Check that class of the constrained nn is "nn2poly"
#' class(nn)[1]
#' }
#'
#' @export
add_constraints <- function(object, type = c("l1_norm", "l2_norm"), ...) {
Expand Down
5 changes: 5 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#' @seealso [add_constraints()]
#'
#' @examples
#' if (requireNamespace("luz", quietly=TRUE)) {
#' # Create a NN using luz/torch as a sequential model
#' # with 3 fully connected linear layers,
#' # the first one with input = 5 variables,
Expand All @@ -31,6 +32,10 @@
#'
#' nn
#'
#' # Check that the nn is of class nn_squential
#' class(nn)
#' }
#'
#'
#' @export
luz_model_sequential <- function(...) {
Expand Down
90 changes: 29 additions & 61 deletions man/add_constraints.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/luz_model_sequential.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 269aee7

Please sign in to comment.