Search code examples
rkerasneural-networkdropout

R Keras: apply dropout regularization both on the input and the hidden layer


I'm learning Keras in R and want to apply dropout regularization both on the input layer as it's very big (20000 variables) and the intermediate layer (100 neurons). I'm deploying Keras for regression. From the official documentation I've reached to the below model build:

model <- keras_model_sequential() %>%
         layer_dense(units = 100, activation = "relu", input_shape = 20000) %>%
         layer_dropout(0.6) %>%
         layer_dense(units = 1)

How should I adapt the code to achieve what I intend to make?


Solution

  • I've found a solution so I'm posting an answer in case someone else bumps into the same problem.

    model <- keras_model_sequential() %>%
             layer_dropout(0.8, input_shape = 20000) %>%
             layer_dense(units = 100, activation = "relu") %>%
             layer_dropout(0.6) %>%
             layer_dense(units = 1)