keras:::as.data.frame.keras_training_history and early stopping

Created on 28 Aug 2020  路  2Comments  路  Source: rstudio/keras

The function keras:::as.data.frame.keras_training_history uses the number of epochs instead of the number of rows in

df <- data.frame(epoch = seq_len(x$params$epochs), value = unlist(values),

This is incompatible with an early stopping callback. Put differently: The history plotting does not work with early stopping.

My keras package has version 2.3.0.0.

bug

Most helpful comment

I have the same problem. I believe the problem is here:

https://github.com/rstudio/keras/blob/ad737d1f9f7720d021db381f6ef9a02d63b79e98/R/history.R#L182-L186

It should be replaced with:

values[] <- lapply(values, `length<-`, x$params$epochs)

Till the issue isn't solved, you can hack it this way:

# set plot.keras_training_history in your global env to call user functions
plot.keras_training_history <- keras:::plot.keras_training_history
environment(plot.keras_training_history) <- globalenv()

# replace as.dataframe with custom function
as.data.frame.keras_training_history <- function (x, ...){

 if (tensorflow::tf_version() < "2.2") 
  x$metrics <- x$metrics[x$params$metrics]
 values <- x$metrics

 # pad <- x$params$epochs - length(values$loss)
 # pad_data <- list()
 # for (metric in x$params$metrics) pad_data[[metric]] <- rep_len(NA, 
 #                                                                pad)
 # values <- rbind(values, pad_data)

 values[] <- lapply(values, `length<-`, x$params$epochs)
 df <- data.frame(epoch = seq_len(x$params$epochs), value = unlist(values), 
                  metric = rep(sub("^val_", "", names(x$metrics)), each = x$params$epochs),
                  data = rep(grepl("^val_", names(x$metrics)), each = x$params$epochs))
 rownames(df) <- NULL
 df$data <- factor(df$data, c(FALSE, TRUE), c("training", "validation"))
 df$metric <- factor(df$metric, unique(sub("^val_", "", names(x$metrics))))
 df

}

TRY IT OUT FROM THIS EXAMPLE

plot(history)

All 2 comments

I am having the same problem with the same version of keras. Looks like a return of an old issue (#117), I get he same error message as mentioned in that issue when I try and plot the history when the model has stopped early.

I have the same problem. I believe the problem is here:

https://github.com/rstudio/keras/blob/ad737d1f9f7720d021db381f6ef9a02d63b79e98/R/history.R#L182-L186

It should be replaced with:

values[] <- lapply(values, `length<-`, x$params$epochs)

Till the issue isn't solved, you can hack it this way:

# set plot.keras_training_history in your global env to call user functions
plot.keras_training_history <- keras:::plot.keras_training_history
environment(plot.keras_training_history) <- globalenv()

# replace as.dataframe with custom function
as.data.frame.keras_training_history <- function (x, ...){

 if (tensorflow::tf_version() < "2.2") 
  x$metrics <- x$metrics[x$params$metrics]
 values <- x$metrics

 # pad <- x$params$epochs - length(values$loss)
 # pad_data <- list()
 # for (metric in x$params$metrics) pad_data[[metric]] <- rep_len(NA, 
 #                                                                pad)
 # values <- rbind(values, pad_data)

 values[] <- lapply(values, `length<-`, x$params$epochs)
 df <- data.frame(epoch = seq_len(x$params$epochs), value = unlist(values), 
                  metric = rep(sub("^val_", "", names(x$metrics)), each = x$params$epochs),
                  data = rep(grepl("^val_", names(x$metrics)), each = x$params$epochs))
 rownames(df) <- NULL
 df$data <- factor(df$data, c(FALSE, TRUE), c("training", "validation"))
 df$metric <- factor(df$metric, unique(sub("^val_", "", names(x$metrics))))
 df

}

TRY IT OUT FROM THIS EXAMPLE

plot(history)
Was this page helpful?
0 / 5 - 0 ratings