Keras: Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'int' object has no attribute 'ndim'

Created on 26 Jan 2018  路  13Comments  路  Source: rstudio/keras

Hello,

This issue is very similar to https://github.com/rstudio/keras/issues/33 but with a different error. I can't run any of the R keras imdb examples but can run the mnist examples.

This is a sample of one of the imdb examples that won't run.

library(keras)
use_condaenv('py35', required = TRUE)
reticulate::py_config()
tf_config()

# Parameters --------------------------------------------------------------

# Embedding
max_features = 20000
maxlen = 100
embedding_size = 128

# Convolution
kernel_size = 5
filters = 64
pool_size = 4

# LSTM
lstm_output_size = 70

# Training
batch_size = 30
epochs = 2

# Data Preparation --------------------------------------------------------

# The x data includes integer sequences, each integer is a word
# The y data includes a set of integer labels (0 or 1)
# The num_words argument indicates that only the max_fetures most frequent
# words will be integerized. All other will be ignored.
# See help(dataset_imdb)
imdb <- dataset_imdb(num_words = max_features)
# Keras load all data into a list with the following structure:
str(imdb)

# Pad the sequences to the same length
# This will convert our dataset into a matrix: each line is a review
# and each column a word on the sequence
# We pad the sequences with 0s to the left
x_train <- imdb$train$x %>%
  pad_sequences(maxlen = maxlen)
x_test <- imdb$test$x %>%
  pad_sequences(maxlen = maxlen)

# Defining Model ------------------------------------------------------

model <- keras_model_sequential()

model %>%
  layer_embedding(max_features, embedding_size, input_length = maxlen) %>%
  layer_dropout(0.25) %>%
  layer_conv_1d(
    filters, 
    kernel_size, 
    padding = "valid",
    activation = "relu",
    strides = 1
  ) %>%
  layer_max_pooling_1d(pool_size) %>%
  layer_lstm(lstm_output_size) %>%
  layer_dense(1) %>%
  layer_activation("sigmoid")

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

# Training ----------------------------------------------------------------

model %>% fit(
  x_train, imdb$train$y,
  batch_size = batch_size,
  epochs = epochs,
  validation_data = list(x_test, imdb$test$y)
)

As in the other ticket I saw this is the output i get from reticulate

> reticulate::py_config()
python:         C:\Users\jmauzey\AppData\Local\conda\conda\envs\r-tensorflow\python.exe
libpython:      C:/Users/jmauzey/AppData/Local/conda/conda/envs/r-tensorflow/python36.dll
pythonhome:     C:\Users\jmauzey\AppData\Local\conda\conda\envs\R-TENS~1
version:        3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\jmauzey\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\numpy
numpy_version:  1.14.0
keras:          C:\Users\jmauzey\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\keras

And the output from the python versions found

python versions found: 
 C:\Users\jmauzey\AppData\Local\conda\conda\envs\r-tensorflow\python.exe
 C:\Users\jmauzey\AppData\Local\CONTIN~1\ANACON~1\python.exe
 C:\Users\jmauzey\AppData\Local\Continuum\anaconda3\python.exe
 C:\Users\jmauzey\AppData\Local\conda\conda\envs\my_root\python.exe
 C:\Users\jmauzey\AppData\Local\conda\conda\envs\tf-keras\python.exe

I have been struggling with this for a couple of days. I had been experiencing another issue and read that uninstalling anaconda would help. I removed it and it solved my problem. I used R keras for a week but then needed jupyter notebook again so reinstalled anaconda thinking that a fresh installation would help. R keras hasn't worked after that. I have attempted uninstalling R, R studio, all python and anaconda installations in an attempt to start from scratch. I have reinstalled reticulate as well. I have also noticed this message when using install_keras()

> install_keras()
Using r-tensorflow conda environment for TensorFlow installation
Solving environment: ...working... 
WARNING: The remote server could not find the noarch directory for the
requested channel with url: https://conda.anaconda.org/conda-forge

It is possible you have given conda an invalid channel. Please double-check
your conda configuration using `conda config --show`.

If the requested url is in fact a valid conda channel, please request that the
channel administrator create `noarch/repodata.json` and associated
`noarch/repodata.json.bz2` files, even if `noarch/repodata.json` is empty.
$ mkdir noarch
$ echo '{}' > noarch/repodata.json
$ bzip2 -k noarch/repodata.json
done

# All requested packages already installed.

Here are some additional messages I got when using the same commands I saw in the other ticket.

> tf_config()
TensorFlow v1.2.1 (C:\Users\jmauzey\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\keras)
Python v3.6 (C:\Users\jmauzey\AppData\Local\conda\conda\envs\r-tensorflow\python.exe)
> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.419)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Denver              
 date     2018-01-26                  

Packages --------------------------------------------------------------------------------------------------------------------------------------------------
 package    * version date       source        
 base       * 3.4.3   2017-12-06 local         
 base64enc    0.1-3   2015-07-28 CRAN (R 3.4.0)
 compiler     3.4.3   2017-12-06 local         
 datasets   * 3.4.3   2017-12-06 local         
 devtools     1.13.4  2017-11-09 CRAN (R 3.4.3)
 digest       0.6.14  2018-01-14 CRAN (R 3.4.3)
 graphics   * 3.4.3   2017-12-06 local         
 grDevices  * 3.4.3   2017-12-06 local         
 jsonlite     1.5     2017-06-01 CRAN (R 3.4.3)
 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-06 local         
 R6           2.2.2   2017-06-17 CRAN (R 3.4.3)
 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-06 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-06 local         
 utils      * 3.4.3   2017-12-06 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.0.6   2017-09-28 CRAN (R 3.4.3)

