Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R Warning / Python Error: module 'tensorflow' has no attribute 'reset_default_graph' #47

Closed
gsteinbu opened this issue Jul 3, 2020 · 6 comments

Comments

@gsteinbu
Copy link
Contributor

gsteinbu commented Jul 3, 2020

Using tfruns with the latest version of tensorflow produces a warning/error for me. Here a minimal example for reproduction:

library(tfruns)

# Download example
download.file(url = "https://raw.githubusercontent.com/rstudio/tfruns/master/inst/examples/mnist_mlp/mnist_mlp.R", 
              destfile = "mnist_mlp.R")

# Change number of epochs in "mnist_mlp.R" from k 20 to 5 (to make it faster)
line_idx <- 58
r_file <- readLines("mnist_mlp.R")
change_to <- "  epochs = 2," 
cat("Changing \"", r_file[line_idx], "\" to \"", change_to, "\"", sep="")
#> Changing "  epochs = 20," to "  epochs = 2,"
r_file[line_idx] <- change_to
write(r_file, file = "mnist_mlp.R")

# Run example
training_run("mnist_mlp.R", echo=FALSE)
#> Using run directory runs/2020-07-03T06-30-11Z
#> Error in score$loss: $ operator is invalid for atomic vectors
#> Warning in value[[3L]](cond): Error occurred resetting tf graph: AttributeError:
#> module 'tensorflow' has no attribute 'reset_default_graph'

Created on 2020-07-03 by the reprex package (v0.3.0)

The first error (in score$loss) is just a minor bug in the example file; it should be score["loss"] instead of score$loss (don't really like to open an issue for this). My problem is the line after that, starting with the warning. While tfruns seems to work nontheless, this warning is shown everytime I use tfruns and I am worried is has some side effects (allthough I am not sure). From reading about this issue in the web, this issue seems very related to keras-team/keras#12379

Here some session Infos for the example above (obscured some poths though):

# Summarize session
sessionInfo()
#> R version 4.0.1 (2020-06-06)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
#> [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
#> [5] LC_TIME=German_Germany.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] keras_2.3.0.0 tfruns_1.4   
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4.6         compiler_4.0.1       pillar_1.4.4        
#>  [4] highr_0.8            base64enc_0.1-3      tools_4.0.1         
#>  [7] zeallot_0.1.0        digest_0.6.25        jsonlite_1.6.1      
#> [10] evaluate_0.14        lifecycle_0.2.0      tibble_3.0.1        
#> [13] gtable_0.3.0         lattice_0.20-41      pkgconfig_2.0.3     
#> [16] rlang_0.4.6          Matrix_1.2-18        yaml_2.2.1          
#> [19] xfun_0.14            dplyr_1.0.0          stringr_1.4.0       
#> [22] knitr_1.28           vctrs_0.3.1          generics_0.0.2      
#> [25] rappdirs_0.3.1       tidyselect_1.1.0     grid_4.0.1          
#> [28] reticulate_1.16-9000 glue_1.4.1           R6_2.4.1            
#> [31] rmarkdown_2.2        purrr_0.3.4          ggplot2_3.3.1       
#> [34] magrittr_1.5         whisker_0.4          scales_1.1.1        
#> [37] htmltools_0.4.0      ellipsis_0.3.1       colorspace_1.4-1    
#> [40] tensorflow_2.2.0     config_0.3           stringi_1.4.6       
#> [43] munsell_0.5.0        crayon_1.3.4

# Summarize Python
reticulate::py_config()
#> python:         [...]/miniconda3/envs/r-tensorflow/python.exe
#> libpython:      [...]/miniconda3/envs/r-tensorflow/python36.dll
#> pythonhome:     [...]/miniconda3/envs/r-tensorflow
#> version:        3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 15:18:16) [MSC v.1916 64 bit (AMD64)]
#> Architecture:   64bit
#> numpy:          [...]/miniconda3/envs/r-tensorflow/Lib/site-packages/numpy
#> numpy_version:  1.18.5
#> tensorflow:     [...]\MINICO~1\envs\R-TENS~1\lib\site-packages\tensorflow\__init__.p
#> 
#> python versions found: 
#>  [...]/miniconda3/envs/r-tensorflow/python.exe
#>  [...]/miniconda3/python.exe

# tensorflow version
tensorflow::tf$version$VERSION
#> [1] "2.2.0"

Created on 2020-07-03 by the reprex package (v0.3.0)

@jemus42
Copy link

jemus42 commented Jul 22, 2020

Ran into this today as a first time user – from what I can tell at least the saved data seems fine, and I can analyze my runs just fine, or am I missing something?

@gsteinbu
Copy link
Contributor Author

In the meantime I looked into this issue slightly more. AFAIK the graphs in TensorFlow are used for representation of the models (e.g., in TensorBoard’s Graphs dashboard). Thus, this issue should not impact your actual calculations / training. However, since I am neither very familiar with TensorFlow, I am not sure (@dfalbel maybe has some insights here?).

I will also create a pull request for fixing this issue: the essential problem seems to be that the call of 'tf$reset_default_graph()' in R/training_run.R is not correct for TensorFlow >= 2.0 (not 100% sure from which version exactly). Using 'tf$compat$v1$reset_default_graph()' which is introduced by TensorFlow for compatibility reasons (I guess) seems to clear the issue. Since this does not seem to be the optimal way for TensorFlow >= 2.0 then, there might be a better solution.

@jemus42
Copy link

jemus42 commented Jul 24, 2020

Thanks for taking a look!

@dieison-depra
Copy link

Hi guys, I had the same problem for Rstudio 1.4.1103 (for MacOsX bigSur).
My workaround was:

library("tensorflow")
#Workflow to tensorflow 2.0 bug
tf$reset_default_graph = tf$compat$v1$reset_default_graph

@jemus42
Copy link

jemus42 commented Feb 17, 2021

Shouldn't that be fixed by now?
Or is that fix not on the CRAN version yet?

@dfalbel
Copy link
Member

dfalbel commented Feb 24, 2021

It's fixed in the dev version. Will submit it to CRAN soon.

@dfalbel dfalbel closed this as completed Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants