rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
839 stars 282 forks source link

neural structured learning #940

Open ringprince opened 5 years ago

ringprince commented 5 years ago

How is it possible to use graph regularization / neural structured learning from R?

If that is not possible, please consider this a feature request.

dfalbel commented 5 years ago

You should be able to use them using reticulate. First install the nsl library:

reticulate::py_install("neural-structured-learning", pip = TRUE)

Then the following should work:

library(keras)
nsl <- reticulate::import("neural_structured_learning")

mnist <- dataset_mnist()
mnist$train$x <- mnist$train$x/255
mnist$test$x <- mnist$train$x/255

model <- keras_model_sequential() %>% 
  layer_flatten(input_shape = c(28, 28)) %>% 
  layer_dense(units = 128, activation = "relu") %>% 
  layer_dense(units = 10, activation = "softmax")

adv_config <- nsl$configs$make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)
adv_model <- nsl$keras$AdversarialRegularization(model, adv_config=adv_config)

adv_model %>% 
  compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics='accuracy'
  )

adv_model %>% 
  fit(list('feature' = mnist$train$x, 'label'=  mnist$train$y), batch_size=32, epochs=5)
ringprince commented 5 years ago

great. Thanks.