Reputation: 69
I am trying to get tensorflow up and running in R but several attempts have failed. First my specs: Computer: MacBook Pro M1 Max OS: MacOS Ventura 13.1 R version: version 4.2.2 (2022-10-31) [arch: aarch64] with RStudio 2022.12.0 Build 353
I followed initially these guidelines: https://tensorflow.rstudio.com/install/ and https://tensorflow.rstudio.com/install/local_gpu
In R I then run the following in a new session:
library(reticulate)
virtualenv_create("r-reticulate")
library(tensorflow)
tf$constant("Hello Tensorflow!")
I receive the following output:
> library(reticulate)
> virtualenv_create("r-reticulate")
virtualenv: r-reticulate
> library(tensorflow)
> tf$constant("Hello Tensorflow!")
Error: Valid installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
/Users/daniel/Library/r-miniconda-arm64/envs/r-reticulate/bin/python3.8
Python exception encountered:
Traceback (most recent call last):
File "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library/reticulate/python/rpytools/loader.py", line 39, in _import_hook
module = _import(
File "/Users/daniel/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow/__init__.py", line 443, in <module>
_ll.load_library(_plugin_dir)
File "/Users/daniel/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow/python/framework/load_library.py", line 151, in load_library
py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: dlopen(/Users/daniel/Library/r-miniconda-arm64/envs/r-reticulate/lib/python3.8/site-packages/tensorflow-plugins/libmetal_plugin.dylib, 0x0006): symbol not found in
When I type 'python3 --version' in the Terminal, it returns: Python 3.10.9 and when I type 'type -a python3' it gives me the following paths:
python3 is /opt/homebrew/bin/python3
python3 is /Library/Frameworks/Python.framework/Versions/3.11/bin/python3
python3 is /usr/local/bin/python3
python3 is /usr/bin/python3
This looked a bit odd to me to have these different paths, but I don't know because I haven't worked with Python installations before; also note that the initial Error Message returns this path (/Users/daniel/Library/r-miniconda-arm64/envs/r-reticulate/bin/python3.8), which is not listed
In additional trials, I specified the virtual environment path to any of these python paths, such as:
virtualenv_create("r-reticulate", python = "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3")
The Error message is the same though.
I also tried the following solutions to reinstall and specify differently.
https://gist.github.com/juliasilge/035d54c59436604d6142ebebf29aa224
In the global menu --> python: I have selected '/usr/local/bin/python3.11'. When I try to install tensorflow for Python it returns the following:
py_install('tensorflow') Using virtual environment '~/.virtualenvs/r-reticulate' ...
Thanks for any pointers you can provide!
After reinstalling miniconda and tensorflow dependencies for metal (following apple's developer instructions). I get the following: tf$constant("Hello Tensorflow!") List of 20 $ python : chr "/Users/daniel/miniconda3/bin/python3.10" $ libpython : chr "/Users/daniel/miniconda3/lib/libpython3.10.dylib" $ pythonhome : chr "/Users/daniel/miniconda3:/Users/daniel/miniconda3" $ pythonpath : chr "/Users/daniel/miniconda3/lib/python310.zip:/Users/daniel/miniconda3/lib/python3.10:/Users/daniel/miniconda3/lib"| truncated $ prefix : chr "/Users/daniel/miniconda3" $ exec_prefix : chr "/Users/daniel/miniconda3" $ base_exec_prefix : chr "/Users/daniel/miniconda3" $ virtualenv : chr "" $ virtualenv_activate : chr "" $ version_string : chr "3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:25:13) [Clang 14.0.6 ]" $ version : chr "3.10" $ architecture : chr "64bit" $ anaconda : logi FALSE $ conda : logi TRUE $ numpy :List of 2 ..$ path : chr "/Users/daniel/miniconda3/lib/python3.10/site-packages/numpy" ..$ version:Class 'numeric_version' hidden list of 1 .. ..$ : int [1:3] 1 22 3 $ required_module : chr "tensorflow" $ required_module_path: chr "/Users/daniel/miniconda3/lib/python3.10/site-packages/tensorflow" $ available : logi TRUE $ python_versions : chr "/Users/daniel/miniconda3/bin/python3.10" $ forced : chr "use_python function"
Detected Python configuration:
Upvotes: 3
Views: 1391
Reputation: 898
I got it working for tensorflow 2.8.0, doing the following steps:
conda create --name rstudio-tf-2.8
conda activate rstudio-tf-2.8
conda install python=3.8
conda install -c apple tensorflow-deps==2.8.0
python -m pip install tensorflow-macos==2.8.0
python -m pip install tensorflow-metal==0.4.0
conda install -c anaconda tensorflow==2.8.0
echo "RETICULATE_PYTHON=~/miniforge3/envs/rstudio-tf-2.8/bin/python" >> ~/.Renviron
conda list | grep tensorflow
It should look like this:
➜ ~ conda list | grep tensorflow
tensorflow 2.8.0 cpu_py39h2839aeb_0 conda-forge
tensorflow-base 2.8.0 cpu_py39hcf13c84_0 conda-forge
tensorflow-deps 2.8.0 0 apple
tensorflow-estimator 2.8.0 cpu_py39hfad2b03_0 conda-forge
tensorflow-macos 2.8.0 pypi_0 pypi
tensorflow-metal 0.4.0 pypi_0 pypi
Session -> Restart R
reticulate::use_python("~/miniforge3/envs/rstudio-tf-2.8/bin/python")
reticulate::use_condaenv("rstudio-tf-2.8", conda="~/miniforge3/bin/conda", required = TRUE)
library(tensorflow)
tf_config()
tf_version()
tf$config$list_logical_devices()
tf_config()
crashes go back to Terminal and upgrade to python 3.9 conda install python=3.9
conda list | grep tensorflow
, for me tensorflow-macos and metal were missing after the python upgrade, I had to repeat step 8 and 9I don't know how to avoid the crash, if I install from the beginning python 3.9 the crash still happens and get fixed by downgrading to python 3.8. My guess is come dependency issue, which I couldn't figure out.
Upvotes: 1