rstudio / keras3

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

Error in py_call_impl(callable, dots$args, dots$keywords) : AttributeError: 'NoneType' object has no attribute 'write' #33

Closed kdpsingh closed 7 years ago

kdpsingh commented 7 years ago

Hi all, I'm running the code below in R using the keras package.

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

model <- keras_model_sequential()
# add layers and compile the model
model %>% 
  layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% 
  layer_dense(units = 1, activation = 'sigmoid') %>% 
  compile(
    optimizer = 'rmsprop',
    loss = 'binary_crossentropy',
    metrics = c('accuracy')
  )

# Generate dummy data
data <- matrix(runif(1000*100), nrow = 1000, ncol = 100)
labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1)

# Train the model, iterating on the data in batches of 32 samples
model %>% fit(data, labels, epochs=20, batch_size=32)

# output predictions for first 10 rows
model %>% predict(data[1:10,]) %>% round()

# session info
devtools::session_info()

When I get to the line fitting the model (model %>% fit(data, labels, epochs=20, batch_size=32)), my script produces the following error:

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

Detailed traceback: 
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 835, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1494, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1144, in _fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end
    callback.on_batch_end(batch, logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callback

Strangely, when I compile/knit this as a report, everything works fine the FIRST time but then the error recurs. Any ideas what could be causing this?

Here's the output from py_config, tf_config, and session_info.

> reticulate::py_config()
python:         C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
libpython:      C:/Users/kdpsingh/AppData/Local/conda/conda/envs/py35/python35.dll
pythonhome:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35
version:        3.5.3 |Anaconda 4.4.0 (64-bit)| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow

python versions found: 
 C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
 C:\PROGRA~3\ANACON~1\python.exe

> tf_config()
TensorFlow v1.1.0 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow)
Python v3.5 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe)

> devtools::session_info()
Session info ---------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.2 (2016-10-31)
 system   x86_64, mingw32             
 ui       RStudio (1.0.136)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2017-06-06                  

Packages -------------------------------------------------------------------------------------
 package    * version date       source                             
 backports    1.0.4   2016-10-24 CRAN (R 3.3.2)                     
 devtools     1.12.0  2016-06-24 CRAN (R 3.3.3)                     
 digest       0.6.10  2016-08-02 CRAN (R 3.3.2)                     
 evaluate     0.10    2016-10-11 CRAN (R 3.3.2)                     
 htmltools    0.3.5   2016-03-21 CRAN (R 3.3.2)                     
 jsonlite     1.5     2017-06-01 CRAN (R 3.3.3)                     
 keras      * 0.3.6   2017-06-05 Github (rstudio/keras@023d109)     
 knitr        1.15.1  2016-11-22 CRAN (R 3.3.2)                     
 magrittr     1.5     2014-11-22 CRAN (R 3.3.2)                     
 memoise      1.0.0   2016-01-29 CRAN (R 3.3.3)                     
 R6           2.2.1   2017-05-10 CRAN (R 3.3.3)                     
 Rcpp         0.12.7  2016-09-05 CRAN (R 3.3.2)                     
 reticulate   0.8     2017-05-22 CRAN (R 3.3.3)                     
 rmarkdown    1.3     2016-12-21 CRAN (R 3.3.2)                     
 rprojroot    1.1     2016-10-29 CRAN (R 3.3.2)                     
 rstudioapi   0.6     2016-06-27 CRAN (R 3.3.3)                     
 stringi      1.1.2   2016-10-01 CRAN (R 3.3.2)                     
 stringr      1.1.0   2016-08-19 CRAN (R 3.3.2)                     
 tensorflow   0.8.2   2017-06-05 Github (rstudio/tensorflow@bace720)
 withr        1.0.2   2016-06-20 CRAN (R 3.3.3)  
jjallaire commented 7 years ago

I can't repro this locally so this is a bit of mystery. Here's my diagnostics:

