This weekend, I decided it is time:i is going to update my Python environment and get Keras and TensorFlow installed So I could the start doing tutorials (particularly for deep learning) using R. Although I used to is a systems administrator (about years ago), I don ' t do much installing or configuring so I guess T Hat ' s why I ' ve put the this task off for so long. And it wasn ' t unwarranted:it took me the whole weekend to get the install working. Here is the steps I used to get things running on Windows Ten, leveraging clues in about different online resources-a nd Yes (I found out the hard), the order of operations is very important. I don't claim to has nailed the Order of operations here, but definitely one that Works.
Step 0: I had already installed the TensorFlow and Keras packages within R, and had been wondering why they wouldn ' t work. "Of course!" I-finally realized, a few weeks later. "I don ' t has python on this machine, and both of these packages depend on a python install." Turns out they also depend in the Reticulate package, so Install.packages ("reticulate") if you had not already.
Step 1: Installed Anaconda3 to C:/users/user/anaconda3 (from https://www.anaconda.com/download/)
Step 2: Opened "Anaconda Prompt" from Windows Start menu. First, to ' Create an ' environment ' specifically for use with TensorFlow and Keras in R called ' Tf-keras ' with a 64-bit vers Ion of Python 3.5 I typed:
Conda create-n Tf-keras python=3.5 Anaconda
... and then after it is done, I do this:
Activate Tf-keras
Step 3: Install TensorFlow from Anaconda prompt. Using the instructions at Https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.1.0-cp35-cp35m-win_ AMD64.WHL I Typed this:
Pip Install--ignore-installed--upgrade
I didn ' t know whether this worked or not-it gave me a error saying that it "can not import Html5lib", so I do this NEX T:
Conda install-c Conda-forge Html5lib
I tried to run the PIP command again, but there is an error so I consulted Https://www.tensorflow.org/install/install_win Dows. It told me to does this:
Pip Install--ignore-installed--upgrade tensorflow
This failed, and told me, the PIP command had an error. I searched the web for a alternative to that command, and found this, which worked!!
Conda install-c Conda-forge TensorFlow
Step 4: From inside the Anaconda prompt, I opened python by typing "python". Next, I did this, line by line:
Import TensorFlow as tf hello = tf.constant (' Hello, tensorflow! ') Sess = tf. Session () print (Sess.run (hello))
It said "B ' Hello, tensorflow! '" which I believe means it works. (Ctrl-z then Enter would then get the "out of Python" and "back" to the Anaconda prompt.) This means, my Python installation of TensorFlow was functional.
Step 5: Install Keras. I tried this:
Pip Install Keras
... but I got the same error message, PIP could not be installed or found or imported or something. So I tried this, the which seemed to work:
Conda install-c Conda-forge Keras
Step 6: Load them up from within R. First, I opened a 64-bit version of R v3.4.1 and did this:
Library (TensorFlow) install_tensorflow (conda= "Tf=keras")
It took a couple minutes but it seemed to work.
Library (Keras)
Step 7: Try a tutorial! I decided to go for https://www.analyticsvidhya.com/blog/2017/06/ getting-started-with-deep-learning-using-keras-in-r/which guides through developing a classifier for the MNIST HANDW Ritten image database-a Very popular data science resource. I loaded up my datasets and checked to make sure it loaded properly:
Data <-data_mnist ()
STR (data) List of 2 $ train:list of 2. $ x:int [1:60,000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ..... $ y:int [1:60000 (1d)] 5 0 4 1 9 2 1 3 1 4 ... $ test:list of 2.. $ x:int [1:10,000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ..... $ y:int [1:10000 (1d)] 7210414959...
Step 8: Here's the code I used to prepare the data and create the neural network model. This didn ' t take a long to run at all.
Trainx<-data$train$xtrainy<-data$train$ytestx<-data$test$xtesty<-data$test$ytrain_x <-Array ( train_x, Dim = C (Dim (train_x) [1], prod (Dim (train_x) [-1]))/255test_x <-Array (test_x, Dim = C (Dim (test_x) [1], prod (di M (test_x) [-1]))/255train_y<-to_categorical (train_y,10) test_y<-to_categorical (test_y,10) Model%>% layer _dense (units = 784, Input_shape = 784)%>% layer_dropout (rate=0.4)%>%layer_activation (activation = ' relu ')%>% l Ayer_dense (units = ten)%>% layer_activation (activation = ' Softmax ') model%>% compile (loss = ' Categorical_ Crossentropy ', optimizer = ' Adam ', metrics = C (' accuracy '))
Step 9: Train the network. This TOOK is about MINUTES in a powerful machine with 64GB high-performance RAM. It looks like it worked, but I don ' t know how to find or evaluate the results yet.
Model%>% Fit (train_x, train_y, epochs = +, batch_size = max) Loss_and_metrics <-model%>% Evaluate (test_ X, test_y, batch_size = +)
Str (model)
Model
________________________________________________________________________ ___________
Layer (type) Output Shape Param #
=============================================================== ====================
Dense_1 (dense) (None, 784) 615440
____________________________________________________ _______________________________
Dropout_1 (Dropout) (None, 784) 0
__________________________________________ _________________________________________
Activation_1 (activation) (None, 784) 0
__________________________ _________________________________________________________
dense_2 (dense) (None, ten) 7850
__________________ _________________________________________________________________
Activation_2 (activation) (None, ten) 0
= = = ================================================================================
Total params:623,290
trainable params:623,290
non-trainable params:0
Step Ten: Next, I wanted to try the tutorial at https://cran.r-project.org/web/packages/kerasR/vignettes/introduction.html. Turns out this uses the KERASR package, not the Keras package:
X_train <-mnist$x_trainy_train <-mnist$y_trainx_test <-mnist$x_testy_test <-mnist$Y_test> Dim (X_ Train) [1] 60000 28x_train <-Array (x_train, Dim = C (Dim (X_train) [1], prod (Dim (x_train) [-1]))/255x_test <-Arra Y (x_test, Dim = C (Dim (X_test) [1], prod (Dim (x_test) [-1]))/255
To check and see what ' s in any individual image, type:
Image (X_train[1,,])
At this point, the To_categorical function stopped working. I was supposed to does this and got an error:
Y_train <-to_categorical (Mnist$y_train, 10)
So I do this instead:
MM <-Model.matrix (~ y_train) y_train <-to_categorical (mm[,2]) mod <-sequential () # This was the exciting part WH ERE You use keras!! :)
But then I tried this, and it is clear I was stuck again-it wouldn ' t work:
Mod$add (Dense (units = Input_shape = Dim (X_train) [2]))
Stack Overflow recommended grabbing a version of KERASR from GitHub, so that's what I did next:
Install.packages ("Devtools") library (Devtools) Devtools::install_github ("Statsmaths/kerasr") Library (KERASR)
I got an error in R which told me to go to the Anaconda prompt (which I did), and type this:
Conda Install M2w64-toolchain
Then I went back to R and this worked fantastically:
MoD <-sequential ()
Mod$add would still not work though, and this is where my patience expired for the evening. I ' m pretty happy Though-python is up, Keras and tensorflow be up in Python, all three (Keras, TensorFlow, and Kerasr) a Re up in R, and some tutorials seem to be working.
Transferred from: https://qualityandinnovation.com/2017/10/16/a-newbies-install-of-keras-tensorflow-on-windows-10-with-r/
A newbie ' s Install of Keras & TensorFlow on Windows ten with R