A Newbie’s Install of Keras & Tensorflow on Windows 10 with R
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 very important. I do not claim to have 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 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.
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 version of Python 3.5 I typed:
conda create -n tf-keras python=3.5 anaconda
… and then after it was done, I did 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 an error saying that it “can not import html5lib”, so I did this next:
conda install -c conda-forge html5lib
I tried to run the pip command again, but there was an error so I consulted https://www.tensorflow.org/install/install_windows. It told me to do this:
pip install --ignore-installed --upgrade tensorflow
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!!
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 will then get you out of Python and back to the Anaconda prompt.) This means that my Python installation of TensorFlow was functional.
Step 5: Install Keras. I tried this:
pip install keras
…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:
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 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:
data <- data_mnist()
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 ...
Step 8: 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.
trainx<-data$train$x
trainy<-data$train$y
testx<-data$test$x
testy<-data$test$y train_x <- array(train_x, dim = c(dim(train_x)[1], prod(dim(train_x)[-1]))) / 255 test_x <- array(test_x, dim = c(dim(test_x)[1], prod(dim(test_x)[-1]))) / 255 train_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') %>%
layer_dense(units = 10) %>%
layer_activation(activation = 'softmax') model %>% compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
Step 9: 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.
model %>% fit(train_x, train_y, epochs = 100, batch_size = 128)
loss_and_metrics <- model %>% evaluate(test_x, test_y, batch_size = 128)
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, 10) 7850
___________________________________________________________________________________
activation_2 (Activation) (None, 10) 0
===================================================================================
Total params: 623,290
Trainable params: 623,290
Non-trainable params: 0
Step 10: 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_train
Y_train <- mnist$Y_train
X_test <- mnist$X_test
Y_test <- mnist$Y_test > dim(X_train)
[1] 60000 28 28 X_train <- array(X_train, dim = c(dim(X_train)[1], prod(dim(X_train)[-1]))) / 255
X_test <- array(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 do this but got an error:
Y_train <- to_categorical(mnist$Y_train, 10)
So I did this instead:
mm <- model.matrix(~ Y_train) Y_train <- to_categorical(mm[,2]) mod <- Sequential() # THIS IS THE EXCITING PART WHERE YOU USE KERAS!! :)
But then I tried this, and it was clear I was stuck again — it wouldn’t work:
mod$add(Dense(units = 512, 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 into 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 are up on Python, all three (keras, tensorflow, and kerasR) are up in R, and some tutorials seem to be working.
转自: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 10 with R的更多相关文章
- windows 10安装和配置caffe教程 | Install and Configure Caffe on windows 10
本文首发于个人博客https://kezunlin.me/post/1739694c/,欢迎阅读! Install and Configure Caffe on windows 10 Part 1: ...
- 【适合核显电脑的环境配置】Tensorflow教程-Windows 10下安装tensorflow CPU with Anaconda
安装TensorFlow 1.5.0 CPU版本 :仅支持CPU的TensorFlow. 如果您的系统没有NVIDIA GPU,则必须安装此版本. 1.首先下载和安装Anaconda TensorFl ...
- 【适合N卡独显电脑的环境配置】Tensorflow教程-Windows 10下安装tensorflow 1.5.0 GPU with Anaconda
注意: 1.目前Anaconda 更新原命令activate tensorflow 改为 conda activate tensorflow 2. 目前windows with anaconda 可以 ...
- Win10安装Keras+Tensorflow+Opencv
Win10安装keras 安装 Anaconda 清华加速下载链接: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 我选择的版本是: A ...
- Install and run DB Query Analyzer 6.04 on Microsoft Windows 10
Install and run DB Query Analyzer 6.04 on Microsoft Windows 10 DB Query Analyzer is presented ...
- [AI开发]centOS7.5上基于keras/tensorflow深度学习环境搭建
这篇文章详细介绍在centOS7.5上搭建基于keras/tensorflow的深度学习环境,该环境可用于实际生产.本人现在非常熟练linux(Ubuntu/centOS/openSUSE).wind ...
- windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速
原文地址:http://www.jianshu.com/p/c245d46d43f0 写在前面的话 2016年11月29日,Google Brain 工程师团队宣布在 TensorFlow 0.12 ...
- tensor搭建--windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速
windows 10 64bit下安装Tensorflow+Keras+VS2015+CUDA8.0 GPU加速 原文见于:http://www.jianshu.com/p/c245d46d43f0 ...
- 人工智能不过尔尔,基于Python3深度学习库Keras/TensorFlow打造属于自己的聊天机器人(ChatRobot)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_178 聊天机器人(ChatRobot)的概念我们并不陌生,也许你曾经在百无聊赖之下和Siri打情骂俏过,亦或是闲暇之余与小爱同学谈 ...
随机推荐
- ubuntu18.04下监视显卡的运行情况【学习笔记】
作者:庄泽彬(欢迎转载,请注明作者) 说明:使用watch命令监听显卡的使用 安装完显卡驱动之后系统会生成nvidia-smi 这个工具,我们只需要配合watch命令就可以周期性的查看显卡的信息,-n ...
- 从0开始学习 GITHUB 系列之「团队合作利器 BRANCH」【转】
本文转载自:http://stormzhang.com/github/2016/07/09/learn-from-github-from-zero6/ 版权声明:本文为 stormzhang 原创文章 ...
- 【查看内存】Linux查看内存使用情况(一)
用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...
- 使用SpringBoot发送邮件
最后发送成功后,感觉SpringBoot真的很强大. http://www.ykmimi.com/email ↑待加入email输入的重载(可以不上传文件或可以不填写主内容) ↑待加入邮箱RegExp ...
- python应用-掷骰子模拟-pygal
pygal安装: Linux下: pip install pygal Windows下: python -m pip install pygal 效果如图: # -*- coding: utf-8 - ...
- IDEA 搭建授权服务器
引用地址 http://blog.lanyus.com/archives/317.html 今天突出发现基本公开的激活地址都被屏蔽了(IDEA 2017.3 版本) 记录一下 docker pull ...
- 自动生成makefile
原文 http://www.laruence.com/2009/11/18/1154.html 作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实 ...
- UOJ #185【ZJOI2016】 小星星
题目链接:小星星 首先有个暴力很好想.令\(f_{i,j,S}\)表示把\(i\)这棵子树对应到原图中的\(S\)集合,\(i\)号点对应到了\(j\)号点的方案数.这玩意儿复杂度是\(O(3^nn^ ...
- 教你上传代码到码云(与github一样)
以下所有操作都在命令行进行 1 git 配置 git config --global user.name “用户名” git config --global user.email “邮箱”2 生成公钥 ...
- php多站点配置以及Forbidden You don't have permission to access / on this server问题解决
php多站点配置以及Forbidden You don't have permission to access / on this server问题解决 一.总结 一句话总结:我的问题是php的版本问 ...