> reticulate::py_config()
python:         C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\\python.exe
libpython:      C:/Users/JJALLA~1/AppData/Local/Programs/Python/Python35/python35.dll
pythonhome:     C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35
version:        3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow

python versions found: 
 C:\Python27\\python.exe
 C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\\python.exe
 C:\Users\JJALLA~1\AppData\Local\Programs\Python\PYTHON~1\\python.exe
 C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python36\\python.exe
 C:\Users\JJALLA~1\ANACON~1\python.exe

> tf_config()
TensorFlow v1.1.0 (C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow)
Python v3.5 (C:\Users\JJALLA~1\AppData\Local\Programs\Python\Python35\\python.exe)

> devtools::session_info()
Session info ------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.3 (2017-03-06)
 system   x86_64, mingw32             
 ui       RStudio (1.1.241)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2017-06-06                  

Packages ----------------------------------------------------------------------------------------
 package    * version date       source        
 devtools     1.12.0  2016-06-24 CRAN (R 3.3.3)
 digest       0.6.12  2017-01-27 CRAN (R 3.3.3)
 jsonlite     1.4     2017-04-08 CRAN (R 3.3.3)
 keras      * 0.3.6   2017-06-06 local         
 magrittr     1.5     2014-11-22 CRAN (R 3.3.3)
 memoise      1.0.0   2016-01-29 CRAN (R 3.3.3)
 R6           2.2.0   2016-10-05 CRAN (R 3.3.3)
 Rcpp         0.12.11 2017-05-22 CRAN (R 3.3.3)
 reticulate   0.8     2017-05-22 CRAN (R 3.3.3)
 tensorflow   0.8.2   2017-06-05 local         
 withr        1.0.2   2016-06-20 CRAN (R 3.3.3)
 yaml         2.1.14  2016-11-12 CRAN (R 3.3.3)
> 

I do recall fixing an issue similar to this in reticulate a couple months ago (it was Python 3 specific with a similar error message). One guess: perhaps when you updated reticulate it didn't update the underlying DLL (because it was in use). You might want to try quitting all R sessions then re-installing R sessions.

kdpsingh commented 7 years ago

Thanks @jjallaire. I closed my R session and reinstalled reticulate from CRAN but no luck. Yesterday was actually the first time I had ever installed reticulate so didn't think this would necessarily be the issue. Still seems to work when I knit the R file but fails if I source it or run it in the console.

I wonder if keras (or tensorflow) in Python is trying to write the model output to file as part of building the model and that's returning no output and leading to problems. Not sure why the code is behaving differently when run directly vs. when knitting.

mrecos commented 7 years ago

I also receive this error. Had been running keras/TF in Rstudio fine all week, then today this error popped up. I re-ran the model and all went well. Then, my laptop shut down (I forgot to plug it in...) and now I get the error every time.

I am running the LSTM Nietzsche example code.

edit: running the example model in the above code delivers the same error on my machine.

edit 2: I ran the code block at the bottom to reinstall keras/tensorflow and the above code example ran without error.

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

Detailed traceback: 
  File "C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 835, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1494, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1144, in _fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)
  File "C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end
    callback.on_batch_end(batch, logs)
  File "C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensor 
> reticulate::py_config()
python:         C:\Users\matthew.d.harris\AppData\Local\conda\conda\envs\r-tensorflow\python.exe
libpython:      C:/Users/matthew.d.harris/AppData/Local/conda/conda/envs/r-tensorflow/python35.dll
pythonhome:     C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1
version:        3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow

python versions found: 
 C:/ProgramData/Anaconda3/python.exe
 C:\Users\matthew.d.harris\AppData\Local\conda\conda\envs\r-tensorflow\python.exe
 C:\Python27\ARCGIS~1.4\\python.exe
 C:\PROGRA~3\ANACON~1\python.exe
