Activate dropout layers during inference in R

Viewed 37

I have a Keras model in R, and am looking to perform Monte Carlo dropout during inference. I know there are methods in Python by turning training = TRUE, but I can't find a similar functionality in R.

Take an example Keras model:

model <- keras_model_sequential() %>%
    layer_dense(10, activation = "relu") %>%
    layer_dropout(0.2)

(not my model, mine is unnecessarily complex for stackoverflow)

In this case, the dropout will only work during training, but be completely ignored when predictions are made. I want the dropout to work during prediction, so each prediction is different.

1 Answers

You can also use training = TRUE in keras R like this:

library(keras)
dropout_1 <- layer_dropout(rate = 0.2) 

model <- keras_model_sequential() %>%
  layer_dense(10, activation = "relu") %>%
  dropout_1(training = TRUE)

Created on 2022-09-01 with reprex v2.0.2

Related