R: How to get a Long short term memory (LSTM) neural network running on Windows 10: installation guide and an example program in R - PLC-Programmer/R GitHub Wiki
keywords: R, Windows 10, python, miniconda, tensorflow, keras
Wikipedia: what is a LSTM? --> Long short-term memory
- the host CPU must support AVX2 instructions. So, a very old CPU cannot run tensorflow because trying to load the native TensorFlow runtime will fail. At this point you are required to look for another piece of hardware.
- an existing python installation or environment respectively, no matter in what form, doesn't hurt. So leave it as it is and do not try to get it running from your R installation
- the idea here is that the needed python environment will be installed (efficiently) while executing the R code (here R version 4.X.X)
- so, execute the R code (from the "LSTM test.R" program) until you come to this line:
model <- keras_model_sequential()
When above R code line has been entered (to be executed), R probably cannot find the path to a suitable python environment and throws out these messages:
No non-system installation of Python could be found.
Would you like to download and install Miniconda?
...
Would you like to install Miniconda? [Y/n]:
Now answer "Y".
After a hopefully successful installation of Miniconda, a corresponding environment with name "r-reticulate" should exist in this Windows directory:
C:\Users<user>\AppData\Local\r-miniconda\envs\r-reticulate
Most probably the "tensorflow" package is still missing from the python environment with name "r-reticulate".
So, you now leave your R environment for a while and turn to this python environment. You do this for example by doing a Windows search for "miniconda" and open the App "Anaconda Prompt (Miniconda 3)".
Now you activate the "r-reticulate" environment:
> conda activate r-reticulate
If this doesn't work, then try with the absolute path:
> conda activate C:\Users<user>\AppData\Local\r-miniconda\envs\r-reticulate
Now you install tensorflow:
(r-reticulate) C:\Users<user>> pip install tensorflow
A note: tensorflow might not like the latest python version!!
- at the moment Python 3.7.7 is OK
At this point you can make some very simple tests for tensorflow in this python environment:
(r-reticulate) C:\Users<user>> python
>>> import tensorflow as tf
=> this import command must not fail!
A reason for failure could be a missing Visual C++ Redistributable on your Window host.
Installing "Visual C++ Redistributable 2019, 64Bit" will fix this problem. So, install on your Window host:
https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
Now try again:
>>> import tensorflow as tf
(do not worry here for a potential error message like this: "Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found". This only works on a host with a GPU. If not available, tensorflow will use the CPU.)
>>> import tensorflow.compat.v1 as tfc
>>> sess = tfc.InteractiveSession()
>>> my_tensor1 = tfc.random_uniform((4, 4), 0, 1)
>>> print(my_tensor1)
>>> my_tensor2 = tfc.constant('Hello World')
>>> print(my_tensor2)
Now exit python (quit()) and return to the miniconda prompt to install some other packages:
(r-reticulate) C:\Users<user>> conda install libpython
(r-reticulate) C:\Users<user>> conda install m2w64-toolchain
(r-reticulate) C:\Users<user>> conda install pip install --upgrade keras
At last return to python and test Keras:
>>> from keras import backend
>>> dir(backend)
(this dir command should result in a meaningful answer)
>>> quit()
At last deactivate the current python environment: (r-reticulate) C:\Users<user>> conda deactivate
Now you can close this miniconda prompt.
Whenever you run R code which makes calls to the "r-reticulate" python environment, it happens automatically (from now on).
Or in other words: from now on you can concentrate on the R code. There's no need to open miniconda again and manually activate the "r-reticulate" python environment or operations like this.
Now return to your R environment and run again above command and after that just go on with the remaining "LSTM test.R" program:
model <- keras_model_sequential()
=> it should all sail smoothly on:
__end