> tf_config()
TensorFlow v1.1.0 (C:\Users\MATTHE~1.HAR\AppData\Local\conda\conda\envs\R-TENS~1\lib\site-packages\tensorflow)
Python v3.5 (C:\Users\matthew.d.harris\AppData\Local\conda\conda\envs\r-tensorflow\python.exe)
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages:
[1] dplyr_0.5.0      tokenizers_0.1.4 purrr_0.2.2      stringr_1.2.0    readr_1.0.0      tensorflow_0.8.2
[7] keras_0.3.5     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.10    assertthat_0.1  SnowballC_0.5.1 R6_2.2.1        jsonlite_1.4    DBI_0.5-1      
 [7] magrittr_1.5    stringi_1.1.2   reticulate_0.8  tools_3.3.2     tibble_1.2     
Sys.setenv(TENSORFLOW_PYTHON="C:/ProgramData/Anaconda3/python.exe")

devtools::install_github("rstudio/keras", force = TRUE)
library(keras)
keras::install_tensorflow()

library("tensorflow")
tensorflow::use_condaenv("r-tensorflow")

# TF test
sess = tf$Session()
hello <- tf$constant('Hello, TensorFlow!')
sess$run(hello)

# keras test
model <- keras_model_sequential() 
gsimchoni commented 7 years ago

Same here. But I get the error only when running the imdb_lstm.R or cifar10_cnn.R examples. When I run mnist_mlp.R everything goes well. And I also see that when I knit this code in RMarkdown it runs perfectly fine.

> Error in py_call_impl(callable, dots$args, dots$keywords) : 
>   AttributeError: 'NoneType' object has no attribute 'write'
> 
> Detailed traceback: 
>   File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 1081, in fit_generator
>     initial_epoch=initial_epoch)
>   File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1886, in fit_generator
>     callbacks.on_batch_end(batch_index, batch_logs)
>   File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end
>     callback.on_batch_end(batch, logs)
>   File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 303, in on_batch_end
>     self.progbar.update(self.seen, self.log_values)
>   File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\utils\generic_utils.py", line 272, in update
>     sys.stdout.write('
> reticulate::py_config()
python:         C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe
libpython:      C:/PROGRA~3/ANACON~1/envs/r-tensorflow/python35.dll
pythonhome:     C:\PROGRA~3\ANACON~1\envs\R-TENS~1
version:        3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow

python versions found: 
 C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe
 C:\PROGRA~3\ANACON~1\python.exe
 C:\PROGRA~3\ANACON~1/envs/keras_tf/python.exe
> tf_config()
TensorFlow v1.1.0 (C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow)
Python v3.5 (C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe)
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages:
[1] keras_0.3.6

loaded via a namespace (and not attached):
[1] magrittr_1.5     R6_2.2.1         tools_3.3.3      Rcpp_0.12.8      reticulate_0.8   tensorflow_0.8.2
[7] jsonlite_1.5    
jjallaire commented 7 years ago

I now have a repro for this: for me the error occurs in R GUI but not in Rterm or RStudio. The underlying cause is that Python sys$stdout is NULL within R GUI (thus the error when attempting to call write). The solution will probably be to provide sys.stdout / sys.stderr (mapping it to R output functions) when it's not available. More soon.

On Wed, Jun 7, 2017 at 1:36 AM, Giora Simchoni notifications@github.com wrote:

Same here. But I get the error only when running the imdb_lstm.R or cifar10_cnn.R examples. When I run mnist_mlp.R everything goes well.

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

Detailed traceback: File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 1081, in fit_generator initial_epoch=initial_epoch) File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1886, in fit_generator callbacks.on_batch_end(batch_index, batch_logs) File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end callback.on_batch_end(batch, logs) File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 303, in on_batch_end self.progbar.update(self.seen, self.log_values) File "C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow\contrib\keras\python\keras\utils\generic_utils.py", line 272, in update sys.stdout.write('

reticulate::py_config() python: C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe libpython: C:/PROGRA~3/ANACON~1/envs/r-tensorflow/python35.dll pythonhome: C:\PROGRA~3\ANACON~1\envs\R-TENS~1 version: 3.5.3 |Continuum Analytics, Inc.| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)] Architecture: 64bit numpy: C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\numpy numpy_version: 1.12.1 tensorflow: C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow

