Error in keras when trying to recreate example from website

Hi,

I recently installed keras/tensorflow on my computer (macOS), and I am trying to run one of the standard examples (the one on the front page of https://keras.rstudio.com), and I receive the following error when fitting my model:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: update() takes from 2 to 3 positional arguments but 4 were given 

Does anyone have any clue what could be causing it? I copied and pasted the code straight from the website.


My code is pasted here:

library(keras)
use_condaenv("r-tensorflow")
mnist <- dataset_mnist()
x_train <- mnist$train$x
y_train <- mnist$train$y
x_test <- mnist$test$x
y_test <- mnist$test$y

# reshape
x_train <- array_reshape(x_train, c(nrow(x_train), 784))
x_test <- array_reshape(x_test, c(nrow(x_test), 784))
# rescale
x_train <- x_train / 255
x_test <- x_test / 255

y_train <- to_categorical(y_train, 10)
y_test <- to_categorical(y_test, 10)

model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>% 
  layer_dropout(rate = 0.4) %>% 
  layer_dense(units = 128, activation = 'relu') %>%
  layer_dropout(rate = 0.3) %>%
  layer_dense(units = 10, activation = 'softmax')

model %>% compile(
  loss = 'categorical_crossentropy',
  optimizer = optimizer_rmsprop(),
  metrics = c('accuracy')
)

history <- model %>% fit(
  x_train, y_train, 
  epochs = 30, batch_size = 128, 
  validation_split = 0.2
)

The error shows up on the last part of the code. The traceback is as follows:

18.
stop(structure(list(message = "TypeError: update() takes from 2 to 3 positional arguments but 4 were given", 
    call = py_call_impl(callable, dots$args, dots$keywords), 
    cppstack = structure(list(file = "", line = -1L, stack = c("1   reticulate.so                       0x0000000107d4b7f4 _ZN4Rcpp9exceptionC2EPKcb + 276", 
    "2   reticulate.so                       0x0000000107d4b630 _ZN4Rcpp4stopERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE + 48",  ... 
17.
update_with_patch at progbar.py#23
16.
on_batch_end at callbacks.py#331
15.
on_batch_end at callbacks.py#113
14.
_fit_loop at training.py#1241
13.
fit at training.py#1712
12.
fit at models.py#963
11.
(structure(function (...) 
{
    dots <- py_resolve_dots(list(...))
    result <- py_call_impl(callable, dots$args, dots$keywords) ... 
10.
do.call(object$fit, args) 
9.
fit(., x_train, y_train, epochs = 30, batch_size = 128, validation_split = 0.2) 
8.
function_list[[k]](value) 
7.
withVisible(function_list[[k]](value)) 
6.
freduce(value, `_function_list`) 
5.
`_fseq`(`_lhs`) 
4.
eval(quote(`_fseq`(`_lhs`)), env, env) 
3.
eval(quote(`_fseq`(`_lhs`)), env, env) 
2.
withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 
1.
model %>% fit(x_train, y_train, epochs = 30, batch_size = 128, 
    validation_split = 0.2) 

Thanks for your help!

I ran your code and it works fine on my machine (Windows).

Can you please post your session info?

devtools::session_info()
> devtools::session_info()
Session info -------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.1.392)           
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       America/New_York            
 date     2018-02-14                  

Packages -----------------------------------------------------------------
 package    * version date       source        
 base       * 3.4.3   2017-12-07 local         
 base64enc    0.1-3   2015-07-28 cran (@0.1-3) 
 compiler     3.4.3   2017-12-07 local         
 datasets   * 3.4.3   2017-12-07 local         
 devtools     1.13.4  2017-11-09 CRAN (R 3.4.2)
 digest       0.6.15  2018-01-28 CRAN (R 3.4.3)
 graphics   * 3.4.3   2017-12-07 local         
 grDevices  * 3.4.3   2017-12-07 local         
 jsonlite     1.5     2017-06-01 CRAN (R 3.4.0)
 keras      * 2.1.3   2018-01-17 CRAN (R 3.4.3)
 magrittr     1.5     2014-11-22 CRAN (R 3.4.0)
 memoise      1.1.0   2017-04-21 CRAN (R 3.4.0)
 methods    * 3.4.3   2017-12-07 local         
 R6           2.2.2   2017-06-17 CRAN (R 3.4.0)
 Rcpp         0.12.15 2018-01-20 CRAN (R 3.4.3)
 reticulate   1.4     2018-01-09 CRAN (R 3.4.3)
 stats      * 3.4.3   2017-12-07 local         
 tensorflow   1.5     2018-01-17 CRAN (R 3.4.3)
 tfruns       1.2     2018-01-18 CRAN (R 3.4.3)
 tools        3.4.3   2017-12-07 local         
 utils      * 3.4.3   2017-12-07 local         
 whisker      0.3-2   2013-04-28 CRAN (R 3.4.0)
 withr        2.1.1   2017-12-19 CRAN (R 3.4.3)
 yaml         2.1.16  2017-12-12 CRAN (R 3.4.3)
 zeallot      0.1.0   2018-01-28 CRAN (R 3.4.3)

Could the problem somehow be with my python?

Your session info looks essentially the same as mine.

How did you install the python environment?

The keras package has a function install_keras() that will install both Keras and TensorFlow in a conda env called r-tensorflow. This will install all of the python libraries you need.

Do you use tensorflow::install_tensorflow() and keras::install_keras() or did you do something else?

I had originally installed TensorFlow/Keras through python directly through pip (not in a virtual environment)...but then I also installed keras with the keras::install_keras(), creating the new r-tensorflow conda environment.

So (a) is it possible that its a problem that I installed keras first through without using a viraual/conda environment? (b) Is it a problem that I did keras::install_keras() without first doing tensorflow::install_tensorflow()?

Thanks!

HI!
I have exactly the same issue.

Session info ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, darwin15.6.0        
 ui       RStudio (1.0.143)           
 language (EN)                        
 collate  de_DE.UTF-8                 
 tz       Europe/Berlin               
 date     2018-02-15                  

Packages -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                             
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.0)                     
 base       * 3.4.1      2017-07-07 local                              
 base64enc    0.1-3      2015-07-28 CRAN (R 3.4.0)                     
 bindr        0.1        2016-11-13 CRAN (R 3.4.0)                     
 bindrcpp     0.2        2017-06-17 CRAN (R 3.4.0)                     
 colorspace   1.3-2      2016-12-14 CRAN (R 3.4.0)                     
 compiler     3.4.1      2017-07-07 local                              
 curl         2.8.1      2017-07-21 CRAN (R 3.4.1)                     
 data.table * 1.10.4     2017-02-01 CRAN (R 3.4.0)                     
 datasets   * 3.4.1      2017-07-07 local                              
 devtools     1.13.4     2017-11-09 CRAN (R 3.4.2)                     
 digest       0.6.13     2017-12-14 cran (@0.6.13)                     
 dplyr      * 0.7.3      2017-09-09 CRAN (R 3.4.1)                     
 ggplot2    * 2.2.1      2016-12-30 CRAN (R 3.4.0)                     
 glue         1.2.0      2017-10-29 cran (@1.2.0)                      
 graphics   * 3.4.1      2017-07-07 local                              
 grDevices  * 3.4.1      2017-07-07 local                              
 grid         3.4.1      2017-07-07 local                              
 gtable       0.2.0      2016-02-26 CRAN (R 3.4.0)                     
 jsonlite     1.5        2017-06-01 CRAN (R 3.4.0)                     
 keras      * 2.1.2.9001 2018-01-10 Github (rstudio/keras@9f483b0)     
 kerasR     * 0.6.1      2017-06-01 CRAN (R 3.4.0)                     
 lazyeval     0.2.0      2016-06-12 CRAN (R 3.4.0)                     
 magrittr     1.5        2014-11-22 CRAN (R 3.4.0)                     
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                     
 methods    * 3.4.1      2017-07-07 local                              
 munsell      0.4.3      2016-02-13 CRAN (R 3.4.0)                     
 pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.0)                     
 plyr         1.8.4      2016-06-08 CRAN (R 3.4.0)                     
 purrr      * 0.2.4      2017-10-18 cran (@0.2.4)                      
 R6           2.2.2      2017-06-17 CRAN (R 3.4.0)                     
 Rcpp         0.12.15    2018-01-20 cran (@0.12.15)                    
 reticulate * 1.4.0.9001 2018-02-14 Github (rstudio/reticulate@e49861a)
 rlang        0.1.6      2017-12-21 cran (@0.1.6)                      
 rstudioapi   0.7        2017-09-07 CRAN (R 3.4.1)                     
 scales       0.5.0.9000 2017-09-05 Github (hadley/scales@d767915)     
 stats      * 3.4.1      2017-07-07 local                              
 tensorflow * 1.4.3.9000 2018-01-10 Github (rstudio/tensorflow@9d8edba)
 tfruns       1.1        2017-12-17 cran (@1.1)                        
 tibble       1.3.4      2017-08-22 CRAN (R 3.4.1)                     
 tools        3.4.1      2017-07-07 local                              
 utils      * 3.4.1      2017-07-07 local                              
 whisker      0.3-2      2013-04-28 CRAN (R 3.4.0)                     
 withr        2.1.1.9000 2018-01-10 Github (jimhester/withr@df18523)   
 zeallot      0.0.6      2017-09-28 cran (@0.0.6)                      

I used first install_tensorflow and then install_keras.

My python configuration is:

python:         /Users/kk/anaconda/envs/r-tensorflow/bin/python
libpython:      /Users/kk/anaconda/envs/r-tensorflow/lib/libpython3.6m.dylib
pythonhome:     /Users/kk/anaconda/envs/r-tensorflow:/Users/sk186171/anaconda/envs/r-tensorflow
version:        3.6.4 | packaged by conda-forge | (default, Dec 23 2017, 16:54:01)  [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
numpy:          /Users/kk/anaconda/envs/r-tensorflow/lib/python3.6/site-packages/numpy
numpy_version:  1.14.0
keras:          /Users/kk/anaconda/envs/r-tensorflow/lib/python3.6/site-packages/keras

python versions found: 
 /Users/kk/anaconda/envs/r-tensorflow/bin/python
 /usr/bin/python
 /usr/local/bin/python
 /Users/kk/anaconda/bin/python
 /Users/kk/.env/bin/python

Any ideas?

1 Like

I solved it! Yeay!

I had to load the reticulate library from github and also for keras

library(devtools)
install_github('rstudio/reticulate',force=T)
library(reticulate)
library(tensorflow)
install_tensorflow(version= "1.1.0")
install_github("rstudio/keras",force=T)
library(keras)
keras::install_keras()

Now it's running.

1 Like

This error was introduced in the update from Keras 2.1.3 to 2.1.4. The issue is fixed on the master branch on GitHub so the fix is (as you said):

devtools::install_github("rstudio/keras")
3 Likes