rstudio / tensorflow

TensorFlow for R
https://tensorflow.rstudio.com
Apache License 2.0
1.33k stars 321 forks source link

Installation Issue #582

Closed Asromer-biology closed 1 year ago

Asromer-biology commented 1 year ago

I've been trying to get tensorflow and keras to work in Rstudio on my system for the past several days but despite my best efforts I haven't been able to do so. I have been able to download the packages, create virtual enviroments, install packages into them via the windows command line, but still have not been able to get models to run successfully. Any assistance would be greatly appreciated.

Please provide the output of demo code for tensorflow from rstudio as well as the suggested diagnosistic outputs.

> library(reticulate)
> library(tensorflow)
> library(keras)
> library(tidyverse)
> 
> c(c(x_train, y_train), c(x_test, y_test)) %<-% keras::dataset_mnist()
> x_train <- x_train / 255
> x_test <-  x_test / 255
> 
> model <- keras_model_sequential(input_shape = c(28, 28)) %>%
+   layer_flatten() %>%
+   layer_dense(128, activation = "relu") %>%
+   layer_dropout(0.2) %>%
+   layer_dense(10)
> 
> predictions <- predict(model, x_train[1:2, , ])
1/1 [==============================] - 0s 47ms/step
> 
> predictions
            [,1]      [,2]       [,3]        [,4]      [,5]       [,6]      [,7]      [,8]       [,9]      [,10]
[1,]  0.01118694 0.2813529 0.57456416  0.08346558 0.3401761 -0.7518407 0.6589657 0.1194443 -0.4665765  0.1722614
[2,] -0.25449932 0.6129925 0.05429347 -0.41096535 0.8672591 -1.1021731 0.9212947 0.1462670 -0.1371493 -0.3343928
> 
> tf$nn$softmax(predictions)
tf.Tensor(
[[0.08461587 0.11086219 0.14863581 0.09095824 0.11757907 0.03945237
  0.16172553 0.09429039 0.05247613 0.0994044 ]
 [0.06298045 0.14995215 0.08576553 0.0538584  0.19336563 0.02698148
  0.20410172 0.09402782 0.07082233 0.05814448]], shape=(2, 10), dtype=float64)