python versions found: C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe C:\PROGRA~3\ANACON~1\python.exe C:\PROGRA~3\ANACON~1/envs/keras_tf/python.exe

tf_config() TensorFlow v1.1.0 (C:\PROGRA~3\ANACON~1\envs\R-TENS~1\lib\site-packages\tensorflow) Python v3.5 (C:\PROGRA~3\ANACON~1/envs/r-tensorflow/python.exe)

R version 3.3.3 (2017-03-06) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages: [1] keras_0.3.6

loaded via a namespace (and not attached): [1] magrittr_1.5 R6_2.2.1 tools_3.3.3 Rcpp_0.12.8 reticulate_0.8 tensorflow_0.8.2 [7] jsonlite_1.5

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/33#issuecomment-306728685, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGXx5L2cSQGKdybko9B0tYazEs9czEaks5sBmD8gaJpZM4NxoyT .

jjallaire commented 7 years ago

I've made a fix to the reticulate package to provide an implementation of sys.stdout and sys.stderr when they aren't found (I've seen this in R GUI but it's certainly possible it happens elsewhere). To install the fix you should be sure to close all R sessions then open a fresh R session and execute:

devtools::install_github("rstudio/reticulate")

The reason you need to close all R sessions is that windows shared libraries won't be successfully overwritten if they are in use during the installation.

One note: Keras progress isn't nearly as nice in this scenario (it doesn't update in place) so if you can run within R terminal or RStudio you'll have a better user experience.

kdpsingh commented 7 years ago

Great, thank you so much! I'll give this a shot later today. I only use RStudio and for some reason this issue was cropping up there. I'll post an update once I try it out. Thanks again.

jjallaire commented 7 years ago

Okay, thanks. Good to know that this can happen in RStudio as well. Let me know what you find when you try it out!

On Wed, Jun 7, 2017 at 3:51 PM, kdpsingh notifications@github.com wrote:

Great, thank you so much! I'll give this a shot later today. I only use RStudio and for some reason this issue was cropping up there. I'll post an update once I try it out. Thanks again.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/33#issuecomment-306947694, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGXx1sVX-xsMTAaotl-bAjWpheaBZl_ks5sByl1gaJpZM4NxoyT .

mrecos commented 7 years ago

I reproduced the error; did a clean install of devtools::install_github("rstudio/reticulate") as suggested above; ran the same code & model with no errors. So far so good! Thank you for the fix @jjallaire !

kdpsingh commented 7 years ago

I did a clean install of reticulate from github and it's fixed for me now too! Thank you @jjallaire!

castiel2304 commented 6 years ago

I am still facing the issue, can anyone please throw some light on it? I did start a new session and installed reticulate using the provided command but still facing the issue.

SonicV2 commented 6 years ago

Same here, some help please

mj2991 commented 6 years ago

Hello, I am facing the same issue with keras, I have done an install of devtools::install_github("rstudio/reticulate") as suggested by @jjallaire but the problem still persist. I am running the code below with the keras package.

