Installing Keras/Tensor Flow on R - r

I am trying to install tensorflow on R. As far as I understand I have already installed successlly many times but it won't load
Everytime I run a function from Tensorflow and Keras it does not work.
> testlabels=to_categorical(trainy)
Error: Python module tensorflow.python.keras was not found.
Detected Python configuration:
python: C:\Users\AUSTER~1\ANACON~1\python.exe
libpython: C:/Users/AUSTER~1/ANACON~1/python37.dll
pythonhome: C:\Users\AUSTER~1\ANACON~1
version: 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\AUSTER~1\ANACON~1\lib\site-packages\numpy
numpy_version: 1.16.4
tensorflow: [NOT FOUND]
When I load the libraries I have no issues. They all load up sucessfully.
library(EBImage)
library(keras)
library(tensorflow)
I have ran all these following commands.
install.packages("keras")
source("http://bioconductor.org/biocLite.R")
biocLite("EBImage")
install.packages("reticulate")
install.packages("remotes")
remotes::install_github("rstudio/tensorflow")
reticulate::py_discover_config("keras")
reticulate::py_discover_config("tensorflow")
install.packages("devtools")
devtools::install_github("rstudio/keras")
tensorflow::install_tensorflow()
devtools::install_github("rstudio/keras")
What could be causing the issue. After I installed tensforflow it said "Installation Complete" and then restarted R. Is R simply unable to find the installation?
I have also ran the following commands still with no luck.
library(tensorflow)
Sys.setenv(TENSORFLOW_PYTHON="/usr/local/bin/python")
Update
I was able to get Tensorflow/Keras to work by opening an Anaconda command prompt and running the following commands. Then my R code using Keras/Tensorflow started to work.
conda install -c conda-forge keras

The keras package makes use of keras through python. When you install the package, it installs the R interface, but not the python package. You must separately install the python package. There is a nice guide for doing that on RBloggers

I checked all of the suggestions. None of them worked.
This worked for me on Windows 10
1- Open rstudio and uninstall these package if you already installed using:
uninstall.packages(c("keras", "tensorflow","tfruns"))
2- uninstall rtools from your computer if exists
3- Install Anaconda from https://www.anaconda.com/products/individual
4- Install the last version of rtools from https://cran.r-project.org/bin/windows/Rtools/
5- Close rstudio if it is opened
6- open new session and write this:
install.packages("reticulate")
install.packages("remotes")
library("reticulate")
remotes::install_github("rstudio/tensorflow")
library(tensorflow)
install_tensorflow(version = "2.0.0b1", method = "conda", envname = "r-reticulate")
remotes::install_github("rstudio/keras", dependencies = TRUE)
library(keras)
7- check if they work:
#if tensorflow works
tf$abs(-1)
#if keras works
mnist <- dataset_mnist()

