How to install package keras in R - 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')

Related

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

R Tensorflow installation with no user input in console

I have a basic script in R 4.1 and R Studio which installs TensorFlow 2.
When I run the library installation for Tensorflow, everything works fine with no issues. My issue is that when I call the library and run the install_tensorflow command, the console is asking me to answer a "yes/no" question.
Would you like to install Miniconda? [Y/n]:
My question: How do I answer this with a Y without having to type directly into the console? I want to just run an R Script to install and provision my conda environment without manual intervention. Programmatic User Input.
Here is my full R code:
install.packages("tensorflow")
library(tensorflow)
install_tensorflow(method="auto", conda="auto", version="2.0.0", envname ="tf_test")

Install Keras and Tensorflow for 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")

R: 'tensorflow not found' after importing tensorflow

I am trying to follow along with this tutorial on rbind.io. I thought instead of posting 30-40 lines of reproducible code, it's better to link to the tutorial.
I am trying to create a Time Series forecast using an LSTM neural network.
When following along with the code in the tutorial linked above, I was creating the Keras model from the RStudio Cloud, as my desktop RStudio was giving a different bug that could be resolved by working with the Cloud.
Moving on..
I encountered an error claiming I didn't have TensorFlow installed.
So, being the only thing to do, I installed TensorFlow.
After doing so, I encountered an error.
Now, I'm stuck. I'm not sure at all where my error is, and I request some help.
EDIT:
After trying what Ammar said, I still get an error.
EDIT 2:
Here's my reply to Ammar:
First, install the tensorflow R package from GitHub as follows:
install.packages("tensorflow")
Then, use the install_tensorflow() function to install TensorFlow as given below. Note that on Windows you need a working installation of Anaconda.
library(tensorflow)
install_tensorflow()
You can confirm that the installation succeeded with:
library(tensorflow)
tf$constant("Hellow Tensorflow")
## tf.Tensor(b'Hellow Tensorflow', shape=(), dtype=string)
As given at https://tensorflow.rstudio.com/installation/

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