I am using R4.0.1 and Rstudio1.3.959 on windows 10. I have installed tensor flow:
install.packages("tensorflow")
library(tensorflow)
install_tensorflow(method = "conda", conda_python_version = 3.6)
I checked the installation success by:
library(tensorflow)
tf$constant("Hellow Tensorflow")
Output: Tensor("Const:0", shape=(), dtype=string)
tf$constant(1.5)
Output: Tensor("Const_1:0", shape=(), dtype=float32)
I further checked by:
tf_config()
Output:
TensorFlow v1.13.2 ()
Python v3.6 (C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe)
From here it seems the tensor flow is installed properly and is working fine.
However, I faced problems in loading Keras library. I did the following:
install.packages("keras")
library(keras)
As I load the library, it gives the following error
Error: package or namespace load failed for ‘keras’:
.onLoad failed in loadNamespace() for 'keras', details:
call: py_module_import(module, convert = convert)
error: ImportError: cannot import name 'swish'
Detailed traceback:
File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\__init__.py", line 14, in <module>
from . import activations
File "C:\Users\user\AppData\Local\r-miniconda\envs\r-reticulate\lib\site-packages\tensorflow\keras\activations\__init__.py", line 23, in <module>
from tensorflow.python.keras.activations import swish
Since the keras package could not be loaded I cannot run the following code
install_keras(method = "conda")
I could get additional information as below:
library(reticulate)
> py_discover_config("keras")
python: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python.exe
libpython: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/python36.dll
pythonhome: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate
version: 3.6.10 |Anaconda, Inc.| (default, May 7 2020, 19:46:08) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/user/AppData/Local/r-miniconda/envs/r-reticulate/Lib/site-packages/numpy
numpy_version: 1.18.1
I could also see that default python version in use as:
Sys.which("python")
python
"C:\\Users\\user\\AppData\\Local\\R-MINI~1\\envs\\R-RETI~1\\python.exe"
I would be grateful if anyone can solve this installation issue. Thanks
I am a Windows 10 User and I encountered the same issue today. After checking multiple posts, the below steps worked for me.
Update R to the latest 4.1.2 version. R Studio might say that R is up to date (which happened to me today), but it may not be.
install.packages("installr")
library(installr)
updateR()
Update Rcpp, jsonlite, and curl packages even if they claim to be up to date.
install.packages("Rcpp")
install.packages("jsonlite")
install.packages("curl")
Start a Fresh R session.
Run the below commands to install, reticulate, tensorflow, and keras from github:
devtools::install_github("rstudio/reticulate")
devtools::install_github("rstudio/tensorflow")
devtools::install_github("rstudio/keras")
Run the below commands (Some of them may be redundant if preceding ones include the functionalities. Entering them here anyway as it could help.).
library(keras)
install_keras()
library(reticulate)
library(tensorflow)
Test TensorFlow installation with the command below:
tf$constant("Hello World!")
If you get the below output, you're there. There could be some text before the output (ignore them).
tf.Tensor(b'Hello World!', shape=(), dtype=string)
When running a DNN or RNN, after installing the packages, calling just library(keras) shoudl be adequate.
Have fun!
This might be a bit different but I find it easier to hand manage dependent env. So you can open conda prompt and execute:
conda create -n env_name python=3.6 tensorflow
Then in R before you do anything call
library(keras)
library(tensorflow)
use_condaenv(condaenv = "env_name",required = T)
By default you wil get TF 2.xx, you can specify that in conda env.
EDIT: For TF gpu you need to specify conda create -n env_name python=3.6 tensorflow-gpu and you will get CUDa and CUDNN if you have GPU on your PC.
Related
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!")
I have seen this question answered for issues in Windows but haven't seen any solution for a Mac.
After installing keras like:
devtools::install_github("rstudio/keras")
library(keras)
install_keras()
I get the error when running:
library(keras)
mnist <- dataset_mnist()
>> Error in initialize_python(required_module, use_environment) :
Python shared library not found, Python bindings not loaded.
I have tried the following:
use_python("/Users/me/anaconda3/bin/python3.7", required = TRUE)
use_virtualenv("~/MyFolder/.venv/")
use_python("~/MyFolder/.venv/bin/python")
But haven't been successful, I saw some suggestions about wrong python installation but it shouldn't be it in my case.
i have installed python 3.7.5 with Anaconda
=> python --version
Python 3.7.5
I'm on macOS Catalina Version 10.15.1 in RStudio, Version 1.2.5019. Does anyone have any solution?
I had same problem with OSX. Found solution by configuring python virtual environment to use python3
library(reticulate)
use_python("/usr/local/bin/python3")
This is a old thread, but just wanted to say for anyone that finds this that installing the python-dev package for whatever version of python you're using may work. I was using python3.8, so I installed python3.8-dev
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
I've been following the guide here: https://blogs.rstudio.com/tensorflow/posts/2017-12-14-image-classification-on-small-datasets/ about how to set up CNNs in Keras, and my code fails at this line:
history <- model %>% fit_generator(
train_generator,
steps_per_epoch = 100,
epochs = 30,
validation_data = validation_generator,
validation_steps = 50
)
with the following error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ImportError: Could not import PIL.Image. The use of array_to_img requires PIL.
Here are the results of reticulate::py_config():
python: C:\Users\Trent\Anaconda3\envs\r-tensorflow\python.exe
libpython: C:/Users/Trent/Anaconda3/envs/r-tensorflow/python36.dll
pythonhome: C:\Users\Trent\ANACON~2\envs\R-TENS~1
version: 3.6.8 |Anaconda, Inc.| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\Trent\ANACON~2\envs\R-TENS~1\lib\site-packages\numpy
numpy_version: 1.16.2
keras: C:\Users\Trent\ANACON~2\envs\R-TENS~1\lib\site-packages\keras\__init__.p
python versions found:
C:\Users\Trent\Anaconda3\envs\r-tensorflow\python.exe
C:\Users\Trent\ANACON~2\python.exe
C:\Users\Trent\Anaconda3\python.exe
C:\Users\Trent\Anaconda3\envs\pillow\python.exe
I've never written any python, so I'm not exactly sure how to solve this problem using the backend methods I've seen described on stackOverflow in the past, but I did try.
*I tried following the advice here:
StopIteration: Could not import PIL.Image. The use of `array_to_img` requires PIL error and opened up the anaconda prompt and typed in the following lines:
conda activate r-tensorflow
Then the screen crashes after some text pops on screen (it's too fast to read). In order to avoid this I tried to just do the following:
conda install r-tensorflow pillow
Then it says # All requested packages already installed.
I've updated R and all of its packages.
I've also uninstalled all of anaconda and reinstalled the newest version.
I've also rerun install_keras() with and without the GPU option to try and get it to work (I couldn't get the GPU part to work for the h5py error so I stopped looking at that one since online it says that the CPU version should be easier to work with)
The thread here: https://github.com/rstudio/keras/issues/216 suggests that I should open the anaconda prompt and type in the following:
conda install -c conda-forge pillow
I've modified this to include my environment since I couldn't activate it to read as the following:
conda install -c r-tensorflow conda-forge pillow
but I couldn't get it to read what I meant.
Lots of things say to use pip install pillow but since I don't know how to activate the right environment I'm really not sure how to do that. I've tried
conda install r-tensorflow pillow
with no success so I'm not exactly sure how to do it with pip but would be happy to test it with someone's help.
history <- model %>% fit_generator(
train_generator,
steps_per_epoch = 100,
epochs = 30,
validation_data = validation_generator,
validation_steps = 50
)
I expected the training to begin after I ran the above line but unfortunately I just get the error.
I would be happy to provide more detail as it is needed and thank you all so much for your help in advance.
I'm venturing into using Reticulate in R and having trouble installing a package, specifically psycopg2 but I've also tried installing twisted with the same result.
after I load reticulate in R I double check to make sure my package is installed:
> conda_install(envname = "r-reticulate", packages="psycopg2")
Solving environment: ...working... done
# All requested packages already installed.
Looks good. So I set my condaenv to r-reticulate just to be double sure. The docs say I should not have to do this, but it should not hurt:
> use_condaenv( "r-reticulate")
Then I try to import psycopg2:
> psycopg2 <- import('psycopg2')
Error in py_module_import(module, convert = convert) :
ImportError: No module named psycopg2
If I install twisted the same way, I get the same error:
> twisted <- import('twisted')
Error in py_module_import(module, convert = convert) :
ImportError: No module named twisted
So, what am I doing wrong here?
Below is the output of conda_list and py_config... I was expecting py_config to show me the path to the python exe in my r-reticulate environment. However I don't have anything to compare this to, so my expectations may have been wrong.
> conda_list()
name python
1 Anaconda C:\\Users\\jal\\AppData\\Local\\Continuum\\Anaconda\\python.exe
2 r-reticulate C:\\Users\\jal\\AppData\\Local\\Continuum\\Anaconda\\envs\\r-reticulate\\python.exe
> py_config()
python: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
libpython: C:/Users/jal/AppData/Local/CONTIN~1/Anaconda/python27.dll
pythonhome: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda
version: 2.7.15 |Anaconda custom (64-bit)| (default, May 1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\lib\site-packages\numpy
numpy_version: 1.14.3
pandas: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\lib\site-packages\pandas
python versions found:
C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\ANACON~1\python.exe
On a whim I downloaded and installed the latest RStudio desktop (1.2.701) and upgraded from 1.1.442. This seems to have solved my issue. The packages load and I now see that when I run py_config I see paths that reflect my environment choice, as I would expect.
> py_config()
python: C:\Users\jal\AppData\Local\Continuum\Anaconda\envs\r-reticulate\python.exe
libpython: C:/Users/jal/AppData/Local/Continuum/Anaconda/envs/r-reticulate/python36.dll
pythonhome: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1
version: 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1\lib\site-packages\numpy
numpy_version: 1.14.3
psycopg2: C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\envs\R-RETI~1\lib\site-packages\psycopg2
python versions found:
C:\Users\jal\AppData\Local\Continuum\Anaconda\envs\r-reticulate\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\Anaconda\python.exe
C:\Users\jal\AppData\Local\CONTIN~1\ANACON~1\python.exe
I solved my issue by following this way in RStudio:
py_config()
No non-system installation of Python could be found.
Would you like to download and install Miniconda?
Miniconda is an open source environment management system for Python.
See https://docs.conda.io/en/latest/miniconda.html for more details.
Would you like to install Miniconda? [Y/n]: Y
Downloading "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe" ...
trying URL 'https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe'
Content type 'application/octet-stream' length 57256056 bytes (54.6 MB)
downloaded 54.6 MB
Installing Miniconda -- please wait a moment ...
I solved my issue by following this way:
Close RStudio.
Open Terminal and activate your desired environment.
Run RStudio by $ open -na Rstudio
Now you have your anac