Install Keras and Tensorflow for R - r

I would like to install Keras and Tensorflow for R and I don't know why but I can't. I got a Miniconda installation though.
Even if I try to use my Miniconda path I still have the same error when I try to install Keras or Tensorflow I get the same error.
Here's my code:
use_condaenv("miniconda3", required = TRUE)
install.packages('keras')
install.packages('tensorflow')
library(keras)
library(tensorflow)
install_keras()
install_tensorflow()
The error comes with install_keras() (or install_tensorflow(), whatever the first row is):
Error: installation of 'python=3.7' into environment '.../r-miniconda/envs/r-reticulate' failed [error code 1]
But this is not my miniconda3 path. I'm using this path for r-reticulate in order to generate R Markdown documents including both R and Python code.
Is there a simple way to install Keras and Tensorflow for R?
Thanks so much

First, we need to make sure we have DevTools installed so we can import the necessary packages, then install Keras.
install.packages("devtools")
devtools::install_github("rstudio/keras")

Related

Is there a way not to install tensorflow or keras each time we restart RStudio?

When I want to use keras and tensorflow in R, I use the following codes in order to be able to install them:
library(keras)
library(tensorflow)
library(reticulate)
and it is performing without any problem. The next step is to use the following codes:
path_to_python <- "C:/Users/.../AppData/Local/Programs/Python/Python310"
virtualenv_create("r-reticulate", python = path_to_python)
install_tensorflow(envname = "r-reticulate")
install_keras(envname = "r-reticulate")
Each time I install keras and tensorflow using install_keras() and install_tensorflow() functions, I have to re-install them after closing RStudio. I there a way to just load the packages into RStudio Environment?
You only have to install each time because you overwrite your python env each time you run this line:
virtualenv_create("r-reticulate", python = path_to_python)
This overwrites your env as the function name suggests!
instead use:
reticulate::use_python( python = path_to_python)
reticulate::use_virtualenv("r-reticulate")
to reenter the environment.

Error encountered with installing Tensorflow within R on linux

I am try to install tensorflow within R and subsequently a package called "cellassign". I have installed tensorflow, reticulate, and keras with the following command and they all seem to be installed without any hiccups. I am also able to load the packages after installing.
devtools::install_github("rstudio/reticulate")
devtools::install_github("rstudio/tensorflow")
devtools::install_github("rstudio/keras")
However, I keep encountering the following issue when trying to use any command within tensorflow or keras package.
tensorflow::install_tensorflow()
Error: 'activate' is not a conda command.
Did you mean "source activate /home/users/ntu/ling0086/.conda/envs/r_env" ?
+ . /app/anaconda/3/bin/activate
+ conda activate '/home/users/ntu/ling0086/.conda/envs/r_env'
Also ran into another error if I tried using the following
> tensorflow::tf_config()
Python environments searched for 'tensorflow' package:
/home/users/ntu/ling0086/.conda/envs/r_env/bin/python3.10
/usr/bin/python
Python exception encountered:
ModuleNotFoundError: No module named 'six'
Any idea why this might be so? I am not familiar with linux-environment variables and I do not have sudo privilege. I was told that my "R" is loaded in a virtual environment and this might affect the path finding to the right packages. But I am unsure as to how I should rectify the above error. Any help would be deeply appreciated.
Not sure if this is of any help, but how I would normally run R entails:
module load anaconda/3
source activate r_env
R

Trouble installing libpng in R error: Cannot find such file

I currently have a conda environment created as "r_env" and is trying to install the "tensorflow" package into R on terminal. However, in R I keep having the same error, regardless if I am install "tensorflow" or "png" package directly from terminal after loading R.
I currently have the following packages installed in the conda env:
It appears to me that the installer is trying to look for the library files in ./conda/envs/r_env/lib/R/library whilst it is already installed in ./conda/envs/r_env. How may I rectify this issue? (Note: I do not have sudo privileges, but any solutions at this point is helpful!)
Use this code to install tensorFlow:
library(keras)
install_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)
After that try installing png:
install.packages("png")
library(png)
Finally check png:
capabilities()["png"]
Output:
png
TRUE

How to install package keras in R

I'm trying to install deep learning package keras on RStudio using this website. I installed keras using
install.packages("keras")
library(keras)
install_keras()
but when I tried to open the MNIST dataset
mnist <- dataset_mnist()
I keep getting the error
Error: ModuleNotFoundError: No module named 'absl'
I thought keras installed tensorflow but do I need to install tensorflow separately?
I had the same problem and it is solved by installing the package in two steps:
install keras: install.packages("keras")
keras::install_keras()
There you go!
If you follow the TUT and still got error, try running py_config() and check the python and libpython if it is pointing to an r-tensorflow environment. If not, best to try manually install keras in your manually set up conda environment.
Step 1: Install keras in your R just like in the link above.
#Open rstudio and run the following command
devtools::install_github("rstudio/keras")
#Don't close rstudio after running this, okay?
Step 2: Manually install keras (and tensorflow) in your machine ##. When i say “manual” it means using python specifically through conda. Here’s the link I followed: https://medium.com/i-want-to-be-the-very-best/installing-keras-tensorflow-using-anaconda-for-machine-learning-44ab28ff39cb .
In summary, the link will teach you to install anaconda, create an environment and install necessary libraries. Just follow it. I named my environment as “r-tensorflow” because that is the name of the environment that the install_keras() in R will do :)
Step 3: Point rstudio to use the python in your newly created environment using use_python() function
Open your rstudio (if you close it after following step 1) and type the following code
library(keras)
library(reticulate)
# in case you run into error run this : reticulate::py_discover_config("keras")
use_python("<yourpath>/Anaconda3/envs/r-tensorflow/Scripts/python.exe")
# change <yourpath> approriately
# write all the codes for building model in keras (or tensorflow) e.g. mnist<-dataset_mnist()
Important note on Step 3: If you still got the "not found module" after following step 3, you must start a new fresh R session and ensure to delete the workspace (.RData) because more likely your current script will still use the old python configuration though you used use_python
I had the same problem, but mine was solved by enclosing keras in double quotes.
install.packages("keras") ## worked for me,
install.packages(keras) ## never worked.
try:
install.packages("devtools")
devtools::install_github("rstudio/keras")
library(keras)
mnist<-dataset_mnist()
Please install "reticulate" library using command install.packages("reticulate") and then load using library(reticulate)
then install absl using command
conda_install('r-tensorflow','absl-py')

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