Error encountered with installing Tensorflow within R on linux - r

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

Related

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")

Installing R 4.0.2 version

I used to work in R 3.4.0 version. Hovewer, this version doesn't support such packages as keras and tensorflow.
I was adviced to upgrade my R version to the newest one.
I downloaded the most recent R version 4.0.2 from the official site, then ran the following code:
install.packages("keras")
library(keras)
install_keras()
And got the following error:
Error in install_keras() :
You should call install_keras() only in a fresh R session that has not yet initialized Keras and TensorFlow (this is to avoid DLL in use errors during installation)
After this, when I tried to quit R session by q() , I faced the following error:
Error: option error has NULL value
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Error: option error has NULL value
I've never faced such an error before. When I used old R version, I typed q() and then had to choose between y and n. No errors appeared.
I'm asking you to help to to solve this problem.
You need to create a new environment and then you can install R 4.+ in Anaconda. Follow these steps.
conda create --name r4-base
After activating r4-base run these commands
conda activate r4-base
conda install -c conda-forge r-base
conda install -c conda-forge/label/gcc7 r-base
Finally, you will notice r-basa version 4 will be installed.
Thereafter, you can install any supported packages. But with this only, you won't have the ability to use it in the Jupyter notebook. You need to install install.packages('IRkernel') and Jupyter notebook as well if you want to use it. Otherwise you are good to go with R-Studio.
For Jupyter Installation and RKernel.
conda install jupyter
Then open the R console. Write in R console
install.packages('IRkernel')
IRkernel::installspec()
Congrats! You can use Notebook for Python and R.
Find the location of R.exe on your computer. In my computer, this executable is at
C:\Program Files\R\R-3.4.3\bin
Open another Anaconda Prompt as Administrator and change directories to wherever R.exe is on your computer with cd file path. On my computer, it’s cd C:\Program Files\R\R-3.4.3\bin, but it might be different for you.
Then run R from within Anaconda Prompt in Admin mode with R.exe
You’ll notice that you’re in an R session. From here, run the following three commands into the terminal.
install.packages("devtools")
devtools::install_github("IRkernel/IRkernel")
IRkernel::installspec()
In order, they (1) install the devtools package which gets you the install_github() function, (2) install the IR Kernel from GitHub, and (3) tell Jupyter where to find the IR Kernel.
Open Jupyter notebook and enjoy your new R kernel!
Get more information here
#Rheatey Bash works perfectly. but i was facing python.exe this program cant start because api-ms-win-core-path-l1-1-0.dll python system error. this is a problem running on windows 7 but i resolved this issue by installing the kernel following https://richpauloo.github.io/2018-05-16-Installing-the-R-kernel-in-Jupyter-Lab/ and it works fine

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()

Errors building R-packages for conda

I am having a tough time installing R-packages that are not available in the Anaconda repositories. My attempts so far can be found here How to install R-packages not in the conda repositories?.
Currently, I am trying to build the R-package rafalib for conda by following the instructions from this article under the heading Building a conda R package.
The first part works fine.
conda skeleton cran rafalib
Out:
Tip: install CacheControl to cache the CRAN metadata
Fetching metadata from http://cran.r-project.org/
Writing recipe for rafalib
Done
The build command runs into errors
conda build r-rafalib
Out:
Removing old build environment
Removing old work directory
BUILD START: r-rafalib-1.0.0-r3.2.2_0
Using Anaconda Cloud api site https://api.anaconda.org
Fetching package metadata: ......
Solving package specifications: .
Error: Packages missing in current linux-64 channels:
- r 3.2.2*
- r-rcolorbrewer
I have r 3.2.2-64bit installed via conda and it runs without problems. I also already have r-colorbrewer installed via conda and I can use that package without issues in R. Why am I getting these errors when trying to build a conda package?
I am on Linux (Antergos, an Arch derivative) with kernel 4.4.5-1-ARCH.
UPDATE 2015/04/19
Thanks to this answer, I found out that I could include the dependencies by building them separately in the same directory as the package I want to install. That didn't work for me, but I also read that I can include a channel in the build command with -c, just as when installing. So now I do:
conda build -c r r-rafalib
This gets passed all the dependency problems, but after fetching, extracting and linking packages, it fails. Here is the end of the error message.
Removing old work directory
Source cache directory is: /home/joel/anaconda2/conda-bld/src_cache
Downloading source to cache: rafalib_1.0.0.tar.gz
Downloading http://cran.r-project.org/src/contrib/rafalib_1.0.0.tar.gz
rafalib_1.0.0. 100% |#######################| Time: 0:00:00 4.87 MB/s
Success
Extracting download
Package: r-rafalib-1.0.0-r3.2.2_0
source tree in: /home/joel/anaconda2/conda-bld/work/rafalib
+ mv DESCRIPTION DESCRIPTION.old
+ grep -v '^Priority: ' DESCRIPTION.old
+ /home/joel/anaconda2/envs/_build/bin/R CMD INSTALL --build .
sh: symbol lookup error: sh: undefined symbol: rl_signal_event_hook
Command failed: /bin/bash -x -e /home/joel/drafts/r-rafalib/build.sh
The error sh: symbol lookup error: sh: undefined symbol: rl_signal_event_hook is the same as I encounter when using install.packages() as reported here.
There is some related discussion in this thread. I have tried to get around this error by installing different versions of ncurses, including this patched version, and I have tried to link the readline libraries, as suggested here, but I keep running into the same error. I'm quite lost at this point and any help to solve this would be greatly appreciated.
Although I started out with a different problem, the final solution turned out to be the same as I posted elsewhere How to install R-packages not in the conda repositories?. I am adding it here for completeness.
In the end, I got around the rl_event_hookproblems by following the approach recommended here and symlinking anaconda's libreadline to the system one:
mv ~/anaconda3/lib/libreadline.s.6.2 ~/anaconda3/lib/libreadline.s.6.2.bak
ln -s /usr/lib/libreadline.so.6.3 ~/anaconda3/lib/libreadline.s.6.2
I am still having troubles installing some dependency heavy R-packages due to failure to load shared objects when using install.packages() from withing R. However, simpler packages work fine and I can get most of the dependency heavy packages from anacondas R-repositories.

Resources