Let me know anything else I can provide.

Most helpful comment

This should also be fixed in the development version of the keras R package, which you can install with:

devtools::install_github("rstudio/keras")

All 13 comments

I believe I am having problems with my numpy installation. Sorry about bothering. I am hoping to get it fixed. I am not sure how to close a ticket I created.

I do get exactly the same error while running the IMDB example from above.

Error in py_call_impl(callable, dots$args, dots$keywords) : 
 AttributeError: 'int' object has no attribute 'ndim' 

Here is the output of py_config:

reticulate::py_config()
#> python:         /Users/LVG/miniconda3/envs/r-tensorflow/bin/python
#> libpython:      /Users/LVG/miniconda3/envs/r-tensorflow/lib/libpython3.6m.dylib
#> pythonhome:     /Users/LVG/miniconda3/envs/r-tensorflow:/Users/LVG/miniconda3/envs/r-tensorflow
#> version:        3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33)  [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
#> numpy:          /Users/LVG/miniconda3/envs/r-tensorflow/lib/python3.6/site-packages/numpy
#> numpy_version:  1.14.0
#> 
#> python versions found: 
#>  /Users/LVG/miniconda3/envs/r-tensorflow/bin/python
#>  /usr/bin/python
#>  /Users/LVG/miniconda3/bin/python

Any help will be much appreciated.
Thank you.

devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.3 (2017-11-30)
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_CA.UTF-8                 
#>  tz       America/Montreal            
#>  date     2018-01-28
#> Packages -----------------------------------------------------------------
#>  package   * version date       source        
#>  backports   1.1.2   2017-12-13 CRAN (R 3.4.3)
#>  base      * 3.4.3   2017-12-07 local         
#>  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.14  2018-01-14 CRAN (R 3.4.3)
#>  evaluate    0.10.1  2017-06-24 CRAN (R 3.4.1)
#>  graphics  * 3.4.3   2017-12-07 local         
#>  grDevices * 3.4.3   2017-12-07 local         
#>  htmltools   0.3.6   2017-04-28 CRAN (R 3.4.0)
#>  knitr       1.18    2017-12-27 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         
#>  Rcpp        0.12.15 2018-01-20 CRAN (R 3.4.3)
#>  rmarkdown   1.8     2017-11-17 CRAN (R 3.4.2)
#>  rprojroot   1.3-2   2018-01-03 CRAN (R 3.4.3)
#>  stats     * 3.4.3   2017-12-07 local         
#>  stringi     1.1.6   2017-11-17 CRAN (R 3.4.2)
#>  stringr     1.2.0   2017-02-18 CRAN (R 3.4.0)
#>  tools       3.4.3   2017-12-07 local         
#>  utils     * 3.4.3   2017-12-07 local         
#>  withr       2.1.1   2017-12-19 CRAN (R 3.4.3)
#>  yaml        2.1.16  2017-12-12 CRAN (R 3.4.3)

@jpip Could you please tell me how you fixed that problem.
Thanks.

A few more observations about this error.

  1. I am not able to reproduce it on a Windows machine. However, I have been able to reproduce it on 3 different Mac OS machines (2 with anaconda and 1 without).
  2. The issue is somehow linked to the validation_data = list(x_test, imdb$test$y) argument of the fit function. If this argument is removed, the problem disappears. If we use validation_split argument instead, the problem disappears again.

I actually have not been able to resolve this issue yet. Just did what @LVG77 recommended and my model is now training. This is odd. The mnist examples use that same line of code.

I use validation split instead it works as @LVG77 said.

Fixed here: https://github.com/rstudio/keras/commit/efae072c7f18f6910a25c804176de0692d29c6cb

This was indeed an issue introduced in Keras 2.1.3 which is why I couldn't repro it at first. Thanks for pointing out the source of the trouble!

In case anyone is following along in "Deep Learning with R" and getting this error on the first IMDB example: in the fit step, just change the line

history <- model %>% fit( partial_x_train, partial_y_train, epochs = 20, batch_size = 512, validation_data = list(x_val, y_val) )

to

history <- model %>% fit( x_train, y_train, epochs = 20, batch_size = 512, validation_split = 0.4 )

This should also be fixed in the development version of the keras R package, which you can install with:

devtools::install_github("rstudio/keras")

I still get the same error: "Error in py_call_impl(callable, dots$args, dots$keywords)".

I did install the development version from github, but when I try to run the command

history <- model %>% fit_generator(
  train_gen,
  steps_per_epoch = 500,
  epochs = 20,
  validation_data = val_gen,
  validation_steps = val_steps
)

I get this error.

Had the same problem in 2019 (following Deep learning with R book) and installing the dev version from github solved the problem.

Had the same problem in 2019 (following Deep learning with R book) and installing the dev version from github solved the problem.

Hi, I am too following the Deep learning R book and I did install keras from devtools, but I keep getting this error when I run install_keras()
Error 1 occured creating conda enviroment r-reticulate.

How can I change the PATH to be the correct one?

@saikmsm I suggest you open a new issue pertinent to your error message.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fderyckel picture fderyckel  路  4Comments

faltinl picture faltinl  路  6Comments

MaximilianPi picture MaximilianPi  路  5Comments

gsimchoni picture gsimchoni  路  3Comments

jingweihuang picture jingweihuang  路  6Comments