<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/a-newbies-install-of-keras-tensorflow-on-windows-10-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://qualityandinnovation.com/2017/10/16/a-newbies-install-of-keras-tensorflow-on-windows-10-with-r/"> R – Quality and Innovation</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This weekend, I decided it was time: I was going to update my Python environment and get Keras and Tensorflow installed so I could start doing tutorials (particularly for deep learning) using R. Although I used to be a systems administrator (about 20 years ago), I don’t do much installing or configuring so I guess that’s why I’ve put this task off for so long. And it wasn’t unwarranted: it took me the whole weekend to get the install working. Here are the steps I used to get things running on Windows 10, leveraging clues in about 15 different online resources — and yes (I found out the hard way), the order of operations is <em>very</em> important. I do not claim to have nailed <em>the </em>order of operations here, but definitely <em>one</em> that works.</p>
<p><strong>Step 0:</strong> 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 have Python on this machine, and both of these packages depend on a Python install.” Turns out they also depend on the reticulate package, so install.packages(“reticulate”) if you have not already.</p>
<p><strong>Step 1:</strong> Installed Anaconda3 to C:/Users/User/Anaconda3 (from <a rel="nofollow" href="https://www.anaconda.com/download/" target="_blank">https://www.anaconda.com/download/</a>)</p>
<p><strong>Step 2:</strong> 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 version of Python 3.5 I typed:</p>
<pre>conda create -n tf-keras python=3.5 anaconda</pre>
<p>… and then after it was done, I did this:</p>
<pre>activate tf-keras

</pre>
<p><strong>Step 3:</strong> Install TensorFlow from Anaconda prompt. Using the instructions at <a rel="nofollow" href="https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.1.0-cp35-cp35m-win_amd64.whl" target="_blank">https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.1.0-cp35-cp35m-win_amd64.whl</a> I typed this:</p>
<pre>pip install --ignore-installed --upgrade</pre>
<p>I didn’t know whether this worked or not — it gave me an error saying that it “can not import html5lib”, so I did this next:</p>
<pre>conda install -c conda-forge html5lib</pre>
<p>I tried to run the pip command again, but there was an error so I consulted <a rel="nofollow" href="https://www.tensorflow.org/install/install_windows" target="_blank">https://www.tensorflow.org/install/install_windows</a>. It told me to do this:</p>
<pre>pip install --ignore-installed --upgrade tensorflow</pre>
<p>This failed, and told me that the pip command had an error. I searched the web for an alternative to that command, and found this, which worked!!</p>
<pre>conda install -c conda-forge tensorflow</pre>
<p> </p>
<p><strong>Step 4:</strong> From inside the Anaconda prompt, I opened python by typing “python”. Next, I did this, line by line:</p>
<pre>import tensorflow as tf
 hello = tf.constant('Hello, TensorFlow!')
 sess = tf.Session()
 print(sess.run(hello))</pre>
<p>It said <strong>“b’Hello, TensorFlow!’”</strong> which I believe means it works. (Ctrl-Z then Enter will then get you out of Python and back to the Anaconda prompt.) This means that my Python installation of TensorFlow was functional.</p>
<p><strong>Step 5:</strong> Install Keras. I tried this:</p>
<pre>pip install keras</pre>
<p>…but I got the same error message that pip could not be installed or found or imported or something. So I tried this, which seemed to work:</p>
<pre>conda install -c conda-forge keras</pre>
<p> </p>
<p><strong>Step 6:</strong> Load them up from within R. First, I opened a 64-bit version of R v3.4.1 and did this:</p>
<pre>library(tensorflow)
install_tensorflow(conda=&quot;tf=keras&quot;)</pre>
<p>It took a couple minutes but it seemed to work.</p>
<pre>library(keras)</pre>
<p> </p>
<p><strong>Step 7:</strong> Try a tutorial! I decided to go for <a rel="nofollow" href="https://www.analyticsvidhya.com/blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/" target="_blank">https://www.analyticsvidhya.com/blog/2017/06/getting-started-with-deep-learning-using-keras-in-r/</a> which guides you through developing a classifier for the MNIST handwritten image database — a very popular data science resource. I loaded up my dataset and checked to make sure it loaded properly:</p>
<pre>data &lt;- data_mnist()</pre>
<blockquote>
<pre>str(data)
List of 2
 $ train:List of 2
 ..$ x: int [1:60000, 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:10000, 1:28, 1:28] 0 0 0 0 0 0 0 0 0 0 ...
 ..$ y: int [1:10000(1d)] 7 2 1 0 4 1 4 9 5 9 ...</pre>
</blockquote>
<p> </p>
<p><strong>Step 8:</strong> Here is the code I used to prepare the data and create the neural network model. This didn’t take long to run at all.</p>
<pre>trainx&lt;-data$train$x
trainy&lt;-data$train$y
testx&lt;-data$test$x
testy&lt;-data$test$y

train_x &lt;- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255

test_x &lt;- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255

train_y&lt;-to_categorical(train_y,10)
test_y&lt;-to_categorical(test_y,10)

model %>% 
layer_dense(units = 784, input_shape = 784) %>% 
layer_dropout(rate=0.4)%>%
layer_activation(activation = 'relu') %>% 
layer_dense(units = 10) %>% 
layer_activation(activation = 'softmax')

model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)</pre>
<p> </p>
<p><strong>Step 9:</strong> Train the network. THIS TOOK ABOUT 12 MINUTES on 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.</p>
<pre>model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)
 loss_and_metrics &lt;- model %>% evaluate(test_x, test_y, batch_size = 128)</pre>