str(full_data)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1309 obs. of  13 variables:
 $ survived             : num  0 1 1 1 0 0 0 0 1 1 ...
 $ pclass               : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 3 1 3 3 2 ...
 $ sex                  : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
 $ ticket               : Factor w/ 51 levels "A. 2. ","A./5. ",..: 6 23 45 21 21 21 21 21 21 21 ...
 $ cabin                : Factor w/ 9 levels "A","B","C","D",..: 8 3 8 3 8 8 5 8 8 8 ...
 $ embarked             : Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...
 $ fare_factor          : Factor w/ 5 levels "1","2","3","4",..: 1 2 1 4 1 1 3 3 3 3 ...
 $ age_interval         : Factor w/ 6 levels "1","2","3","4",..: 2 4 3 4 4 5 6 1 3 2 ...
 $ percent_survive_age  : Factor w/ 12 levels "0.0888888888888889",..: 2 11 10 11 3 5 1 7 10 8 ...
 $ title                : Factor w/ 6 levels "Lady","Miss",..: 3 4 2 4 3 3 3 5 4 4 ...
 $ percent_survive_title: Factor w/ 6 levels "0.156673114119923",..: 1 5 4 5 1 1 1 3 5 5 ...
 $ fam_size             : Factor w/ 9 levels "1","2","3","4",..: 2 2 1 2 1 1 1 5 3 2 ...
 $ fam_ID               : Factor w/ 176 levels "Abbott_3","Abelson_2",..: 19 40 109 61 109 109 109 130 90 123 ...
 - attr(*, "vars")= chr "fam_ID"
 - attr(*, "labels")='data.frame':  228 obs. of  1 variable:
  ..$ fam_ID: chr  "Abbott_3" "Abelson_2" "Ahlin_2" "Aks_2" ...
  ..- attr(*, "vars")= chr "fam_ID"
  ..- attr(*, "drop")= logi TRUE
 - attr(*, "indices")=List of 228
  ..$ : int  279 746 1283
  ..$ : int  308 874
  ..$ : int 40
  ..$ : int  855 1198
  ..$ : int  297 305 498 1197
  ..$ : int 192
  ..$ : int  13 68 119 541 542 610 813 850 1105
  ..$ : int 275
  ..$ : int  518 1081
  ..$ : int 571
  ..$ : int  49 353
  ..$ : int  25 182 233 261 1045 1065 1270
  ..$ : int  700 1093
  ..$ : int 206
  ..$ : int 85
  ..$ : int  448 469 644 858
  ..$ : int  362 702
  ..$ : int  118 299
  ..$ : int  543 546
  ..$ : int  183 618 1069 1217
  ..$ : int  248 871
  ..$ : int  291 484
  ..$ : int  140 852 971
  ..$ : int  188 593 657
  ..$ : int 356
  ..$ : int  0 477
  ..$ : int  670 684 1066 1247
  ..$ : int  728 1166
  ..$ : int  78 323 898
  ..$ : int  578 1257
  ..$ : int  679 1234
  ..$ : int  249 854
  ..$ : int  390 435 763 802
  ..$ : int  741 987
  ..$ : int  92 905
  ..$ : int  724 809
  ..$ : int  594 1010
  ..$ : int 166
  ..$ : int  580 1132
  ..$ : int  73 1006
  ..$ : int  1143 1163
  ..$ : int  426 1219
  ..$ : int  237 637 801
  ..$ : int  835 1070 1072
  ..$ : int 968
  ..$ : int  348 489 940
  ..$ : int  160 1016
  ..$ : int  540 745 1196
  ..$ : int  1 1125
  ..$ : int  423 616 1092
  ..$ : int 671
  ..$ : int 983
  ..$ : int  549 565 900 1078 1221
  ..$ : int  347 949
  ..$ : int  559 1151
  ..$ : int  93 788 923 1245
  ..$ : int  361 906
  ..$ : int  690 781
  ..$ : int  445 1184 1265
  ..$ : int  98 651
  ..$ : int  544 1130
  ..$ : int 1075
  ..$ : int  416 1085 1138
  ..$ : int  556 599
  ..$ : int  866 1111
  ..$ : int  981 1063
  ..$ : int 1041
  ..$ : int  352 532 1228
  ..$ : int 496
  ..$ : int  53 1168
  ..$ : int  86 147 436 736 1058
  ..$ : int  27 88 341 438 944 960
  ..$ : int  334 1295
  ..$ : int 660
  ..$ : int  587 1288
  ..$ : int 539
  ..$ : int  3 137
  ..$ : int  405 1292
  ..$ : int  1259 1293
  ..$ : int  861 1261
  ..$ : int  453 849
  ..$ : int  165 328 548
  ..$ : int  59 71 386 480 678 683 1030 1031
  ..$ : int  268 332
  ..$ : int  97 1241
  ..$ : int  104 392
  ..$ : int  451 490
  ..$ : int  142 403
  ..$ : int  247 755
  ..$ : int  704 1200
  ..$ : int 860
  ..$ : int  370 1255
  ..$ : int  52 645 720 848
  ..$ : int  62 230
  ..$ : int  314 440 535
  ..$ : int  820 1199
  ..$ : int  615 754 1244 1276
  ..$ : int  120 655 665
  ..$ : int 1129
  .. [list output truncated]
 - attr(*, "drop")= logi TRUE
 - attr(*, "group_sizes")= int  3 2 1 2 4 1 9 1 2 1 ...
 - attr(*, "biggest_group_size")= int 790
