I am following the 'Quick start' instructions on the website: TensorFlow for R - Quick start (copied below) and the installation fails with:
tensorflow::as_tensor("Hello World")
Error: Valid installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11
Python exception encountered:
Traceback (most recent call last):
File "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/reticulate/python/rpytools/loader.py", line 119, in _find_and_load_hook
return _run_hook(name, _hook)
^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/reticulate/python/rpytools/loader.py", line 93, in _run_hook
module = hook()
^^^^^^
File "/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/reticulate/python/rpytools/loader.py", line 117, in _hook
return find_and_load(name, import)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'tensorflow'
You can install TensorFlow using the install_tensorflow() function.
My system is MacBook M1 running the latest OS: 13.4.1 (c) (22F770820d). I did a fresh install of RStudio: Version 2023.06.1+524 (2023.06.1+524)
The full installation documentation from the website (URL above) is:
Quick start
Prior to using the tensorflow R package you need to install a version of Python and TensorFlow on your system. Below we describe how to install to do this as well the various options available for customizing your installation.
Note that this article principally covers the use of the R install_tensorflow()
function, which provides an easy to use wrapper for the various steps required to install TensorFlow. You can also choose to install TensorFlow manually (as described at TensorFlow 2 را نصب کنید). In that case the Custom Installation section covers how to arrange for the tensorflow R package to use the version you installed.
TensorFlow is tested and supported on the following 64-bit systems:
- Ubuntu 20.04 or later
- Windows 10 or later
- macOS 11 (Big Sur) or later (Intel or M1)
Installation
First, install the tensorflow R package from GitHub as follows:
install.packages("tensorflow")
Next, configure R with a Python installation it can use, like this:
library(reticulate)
path_to_python <- install_python()
virtualenv_create("r-reticulate", python = path_to_python)
Note that if you already have Python installed, you don’t need to call install_python()
and instead can just supply an absolute path to the Python executable.
Then, use the install_tensorflow()
function to install TensorFlow.
library(tensorflow)
install_tensorflow(envname = "r-reticulate")
You can also use keras::install_keras()
, which installes Tensorflow, in addition to some commonly used packages like “scipy” and “tensorflow-datasets”.
install.packages("keras")
library(keras)
install_keras(envname = "r-reticulate")
You can confirm that the installation succeeded with:
library(tensorflow)
tf$constant("Hello Tensorflow!")
Loaded Tensorflow version 2.10.0
tf.Tensor(b'Hello Tensorflow!', shape=(), dtype=string)
This will provide you with a default installation of TensorFlow suitable for use with the tensorflow R package. Read on if you want to learn about additional installation options, including installing a version of TensorFlow that takes advantage of Nvidia GPUs if you have the correct CUDA libraries installed.