if (FALSE) {
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]
}
}
Run the code above in your browser using DataLab