<blockquote><p>str(model)<br>
Model<br>
___________________________________________________________________________________<br>
Layer (type) Output Shape Param #<br>
===================================================================================<br>
dense_1 (Dense) (None, 784) 615440<br>
___________________________________________________________________________________<br>
dropout_1 (Dropout) (None, 784) 0<br>
___________________________________________________________________________________<br>
activation_1 (Activation) (None, 784) 0<br>
___________________________________________________________________________________<br>
dense_2 (Dense) (None, 10) 7850<br>
___________________________________________________________________________________<br>
activation_2 (Activation) (None, 10) 0<br>
===================================================================================<br>
Total params: 623,290<br>
Trainable params: 623,290<br>
Non-trainable params: 0</p>
</blockquote>
<p> </p>
<p><strong>Step 10:</strong> Next, I wanted to try the tutorial at <a rel="nofollow" href="https://cran.r-project.org/web/packages/kerasR/vignettes/introduction.html" target="_blank">https://cran.r-project.org/web/packages/kerasR/vignettes/introduction.html</a>. Turns out this uses the kerasR package, not the keras package:</p>
<pre>X_train &lt;- mnist$X_train
Y_train &lt;- mnist$Y_train
X_test &lt;- mnist$X_test
Y_test &lt;- mnist$Y_test

> dim(X_train)
[1] 60000 28 28

X_train &lt;- array(X_train, dim = c(dim(X_train)[1], prod(dim(X_train)[-1]))) / 255
X_test &lt;- array(X_test, dim = c(dim(X_test)[1], prod(dim(X_test)[-1]))) / 255</pre>
<p>To check and see what’s in any individual image, type:</p>
<pre>image(X_train[1,,])</pre>
<p>At this point, the to_categorical function stopped working. I was supposed to do this but got an error:</p>
<pre>Y_train &lt;- to_categorical(mnist$Y_train, 10)</pre>
<p>So I did this instead:</p>
<pre>mm &lt;- model.matrix(~ Y_train)

Y_train &lt;- to_categorical(mm[,2])

mod &lt;- Sequential()  # THIS IS THE EXCITING PART WHERE YOU USE KERAS!! :)</pre>
<p>But then I tried this, and it was clear I was stuck again — it wouldn’t work:</p>
<pre>mod$add(Dense(units = 512, input_shape = dim(X_train)[2]))</pre>
<p>Stack Overflow recommended grabbing a version of kerasR from GitHub, so that’s what I did next:</p>
<pre>install.packages(&quot;devtools&quot;)
library(devtools)
devtools::install_github(&quot;statsmaths/kerasR&quot;)
library(kerasR)</pre>
<p>I got an error in R which told me to go to the Anaconda prompt (which I did), and type this:</p>
<pre>conda install m2w64-toolchain</pre>
<p>Then I went back into R and this worked fantastically:</p>
<pre>mod &lt;- Sequential()</pre>
<p>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 are up on Python, all three (keras, tensorflow, and kerasR) are up in R, and some tutorials seem to be working.</p>
<p> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/qualityandinnovation.wordpress.com/3457/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/qualityandinnovation.wordpress.com/3457/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://qualityandinnovation.com/2017/10/16/a-newbies-install-of-keras-tensorflow-on-windows-10-with-r/"> R – Quality and Innovation</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/ivv51SpFCPM" height="1"></a></div>