> full_data_dummy <- as.matrix(createDummyFeatures(full_data[1:891,]))
> str(full_data_dummy)
 num [1:891, 1:290] 0 1 1 1 0 0 0 0 1 1 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:891] "1" "2" "3" "4" ...
  ..$ : chr [1:290] "survived" "pclass.1" "pclass.2" "pclass.3" ...
> 
> ## Using keras for deeplearning classification
> 
> library(keras)
> ## defining a network with 2 hidden layers with 100 units, using a relu activation in order to initally overfit model
> model <- keras_model_sequential() %>% 
+   layer_dense(units = 16, activation = "relu", input_shape = c(289)) %>% 
+   layer_dense(units = 16, activation = "relu") %>% 
+   layer_dense(units = 1, activation = "sigmoid")
> 
> ## spliting train data into train and validation, in order to monitor how much model overfits the train data
> val_indices <- 1:300
> x_val <-  full_data_dummy[val_indices,-1]
> x_partial <- full_data_dummy[-val_indices,-1]
> y_val <- full_data_dummy[val_indices,1]
> y_partial <- full_data_dummy[-val_indices,1]
> 
> ## compiling model using binary crossenthropy and metrics accuracy
> model %>% compile(
+   optimizer = "rmsprop", 
+   loss = "binary_crossentropy", 
+   metrics = c("accuracy"))
> 
> ## fitting model on training dataset
> history <- model %>% fit(
+   x_partial, y_partial, epochs = 20, batch_size = 50, validation_data = c(x_val,y_val))
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ChristelSwift commented 5 years ago

same here, fresh install of keras / tensorflow / reticulate in R studio with devtools and getting "Error in py_call_impl(callable, dots$args, dots$keywords) : UnboundLocalError: local variable 'a' referenced before assignment"

chris23452 commented 5 years ago

I get exaclty the same error: Fehler in py_call_impl(callable, dots$args, dots$keywords): AttributeError: 'NoneType' object has no attribute 'write' Using keras application_inception_v3(weights = "imagenet", include_top = FALSE)

I tryed a fresh R session. Then I got at my first attempt: Error: The h5py Python package is required to use pre-built Keras models

Without any change the second attemp produced the py_call_impl() Error again. I tryed an new installation of h5py, which did not solve the problem.

> reticulate::py_config()
python:         C:\Anaconda3\envs\r-reticulate\python.exe
libpython:      C:/Anaconda3/envs/r-reticulate/python36.dll
pythonhome:     C:\ANACON~1\envs\R-RETI~1
version:        3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 14:00:49) [MSC v.1915 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\ANACON~1\envs\R-RETI~1\lib\site-packages\numpy
numpy_version:  1.17.0
tensorflow:     C:\ANACON~1\envs\R-RETI~1\lib\site-packages\tensorflow\__init__.p

python versions found: 
 C:\Anaconda3\envs\r-reticulate\python.exe
 C:\ANACON~1\python.exe
 C:\Anaconda3\python.exe
 C:\Anaconda3\envs\tf-keras\python.exe
>sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 14393)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

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

