rstudio / keras3

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

Live ploting training/validation history in shiny app #978

Open maalid opened 4 years ago

maalid commented 4 years ago

Hi! I was wondering how to insert in a shiny app a “live” history plot while the model is training/validating. I mean, the same plot that is shown in RStudio viewer pane.

Due to the research I have done, I believe that one way to do it is by using a custom callback function, but I’m not sure how to send the info (make it reactive) while the keras fit() function is working.

Any ideas?

Thanks in advance.

dfalbel commented 4 years ago

You need to find a plot library that allows you to dynamically modify the data via js. For example you can display the metrics using something like this:

library(shiny)
library(keras)

ui <- fluidPage(
    shinyjs::useShinyjs(),  # Include shinyjs
    actionButton("button", "Click me"),
    textOutput('metrics')
)

server <- function(input, output) {

    observeEvent(input$button, {

        model <- keras_model_sequential() %>%
            layer_dense(1)

        model %>%
            compile(loss = "mse" , optimizer = "adam", metrics = "mse")

        cb <- callback_lambda(on_epoch_end = function(epoch, logs) {
            shinyjs::html("metrics", paste("mse: ", logs$mse))
        })

        model %>% 
            fit(
                x = matrix(runif(100), ncol = 1), 
                y = matrix(runif(100), ncol = 1),
                callbacks = list(cb),
                verbose = 0
                )

    })

}

# Run the application 
shinyApp(ui = ui, server = server)
fabrizio1208 commented 4 years ago

Hi dfalbel, I found your input very useful and I am definitely going into js soon.

I have 2 question related to this issue:

  1. If I run your code, the 1st time is fine, the 2nd time pressing the "Click me" button will close my app. The 3rd is fine and the 4th closes. And so on. Do you have an idea why?

  2. Can I use the same technique to update interactively the training history ggplot while training?

Following my sessionInfo():

`R version 3.6.3 (2020-02-29) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 16299)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] keras_2.2.5.0 shiny_1.4.0.2

loaded via a namespace (and not attached): [1] Rcpp_1.0.3 digest_0.6.25 zeallot_0.1.0 later_1.0.0 rappdirs_0.3.1 mime_0.9
[7] R6_2.4.1 jsonlite_1.6.1 xtable_1.8-4 magrittr_1.5 tfruns_1.4 rlang_0.4.5
[13] promises_1.1.0 whisker_0.4 reticulate_1.14 generics_0.0.2 shinyjs_1.1 tools_3.6.3
[19] rsconnect_0.8.16 httpuv_1.5.2 fastmap_1.0.1 compiler_3.6.3 base64enc_0.1-3 tensorflow_2.0.0 [25] htmltools_0.4.0 `

dfalbel commented 4 years ago

Hi @fabrizio1208 , can you try with the dev version of keras (devtools::install_github("rstudio/keras").

It seems that this is a bug of current CRAN Keras & more recent versions of TensorFlow.