rstudio / tfprobability

R interface to TensorFlow Probability
https://rstudio.github.io/tfprobability/
Other
54 stars 16 forks source link

version on tensorflow for "tfprobability" package #151

Closed medamineskou closed 2 years ago

medamineskou commented 2 years ago

Hello,

I've been wiling to try the tfprobability package in R and I keep getting a weird error after trying this line of code : tfd_binomial(total_count = 7, probs = 0.3) Error in py_get_attr_impl(x, name, silent): ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.7; Detected an installation of version 2.6.2. Please upgrade TensorFlow to proceed.

I've tried all the old version of the package, and update also my tensorflow version to 2.7.0.9000 even if I don't think this is the problem because the 2.6.1 has just been release 2 months ago !

Am I missing something? Thank you for your help !

t-kalinowski commented 2 years ago

Hi, you need to upgrade the underlying tensorflow version. Can you first run

tensorflow::install_tensorflow(version = "2.7")

and then

tfprobability::install_tfprobability()
medamineskou commented 2 years ago

Hello,

Thank you for your respond. I keep getting the same error :

Error in py_get_attr_impl(x, name, silent): ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.7; Detected an installation of version 2.6.2. Please upgrade TensorFlow to proceed.

Detailed traceback:
  File "/miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow_probability/python/internal/lazy_loader.py", line 53, in __getattr__
    module = self._load()
  File "/miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow_probability/python/internal/lazy_loader.py", line 37, in _load
    self._on_first_access()
  File "/miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow_probability/python/__init__.py", line 64, in _validate_tf_environment
    present=tf.__version__))

Traceback:

1. tfprobability::tfd_binomial(total_count = 7, probs = 0.3)
2. do.call(tfp$distributions$Binomial, args)
3. tfp$distributions$Binomial
4. `$.python.builtin.module`(tfp$distributions, "Binomial")
5. `$.python.builtin.object`(x, name)
6. py_get_attr_or_item(x, name, TRUE)
7. py_get_attr(x, name)
8. py_get_attr_impl(x, name, silent)

I cheked my tensorflow version with sessionInfo() and It's _tensorflow2.7.0.9000

Thanks you !

t-kalinowski commented 2 years ago

what is the output from tensorflow::install_tensorflow(version = "2.7") ?

medamineskou commented 2 years ago

Installation complete.

t-kalinowski commented 2 years ago

That's odd, I would expect multiple lines of output, ranging from ~10 to 100+. Do you know if perhaps something is capturing stdout or stderr in your terminal?

anirban-mukherjee commented 2 years ago

Suggest you check if you can import and use TensorFlow Probability and TensorFlow in a Python session. I suggest you run the Python session using repl_python in reticulate. In that Python session, see if you can run the following code:

import tensorflow_probability as tfp
tfd = tfp.distributions
n = 10000
p = 0.3
binomial_dist = tfd.Binomial(total_count=n, probs=p)

If the above runs then the problem is in the tensorflow or tfprobability R package or in the settings or some such. If it does not work then I would check if the Python that reticulate is using (you will see the path at the start of the session) is the one that you want it to use. Perhaps you have multiple versions/installs of Python and R is picking up a version/install that has both an older TensorFlow and a newer TensorFlow Probability module and you are updating and making changes in a different version/install.

Otherwise it is odd that you are getting that error where it says that it "Detected an installation of version 2.6.2.". Thats a very specific version and one can only presume that somewhere 2.6.2 is installed and that where it is installed, that install is the one that is being picked up by reticulate/Python in R.

To change the Python that is used by Reticulate, you can use (please adapt the path as needed).

Sys.setenv(RETICULATE_PYTHON = "/opt/conda/bin/python3")

anirban-mukherjee commented 2 years ago

PS: you need to set RETICULATE_PYTHON at the top of the script before loading reticulate. Either that or use use_python to force reticulate to use a specific Python install.

medamineskou commented 2 years ago

Thank you,

Apparently, I've installed many python version like you said .. I have tried this without installing tensorflow :

library(reticulate)
use_condaenv("r-reticulate", required = TRUE)
install.packages("tfprobability")
library(tfprobability)
tfprobability::install_tfprobability()

tensorflow::tf_config() And I'm getting this output : _

TensorFlow v2.6.2 (/miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow_probability) Python v3.7 (/miniconda/envs/r-reticulate/bin/python)

_ If I try to update my tensorflow version with tensorflow::install_tensorflow(version="2.7.0"), the tensorflow version doesn't change !

Now, I've tried to install Tensorflow first after restarting my session :

library(reticulate)
use_condaenv("r-reticulate", required = TRUE)
library(tensorflow)
tensorflow::install_tensorflow(version="2.7.0")

And I'm getting this output : tensorflow::tf_config()

TensorFlow v2.7.0 (/miniconda/envs/r-reticulate/lib/python3.7/site-packages/tensorflow) Python v3.7 (/miniconda/envs/r-reticulate/bin/python)

But now, I'm getting a big error when trying to import tfprobability

install.packages("tfprobability")
library(tfprobability)

Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified)

Error: package or namespace load failed for ‘tfprobability’: .onLoad failed in loadNamespace() for 'tfprobability', details: call: py_module_import(module, convert = convert) error: ModuleNotFoundError: No module named 'tensorflow_probability'

Detailed traceback: File "/usr/local/lib/R/site-library/reticulate/python/rpytools/loader.py", line 44, in _import_hook level=level

Traceback:

  1. library(tfprobability)
  2. tryCatch({ . attr(package, "LibPath") <- which.lib.loc . ns <- loadNamespace(package, lib.loc) . env <- attachNamespace(ns, pos = pos, deps, exclude, include.only) . }, error = function(e) { . P <- if (!is.null(cc <- conditionCall(e))) . paste(" in", deparse(cc)[1L]) . else "" . msg <- gettextf("package or namespace load failed for %s%s:\n %s", . sQuote(package), P, conditionMessage(e)) . if (logical.return) . message(paste("Error:", msg), domain = NA) . else stop(msg, call. = FALSE, domain = NA) . })
  3. tryCatchList(expr, classes, parentenv, handlers)
  4. tryCatchOne(expr, names, parentenv, handlers[[1L]])
  5. value[3L]
  6. stop(msg, call. = FALSE, domain = NA)

Ps: I'm working on a server (Kaggle notebook)

t-kalinowski commented 2 years ago

Please try the development version of tfprobability. I pushed some minor changes to install_tfprobability recently that may help here.

remotes::install_github("rstudio/reticulate")
remotes::install_github("rstudio/tensorflow")
remotes::install_github("rstudio/tfprobability")
tfprobability::install_tfprobability()
medamineskou commented 2 years ago

Thank you, problem solved !