other attached packages:
[1] keras_2.2.4.1   reticulate_1.13 TinnRcom_1.0.21 formatR_1.5     svSocket_0.9-57

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        lattice_0.20-38   zeallot_0.1.0     grid_3.6.1        R6_2.4.0          jsonlite_1.6      magrittr_1.5      tfruns_1.4        whisker_0.3-2     svMisc_1.1.0      Matrix_1.2-17     generics_0.0.2   
[13] tools_3.6.1       compiler_3.6.1    base64enc_0.1-3   tensorflow_1.14.0 tcltk_3.6.1 
maaxnaax commented 5 years ago

This is the only Keras Rstudio that has ever worked for me: https://irudnyts.github.io/custom-set-up-of-keras-and-tensorflow-for-r-and-python/

It sorted out all attribute problems

Eghosaobasuyi-cyber commented 4 years ago

Did anyone finally get a solution for this issue?

jishupu05 commented 4 years ago

data <- matrix(runif(1000*100), nrow = 1000, ncol = 100) data<-scale(data) data labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1) labels<-scale(labels) labels model <- keras_model_sequential()

add layers and compile the model

model %>% layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% layer_dense(units = 1, activation = 'sigmoid') %>% compile( optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = c('accuracy') ) hist<-model %>% fit(data, labels, epochs=20, batch_size=32) model %>% predict(data[1:10,]) %>% round()

**hi @jjallaire I found the solution. Actually Mr kdpSingh did not use feature scaling in this code. I have run his code in my system , I found also same problem. I tried to rave out this problem by using feature scaling, as we take stochastic gradient decent as optimizer, we must need to use feature scaling. It works out finally. However we may use minimax or standardization scaling. By default here I have use scale() function to do so.

I hope it will be helpful for everyone thanks

jishupu05 commented 4 years ago

Hi all, I'm running the code below in R using the keras package.

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

model <- keras_model_sequential()
# add layers and compile the model
model %>% 
  layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% 
  layer_dense(units = 1, activation = 'sigmoid') %>% 
  compile(
    optimizer = 'rmsprop',
    loss = 'binary_crossentropy',
    metrics = c('accuracy')
  )

# Generate dummy data
data <- matrix(runif(1000*100), nrow = 1000, ncol = 100)
labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1)

# Train the model, iterating on the data in batches of 32 samples
model %>% fit(data, labels, epochs=20, batch_size=32)

# output predictions for first 10 rows
model %>% predict(data[1:10,]) %>% round()

# session info
devtools::session_info()

When I get to the line fitting the model (model %>% fit(data, labels, epochs=20, batch_size=32)), my script produces the following error:

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

Detailed traceback: 
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\models.py", line 835, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1494, in fit
    initial_epoch=initial_epoch)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\engine\training.py", line 1144, in _fit_loop
    callbacks.on_batch_end(batch_index, batch_logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callbacks.py", line 131, in on_batch_end
    callback.on_batch_end(batch, logs)
  File "C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow\contrib\keras\python\keras\callback

Strangely, when I compile/knit this as a report, everything works fine the FIRST time but then the error recurs. Any ideas what could be causing this?

Here's the output from py_config, tf_config, and session_info.

> reticulate::py_config()
python:         C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
libpython:      C:/Users/kdpsingh/AppData/Local/conda/conda/envs/py35/python35.dll
pythonhome:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35
version:        3.5.3 |Anaconda 4.4.0 (64-bit)| (default, May 15 2017, 10:43:23) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\numpy
numpy_version:  1.12.1
tensorflow:     C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow

python versions found: 
 C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe
 C:\PROGRA~3\ANACON~1\python.exe

> tf_config()
TensorFlow v1.1.0 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\lib\site-packages\tensorflow)
Python v3.5 (C:\Users\kdpsingh\AppData\Local\conda\conda\envs\py35\python.exe)

> devtools::session_info()
Session info ---------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.2 (2016-10-31)
 system   x86_64, mingw32             
 ui       RStudio (1.0.136)           
 language (EN)                        
 collate  English_United States.1252  
 tz       America/New_York            
 date     2017-06-06                  