my suggestion is to install anaconda and create an environment called "r-reticulate".
you can do it using anaconda navigator or
reticulate::conda_create(envname = "r-reticulate")
then to check that env detected by reticulate, use reticulate::conda_python().it must return directory of python.exe for your env.
after that you can install tensorflow by install_tensorflow(). [not working in my case]
so I install the tesnorflow from CMD.
follow these steps:
open the cmd :]
activate the r-reticulate env using conda activate r-reticulate (you may need your directory to conda directory if you did not add conda to your PATH)
use : conda install -c anaconda tensorflow
now in R, you can use TensorFlow.
for installing Keras, you can use pip install Keras. [I tried install_keras() function after the installation of tensorflow, but it n

Related

Tensorflow in r not found

I have installed tensorflow in r with this code install_tensorflow(conda_python_version = '3.8').
When calling tf$constant('Hellow Tensorflow'), I get this error:
Error: Installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
C:\Users\User\.conda\envs\r-reticulate\python.exe
C:\ProgramData\Anaconda3\python.exe
You can install TensorFlow using the install_tensorflow() function.
However, I have the python.exe in the first location and have the library in C:\Users\User\.conda\envs\r-reticulate\Lib\site-packages\tensorflow\. Why the tensorflow installation could not be found?
I have solved this by typing conda activate r-reticulate to anaconda powershell while having the tensorflow installed in the r-reticulate environment.

Error: Python module tensorflow was not found. Rstudio, Windows10 - PATH problem

I am trying to install TensorFlow on my RStudio for Windows10 following the official instructions found in the website https://tensorflow.rstudio.com/installation/
and with the requisite found in the official TensorFlow webs
https://www.tensorflow.org/install?hl=en
I found this is still an unsolved problem considering that nobody could really help in the previous questions like this for example (R, TensorFlow, Anaconda Install on Windows)
So I followed many videos on youtube and read a lot but still nothing. When I run:
library(remotes)
remotes::install_github("rstudio/tensorflow")
library(tensorflow)
install_tensorflow()
tf_config()
Installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
C:\Users\Simone\anaconda3\envs\r-reticulate\python.exe
C:\Users\Simone\anaconda3\python.exe
You can install TensorFlow using the install_tensorflow() function.
I tried this after tried the way suggested in the official page
install.packages("tensorflow")
library(tensorflow)
install_tensorflow()
library(tensorflow)
tf$constant("Hellow Tensorflow")
but still, same thing. I think it's a PATH problem but I don't know how to solve it.
Consider that when I run:
py_config() #then i get this
python: C:/Users/Simone/anaconda3/envs/r-reticulate/python.exe
libpython: C:/Users/Simone/anaconda3/envs/r-reticulate/python36.dll
pythonhome: C:/Users/Simone/anaconda3/envs/r-reticulate
version: 3.6.12 |Anaconda, Inc.| (default, Sep 9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/Simone/anaconda3/envs/r-reticulate/Lib/site-packages/numpy
numpy_version: 1.19.4
tensorflow: C:\Users\Simone\ANACON~2\envs\R-RETI~1\lib\site-packages\tensorflow\__init__.p
python versions found:
C:/Users/Simone/anaconda3/envs/r-reticulate/python.exe
C:/Users/Simone/anaconda3/python.exe
TensorFlow seems to be installed in this PATH C:\Users\Simone\ANACON~2\envs\R-RETI~1\lib\site-packages\tensorflow_init_.p but manually checking this PATH it doesn't appear. Seems like python is installed in one path, and tensorflow in another one
Any idea how I could fix the path problem?
Thanks!
EDIT1:
I also noticed this warning message from install_tensorflow()
WARNING: The script tensorboard.exe is installed in 'C:\Users\Simo\ANACON~2\envs\R-RETI~1\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
so that would be the problem I should solve, but don't know how
I have met the same problem of intalling tensorflow in Rstudio, but I found a way to avoid it by installing tensorflow in a new anaconda environment, then activate the envirnoment inside Rstudio. It works in my computer (win 11, R4.2.1).
install anaconda
create a new tensorflow environment named tf-gpu, open anaconda prompt and run:
conda create -n tf-gpu tensorflow-gpu
conda activate tf-gpu
You can check the environment in anaconda prompt by:
conda env list
open Rstudio and run:
library("tensorflow")
use_condaenv("tf-gpu", required = TRUE)
tf$constant("Hello Tensorflow!")

Can't install tensorflow in R, my machine is 64 bit but R doesn't detect it

I'm using windows, my problem is that while trying to install tensorflow in R the following error message appears.
Error in install_tensorflow(method = method, conda = conda, version = tensorflow, :
Unable to install TensorFlow on this platform.Binary installation is only available for 64-bit platforms.
My machine is 64 bit, so I don't understand where is the problem.
I've already cheked if my anaconda installation and the python version are 64bit and they are.
I can install tensorflow in python without any error, but i can't in R.
I've tried to redirect the R tensorflow installation to the python environment but i don't know if it can really help.
> library(tensorflow)
> install_tensorflow()
Error in install_tensorflow() :
Unable to install TensorFlow on this platform.Binary installation is only available for 64-bit platforms.
Can you try to install older version of tensorflow which is 1.5.0 using command pip install tensorflow==1.5.0

R, TensorFlow, Anaconda Install on Windows

I'm tryiing to install tensorflow for R. For this purpose I installed the latest version of Anaconda (4.3.1) to my OS and installed everything as described in:
https://rstudio.github.io/tensorflow/installation.html
To get started, install the tensorflow R package from GitHub as follows:
devtools::install_github("rstudio/tensorflow")
Then, use the install_tensorflow() function to install TensorFlow:
library(tensorflow)
install_tensorflow()
The result of this process is a folder ~\AppData\Local\conda\conda\envs\r-tensorflow which should be used by Anaconda as an Evironment. This environment is not recognized by the current version of Anaconda.
But the output in R is:
Error: Python module tensorflow was not found.
Detected Python configuration:
python: C:\PROGRA~3\ANACON~1\python.exe
libpython: C:/PROGRA~3/ANACON~1/python36.dll
pythonhome: C:\PROGRA~3\ANACON~1
version: 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\PROGRA~3\ANACON~1\lib\site-packages\numpy
numpy_version: 1.11.3
tensorflow: [NOT FOUND]
No comes my guessing:
Anaconda 4.3.1 does use the folder C:\ProgramData\Anaconda3\pkgs for its packages and does not use the folder above at all. This might be the cause why tensorflow is not found.
The described process from https://rstudio.github.io/tensorflow/installation.html does not match the latest version of anaconda.
Can you show me how to get tensorflow to work with the latest versions of R, tensorflow and Anaconda?
I tried to install r-tensorflow to the global Anaconda environment via
conda install -c conda-forge r-tensorflow
It is not recognized by the environment loaded in R. Can I change the environment of tensorflow directly in R?
Ok, I found out that in the case of tensorflow and Anaconda used I need to force Anaconda to use the Tensorflow environment before i can use it. This is not included in the R tensorflow library so far and must be set by the reticulate library:
#set anaconda to tensor flow environment
library(reticulate)
use_condaenv("r-tensorflow")
#Alternative
use_condaenv(condaenv = "r-tensorflow", conda = "YOUR_ANACONDA_PATH")
I posted an issue here:
https://github.com/rstudio/tensorflow/issues/119
My suggestion is to install using the Anaconda Navigator
step 1:
Open Anaconda Navigator and change the Application on from "Root to Tensorflow" channel (top left)
Step 2:
Now install the R available below

How to install Tensorflow for R

I had Tensorflow installed with Anaconda. Now I want use it in R and I need to reinstall Tensorflow, because the note here
NOTE: You should NOT install TensorFlow with Anaconda as there are
issues with the way Anaconda builds the python shared library that
prevent dynamic linking from R.
I already tried to uninstall from Anaconda and install with pip but its came to the same place in anaconda directory. Tesorflow is working from terminal but in R shows Error: Command failed (1)
Anybody can help me to how I can solve the problem? Should I uninstall anaconda and install Tensorflow using pip?
You have several options on what to do. Probably the cleanest one is to install a system-wide python (if not installed yet) and then create a virtual environment. This basically takes your system python binaries and moves them to its own compartment where everythign is isolated from the rest, incl. anaconda. Once you are inside an activated virtual environment you can install all the necessary Python appendages for TensorFlow. Once that is done, make sure you set up a correct environmental PATH for TensorFlow from where R can reach it:
Sys.setenv(TENSORFLOW_PYTHON="/path/to/virtualenv/python/binary")
devtools::install_github("rstudio/tensorflow")
Example of the path to where you installed the virtual environment project would be, I think, something like ~/minion/medvedi/venv_medvedi/bin/python.
This is no longer an issue, the documentation was updated too.
See here:
https://github.com/rstudio/tensorflow/commit/4e1e11d6ba2fe7efe1a03356f96172dbf8db365e
With the help of Keras, we can install the TensorFlow package in R.
install_keras()
library(keras)
devtools::install_github("rstudio/keras")
install_tensorflow(package_url = "https://pypi.python.org/packages/b8/d6/af3d52dd52150ec4a6ceb7788bfeb2f62ecb6aa2d1172211c4db39b349a2/tensorflow-1.3.0rc0-cp27-cp27mu-manylinux1_x86_64.whl#md5=1cf77a2360ae2e38dd3578618eacc03b")
library(tensorflow)
Keras is a high-level neural network API for deep learning from TensorFlow Google.
my suggestion is to install anaconda and create an environment called "r-reticulate".
you can do it using anaconda navigator or
reticulate::conda_create(envname = "r-reticulate")
then to check that env detected by reticulate, use reticulate::conda_python().it must return directory of python.exe for your env.
after that you can install tensorflow by install_tensorflow(). [not working in my case]
so I install the tesnorflow from CMD.
follow these steps:
open the cmd :]
activate the r-reticulate env using conda activate r-reticulate (you may need your directory to conda directory if you did not add conda to your PATH)
use : conda install -c anaconda tensorflow
now in R, you can use TensorFlow.
for installing Keras, you can use pip install Keras. [i tried install_keras() function after the installation of tensorflow, but it ruined my TensorFlow installation also]
Eventually I found the best and fast method to do it in R:
devtools::install_github("rstudio/keras")
library(keras)
install_keras(method = "conda")
install_keras(tensorflow = "gpu")
tensorflow::install_tensorflow()

Resources