{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/keras/issues/355","repository_url":"https://api.github.com/repos/rstudio/keras","labels_url":"https://api.github.com/repos/rstudio/keras/issues/355/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/keras/issues/355/comments","events_url":"https://api.github.com/repos/rstudio/keras/issues/355/events","html_url":"https://github.com/rstudio/keras/issues/355","id":312671618,"number":355,"title":"fit_generator() more than twice as slow as fit()","user":{"login":"t-kalinowski","id":8462255,"avatar_url":"https://avatars1.githubusercontent.com/u/8462255?v=4","gravatar_id":"","url":"https://api.github.com/users/t-kalinowski","html_url":"https://github.com/t-kalinowski","followers_url":"https://api.github.com/users/t-kalinowski/followers","following_url":"https://api.github.com/users/t-kalinowski/following{/other_user}","gists_url":"https://api.github.com/users/t-kalinowski/gists{/gist_id}","starred_url":"https://api.github.com/users/t-kalinowski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/t-kalinowski/subscriptions","organizations_url":"https://api.github.com/users/t-kalinowski/orgs","repos_url":"https://api.github.com/users/t-kalinowski/repos","events_url":"https://api.github.com/users/t-kalinowski/events{/privacy}","received_events_url":"https://api.github.com/users/t-kalinowski/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2018-04-09T20:16:29Z","updated_at":"2018-04-11T15:33:37Z","closed_at":null,"author_association":"NONE","body":"Hello, I noticed that `fit_generator()` is significantly slower than `fit()`. I'm not sure if this is surprising behavior, or if it's an inevitable fact of life due to the overhead between calling an R function from python. I'm hopeful that there is some easy optimization that can put these two functions on equal footing. \r\n\r\nHere is an example with some dummy data and a minimal keras model. \r\n\r\n``` r\r\nlibrary(keras)\r\n\r\nbatch_size = 1024\r\nsteps_per_epoch = 1000\r\nd = c(batch_size * steps_per_epoch, 128, 2)\r\nX <- array(rnorm(prod(d)), d)\r\nY <- matrix(0, nrow = nrow(X), ncol = 10)\r\nY[cbind(seq_len(nrow(X)), sample(10, nrow(X), replace = TRUE ))] <- 1\r\n\r\nnew_model <- function() {\r\n  keras_model_sequential() %>%\r\n    layer_flatten( input = c(128, 2)) %>%\r\n    layer_dense(10, use_bias = FALSE, activation = 'softmax') %>%\r\n    compile(optimizer = 'adam', loss = 'mae')\r\n}\r\n\r\nnew_data_generator <- function() {\r\n  start <- 1L\r\n  function() {\r\n    end <- start + batch_size - 1L\r\n    idx <- start:end\r\n    if(end > nrow(X)) {\r\n      over <- idx > nrow(X)\r\n      idx[over] <- seq_len(sum(over))\r\n    }\r\n\r\n    on.exit(start <<- idx[batch_size] + 1L)\r\n    list(X[idx,,], Y[idx,])\r\n  }\r\n}\r\n\r\ngenerator <-  new_data_generator()\r\n\r\nmodel <- new_model()\r\nfit_timing <- system.time(fit(model, x = X, y = Y, batch_size = batch_size,\r\n                              epochs = 3, verbose = 1))\r\n\r\nmodel <- new_model()\r\nfit_generator_timing <- system.time(fit_generator(model, generator = generator,\r\n                          steps_per_epoch = steps_per_epoch, epochs = 3, verbose = 1))\r\n\r\nfit_timing\r\n#>    user  system elapsed \r\n#>  12.521   1.592   8.737\r\nfit_generator_timing\r\n#>    user  system elapsed \r\n#>  25.036   1.587  20.269\r\n```\r\n\r\nCreated on 2018-04-09 by the [reprex package](http://reprex.tidyverse.org) (v0.2.0)."},"comment":{"url":"https://api.github.com/repos/rstudio/keras/issues/comments/380496123","html_url":"https://github.com/rstudio/keras/issues/355#issuecomment-380496123","issue_url":"https://api.github.com/repos/rstudio/keras/issues/355","id":380496123,"user":{"login":"t-kalinowski","id":8462255,"avatar_url":"https://avatars1.githubusercontent.com/u/8462255?v=4","gravatar_id":"","url":"https://api.github.com/users/t-kalinowski","html_url":"https://github.com/t-kalinowski","followers_url":"https://api.github.com/users/t-kalinowski/followers","following_url":"https://api.github.com/users/t-kalinowski/following{/other_user}","gists_url":"https://api.github.com/users/t-kalinowski/gists{/gist_id}","starred_url":"https://api.github.com/users/t-kalinowski/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/t-kalinowski/subscriptions","organizations_url":"https://api.github.com/users/t-kalinowski/orgs","repos_url":"https://api.github.com/users/t-kalinowski/repos","events_url":"https://api.github.com/users/t-kalinowski/events{/privacy}","received_events_url":"https://api.github.com/users/t-kalinowski/received_events","type":"User","site_admin":false},"created_at":"2018-04-11T15:33:37Z","updated_at":"2018-04-11T15:33:37Z","author_association":"NONE","body":"I've posted a minimal gist solving a toy-problem [here](https://gist.github.com/t-kalinowski/6d4074f1b84979016a8ae1f9026125e4)\r\n"}}
{"actor":{"display_login":"t-kalinowski"}}