Packages -------------------------------------------------------------------------------------
 package    * version date       source                             
 backports    1.0.4   2016-10-24 CRAN (R 3.3.2)                     
 devtools     1.12.0  2016-06-24 CRAN (R 3.3.3)                     
 digest       0.6.10  2016-08-02 CRAN (R 3.3.2)                     
 evaluate     0.10    2016-10-11 CRAN (R 3.3.2)                     
 htmltools    0.3.5   2016-03-21 CRAN (R 3.3.2)                     
 jsonlite     1.5     2017-06-01 CRAN (R 3.3.3)                     
 keras      * 0.3.6   2017-06-05 Github (rstudio/keras@023d109)     
 knitr        1.15.1  2016-11-22 CRAN (R 3.3.2)                     
 magrittr     1.5     2014-11-22 CRAN (R 3.3.2)                     
 memoise      1.0.0   2016-01-29 CRAN (R 3.3.3)                     
 R6           2.2.1   2017-05-10 CRAN (R 3.3.3)                     
 Rcpp         0.12.7  2016-09-05 CRAN (R 3.3.2)                     
 reticulate   0.8     2017-05-22 CRAN (R 3.3.3)                     
 rmarkdown    1.3     2016-12-21 CRAN (R 3.3.2)                     
 rprojroot    1.1     2016-10-29 CRAN (R 3.3.2)                     
 rstudioapi   0.6     2016-06-27 CRAN (R 3.3.3)                     
 stringi      1.1.2   2016-10-01 CRAN (R 3.3.2)                     
 stringr      1.1.0   2016-08-19 CRAN (R 3.3.2)                     
 tensorflow   0.8.2   2017-06-05 Github (rstudio/tensorflow@bace720)
 withr        1.0.2   2016-06-20 CRAN (R 3.3.3)  

data <- matrix(runif(1000*100), nrow = 1000, ncol = 100) data<-scale(data) data labels <- matrix(round(runif(1000, min = 0, max = 1)), nrow = 1000, ncol = 1) labels<-scale(labels) labels model <- keras_model_sequential()

add layers and compile the model

model %>% layer_dense(units = 32, activation = 'relu', input_shape = c(100)) %>% layer_dense(units = 1, activation = 'sigmoid') %>% compile( optimizer = 'rmsprop', loss = 'binary_crossentropy', metrics = c('accuracy') ) hist<-model %>% fit(data, labels, epochs=20, batch_size=32) model %>% predict(data[1:10,]) %>% round()

``

monitarb commented 4 years ago

Hope this helps! I had the same error, but in my case it was a multi-classification problem, so my loss function was: categorical_crossentropy. Don't undestand why, but problem was solved by changing my loss function to: sparse_categorical_crossentropy

jishupu05 commented 4 years ago

Did you work on any CNN problem? I think your data matrix was a sparse matrix.

On Tue, Oct 27, 2020, 10:11 Monica Ramirez notifications@github.com wrote:

Hope this helps! I had the same error, but in my case it was a multi-classification problem, so my loss function was: categorical_crossentropy. Don't undestand why, but problem was solved by changing my loss function to: sparse_categorical_crossentropy

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/33#issuecomment-716976557, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMHIGBCXOADI4GVJQNC3PZLSMZFQLANCNFSM4DOGRSJQ .

ghost commented 4 years ago

I am having the same problem. my code and error follow:

library(reticulate) nx<-import("networkx") py_config() G1 <- nx$cycle_graph(6L) G2 <- nx$wheel_graph(7L) nx$graph_edit_distance(G1,G2)

Error in py_call_impl(callable, dots$args, dots$keywords)

Anyone has any suggestions?

Nesh45 commented 1 year ago

Hello guys i came up with a solution to the error message here you will have to convert all the factor variable whether the target variable or the predictors to numeric using one-hot-coding then rebuild the model it will run prefectly

Nesh45 commented 1 year ago

image

Nesh45 commented 1 year ago

the results to my model