> 
> loss_fn <- loss_sparse_categorical_crossentropy(from_logits = TRUE)
> 
> loss_fn(y_train[1:2], predictions)
tf.Tensor(2.998795959859347, shape=(), dtype=float64)
> 
> model %>% compile(
+   optimizer = "adam",
+   loss = loss_fn,
+   metrics = "accuracy"
+ )
Error in py_call_impl(callable, call_args$unnamed, call_args$named) : TypeError: __init__() missing 1 required positional argument: 'args' Run `reticulate::py_last_error()` for details.
> # If the above installation failed, please gather some diagnostic info > reticulate::py_config() python: C:/Users/alexr/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe libpython: C:/Users/alexr/AppData/Local/r-reticulate/r-reticulate/pyenv/pyenv-win/versions/3.9.13/python39.dll pythonhome: C:/Users/alexr/Documents/.virtualenvs/r-tensorflow version: 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] Architecture: 64bit numpy: C:/Users/alexr/Documents/.virtualenvs/r-tensorflow/Lib/site-packages/numpy numpy_version: 1.24.3 tensorflow: C:\Users\alexr\DOCUME~1\VIRTUA~1\R-TENS~1\lib\site-packages\tensorflow\__init__.p NOTE: Python version was forced by import("tensorflow") > tensorflow::tf_config() TensorFlow v2.13.0 (C:\Users\alexr\DOCUME~1\VIRTUA~1\R-TENS~1\lib\site-packages\tensorflow\__init__.p) Python v3.9 (~/.virtualenvs/r-tensorflow/Scripts/python.exe) > reticulate::py_last_error() ── Python Exception Message ──────────────────────────────────────────────────────────────────────────────────────────────────────── TypeError: 'SparseCategoricalCrossentropy' object is not subscriptable ── R Traceback ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1. └─.rs.environment.isSuspendable() 2. ├─base::tryCatch(...) 3. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers) 4. │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]]) 5. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler) 6. └─.rs.environment.isSuspendableImpl(globalenv(), 1L) 7. └─.rs.environment.isSuspendableImpl(value[[key]], depth + 1L) 8. ├─.rs.environment.isSuspendableImpl(value[[i]], depth + 1L) 9. ├─value[[i]] 10. └─reticulate:::`[[.python.builtin.object`(value, i) 11. └─reticulate:::py_get_attr_or_item(x, name, FALSE) 12. └─reticulate::py_get_item(x, name) 13. └─reticulate:::py_get_item_impl(x, key, silent) > sessionInfo() R version 4.3.0 (2023-04-21 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19045) Matrix products: default locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C LC_TIME=English_United States.utf8 time zone: America/Chicago tzcode source: internal attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 dplyr_1.1.2 purrr_1.0.1 [6] readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.2 tidyverse_2.0.0 [11] keras_2.13.0.9000 tensorflow_2.13.0.9000 reticulate_1.32.0.9000 loaded via a namespace (and not attached): [1] Matrix_1.5-4 gtable_0.3.3 jsonlite_1.8.7 compiler_4.3.0 tidyselect_1.2.0 Rcpp_1.0.11 zeallot_0.1.0 [8] tfruns_1.5.1 scales_1.2.1 png_0.1-8 lattice_0.21-8 here_1.0.1 R6_2.5.1 generics_0.1.3 [15] munsell_0.5.0 rprojroot_2.0.3 tzdb_0.3.0 pillar_1.9.0 rlang_1.1.1 utf8_1.2.3 stringi_1.7.12 [22] timechange_0.2.0 cli_3.6.1 withr_2.5.0 magrittr_2.0.3 grid_4.3.0 rstudioapi_0.15.0 hms_1.1.3 [29] remotes_2.4.2.1 base64enc_0.1-3 lifecycle_1.0.3 vctrs_0.6.3 glue_1.6.2 whisker_0.4.1 fansi_1.0.4 [36] colorspace_2.1-0 tools_4.3.0 pkgconfig_2.0.3
`
t-kalinowski commented 1 year ago

Thank you for the bug report, I could reproduce. This was a bug in the development version of reticulate, which should be now fixed. Can you please install the latest development version and try again?

pak::pak("rstudio/reticulate", ask = FALSE)
Asromer-biology commented 1 year ago

Thank you for such a prompt reply!

Thank you for the bug report, I could reproduce. This was a bug in the development version of reticulate, which should be now fixed. Can you please install the latest development version and try again?

pak::pak("rstudio/reticulate", ask = FALSE)

I tried installing using the pak package but got the following error

> pak::pak("rstudio/reticulate", ask = FALSE)
Error in load_private_package("glue") : 
  Cannot load glue from the private library
t-kalinowski commented 1 year ago

Installation of R packages can fail on Windows if the package is already loaded. This is especially tricky for packages like reticulate, or glue, since the package namespace might be loaded by the IDE.

Make sure you've quit any other sessions of Rstudio / R, and try to again. You can also try using remotes::install_github("rstudio/reticulate"), if this is a {pak} specific bug.

Asromer-biology commented 1 year ago

That did the trick!

> model %>% compile(
+   optimizer = "adam",
+   loss = loss_fn,
+   metrics = "accuracy"
+ )
> 
> model %>% fit(x_train, y_train, epochs = 5)
Epoch 1/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0660 - accuracy: 0.9792
Epoch 2/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0602 - accuracy: 0.9798
Epoch 3/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0535 - accuracy: 0.9826
Epoch 4/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0484 - accuracy: 0.9842
Epoch 5/5
1875/1875 [==============================] - 5s 3ms/step - loss: 0.0442 - accuracy: 0.9852 

Diagnositic info for future reference

> reticulate::py_config()
python:         C:/Users/alexr/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe
libpython:      C:/Users/alexr/AppData/Local/r-reticulate/r-reticulate/pyenv/pyenv-win/versions/3.9.13/python39.dll
pythonhome:     C:/Users/alexr/Documents/.virtualenvs/r-tensorflow
version:        3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/alexr/Documents/.virtualenvs/r-tensorflow/Lib/site-packages/numpy
numpy_version:  1.24.3
tensorflow:     C:\Users\alexr\DOCUME~1\VIRTUA~1\R-TENS~1\lib\site-packages\tensorflow\__init__.p

NOTE: Python version was forced by import("tensorflow")
> tensorflow::tf_config()
TensorFlow v2.13.0 (C:\Users\alexr\DOCUME~1\VIRTUA~1\R-TENS~1\lib\site-packages\tensorflow\__init__.p)
Python v3.9 (~/.virtualenvs/r-tensorflow/Scripts/python.exe)

Thank you greatly!