R语言结合H2O做深度学习

环境: Windows 7,  RStudio

1. 进入RStudio,输入安装

 install.packages("h2o", repos=(c("http://s3.amazonaws.com/h2o-release/h2o/rel-kahan/5/R", getOption("repos"))))

2. 加装包,启动h2o本地环境
   library(h2o)
载入需要的程辑包:rjson
载入需要的程辑包:statmod
载入需要的程辑包:tools

----------------------------------------------------------------------

Your next step is to start H2O and get a connection object (named
'localH2O', for example):
    > localH2O = h2o.init()

For H2O package documentation, first call init() and then ask for help:
    > localH2O = h2o.init()
    > ??h2o

To stop H2O you must explicitly call shutdown (either from R, as shown
here, or from the Web UI):
    > h2o.shutdown(localH2O)

After starting H2O, you can use the Web UI at http://localhost:54321
For more information visit http://docs.0xdata.com

----------------------------------------------------------------------

载入程辑包:‘h2o’

下列对象被屏蔽了from ‘package:base’:

    max, min, sum

Warning messages:
1: 程辑包‘h2o’是用R版本3.0.3 来建造的 
2: 程辑包‘rjson’是用R版本3.0.3 来建造的 
3: 程辑包‘statmod’是用R版本3.0.3 来建造的

 

3.  观看下示例
localH2O = h2o.init(ip = “localhost”, port = 54321, startH2O = TRUE,Xmx = ‘1g’)

H2O is not running yet, starting it now…
Performing one-time download of h2o.jar from
http://s3.amazonaws.com/h2o-release/h2o/rel-knuth/11/Rjar/h2o.jar
(This could take a few minutes, please be patient…)

Note:  In case of errors look at the following log files:
C:/TMP/h2o_huangqiang01_started_from_r.out
C:/TMP/h2o_huangqiang01_started_from_r.err

java version “1.7.0_25”
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Successfully connected to http://127.0.0.1:54321
R is connected to H2O cluster:
H2O cluster uptime:        3 seconds 408 milliseconds
H2O cluster version:       2.4.3.11
H2O cluster name:          H2O_started_from_R
H2O cluster total nodes:   1
H2O cluster total memory:  0.96 GB
H2O cluster total cores:   4
H2O cluster healthy:       TRUE

demo(h2o.glm)

4. 训练minist数据

下载 Train Dataset: http://www.pjreddie.com/media/files/mnist_train.csv
下载 Test Dataset: http://www.pjreddie.com/media/files/mnist_test.csv

res <- data.frame(Training = NA, Test = NA, Duration = NA)

#加载数据到h2o
train_h2o <- h2o.importFile(localH2O, path = “C:/Users/jerry/Downloads/mnist_train.csv”)
test_h2o <- h2o.importFile(localH2O, path = “C:/Users/jerry/Downloads/mnist_test.csv”)

y_train <- as.factor(as.matrix(train_h2o[, 1]))
y_test <- as.factor(as.matrix(test_h2o[, 1]))

##训练模型要很长一段时间,多个cpu使用率几乎是100%,风扇狂响。最后一行有相应的进度条可查看
model <- h2o.deeplearning(x = 2:785,  # column numbers for predictors
y = 1,   # column number for label
data = train_h2o,
activation = “Tanh”,
balance_classes = TRUE,
hidden = c(100, 100, 100),  ## three hidden layers
epochs = 100)

#输出模型结果
> model
IP Address: localhost
Port      : 54321
Parsed Data Key: mnist_train.hex
Deep Learning Model Key: DeepLearning_9c7831f93efb58b38c3fa08cb17d4e4e

Training classification error: 0
Training mean square error: Inf

Validation classification error: 0
Validation square error: Inf

Confusion matrix:
Reported on mnist_train.hex
Predicted
Actual      0    1    2    3    4    5    6    7    8    9 Error
0      5923    0    0    0    0    0    0    0    0    0     0
1         0 6742    0    0    0    0    0    0    0    0     0
2         0    0 5958    0    0    0    0    0    0    0     0
3         0    0    0 6131    0    0    0    0    0    0     0
4         0    0    0    0 5842    0    0    0    0    0     0
5         0    0    0    0    0 5421    0    0    0    0     0
6         0    0    0    0    0    0 5918    0    0    0     0
7         0    0    0    0    0    0    0 6265    0    0     0
8         0    0    0    0    0    0    0    0 5851    0     0
9         0    0    0    0    0    0    0    0    0 5949     0
Totals 5923 6742 5958 6131 5842 5421 5918 6265 5851 5949     0
>

> str(model)

## 评介性能
yhat_train <- h2o.predict(model, train_h2o)$predict
yhat_train <- as.factor(as.matrix(yhat_train))

yhat_test <- h2o.predict(model, test_h2o)$predict
yhat_test <- as.factor(as.matrix(yhat_test))

查看前100条预测与实际的数据相比较
> y_test[1:100]
[1] 7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4
[67] 6 4 3 0 7 0 2 9 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9
Levels: 0 1 2 3 4 5 6 7 8 9
>
> yhat_test[1:100]
[1] 7 2 1 0 4 1 8 9 4 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4
[67] 6 4 3 0 7 0 2 9 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9
Levels: 0 1 2 3 4 5 6 7 8 9
效果还可以

## 查看并保存结果
library(caret)
res[1, 1] <- round(h2o.confusionMatrix(yhat_train, y_train)$overall[1], 4)
res[1, 2] <- round(h2o.confusionMatrix(yhat_test, y_test)$overall[1], 4)
print(res)

(注意:程辑包‘h2o’是用R版本3.0.1 来建造的 , 因此R base应该升级到相应版本, 不然就出现以下报错:

> library(h2o)
Error in eval(expr, envir, enclos) : 没有”.getNamespace”这个函数
此外: 警告信息:
程辑包‘h2o’是用R版本3.0.1 来建造的
Error : 程辑包‘h2o’里的R写碼载入失败
错误: ‘h2o’程辑包/名字空间载入失败

解决方法: 下载http://cran.r-project.org/bin/windows/base/old/3.0.1/R-3.0.1-win.exe 并安装, 更新其它包的 update.packages(ask=FALSE, checkBuilt = TRUE)
)


R 3.0.1 安装包RCurl, rjson

环境: windows 7, R 3.0.1

安装h2o的时候发现无法在线安装这两个包rjson, RCurl,因此把这两个放在迅雷上下载到本地安装
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/rjson_0.2.14.zip
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/RCurl_1.95-4.3.zip

install.packages(“d:/Downloads/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)
install.packages(“d:/tdDownload/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)

R 3.0.1 安装包RCurl, rjson

环境: windows 7, R 3.0.1

安装h2o的时候发现无法在线安装这两个包rjson, RCurl,因此把这两个放在迅雷上下载到本地安装
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/rjson_0.2.14.zip
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/RCurl_1.95-4.3.zip

install.packages(“d:/Downloads/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)
install.packages(“d:/tdDownload/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)

rJava安装

环境: R 64bit,  Rstudio, JDK 64bit,  Windows 7 64bit
由于想使用rmmseg4j来用中文分词,因而必须安装rJava。但发现出现问题:
Error : .onLoad failed in loadNamespace() for 'rJava', details:
  call: inDL(x, as.logical(local), as.logical(now), ...)
  error: 无法载入共享目标对象‘D:/program/R/R-2.15.1/library/rJava/libs/x64/rJava.dll’::
  LoadLibrary failure:  %1 不是有效的 Win32 应用程序。

此外: 警告信息:
程辑包‘rJava’是用R版本2.15.3 来建造的 
错误: ‘rJava’程辑包/名字空间载入失败,


问题描述: R查找dll文件有问题

解决方法: 添加C:\\Program Files\\Java\\jdk1.6.0_10\\jre\\bin\\server环境变量path,即将jvm.dll这个库加入。 然后重启RStudio

R语言XML包readHTMLTable中文乱码

环境: Windows 7,  Ubuntu 12,  RStudio Desktop
问题: 使用安装在windows 7 上的RStudio desktop, 用包XML中的readHTMLTable读取网页上的<table>数据,例:
library(XML)
u = ‘http://tech.163.com/special/00094IGJ/top1000.html’
url = htmlParse(u, encoding=”GB2312″)
tables = readHTMLTable(url)
raw = tables[[6]]
查看raw中文显示乱码, 查看sessionInfo(),
R version 2.15.1 (2012-06-22)
Platform. x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Chinese (Simplified)_People's Republic of China.936 
[2] LC_CTYPE=Chinese (Simplified)_People's Republic of China.936   
[3] LC_MONETARY=Chinese (Simplified)_People's Republic of China.936
[4] LC_NUMERIC=C                                                   
[5] LC_TIME=Chinese (Simplified)_People's Republic of China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] XML_3.95-0.1

loaded via a namespace (and not attached):
[1] tools_2.15.1

这个与操作相关, 可以尝试更改Sys.setlocale("LC_CTYPE", "UTF-8"),但报“操作系统报告说无法执行将本地化设成"UTF-8"的请求”。


在Ubuntu中使用RStudio却能正确显示中文,查看sessionInfo()
R version 2.14.1 (2011-12-22)
Platform. x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C         LC_TIME=C           
 [4] LC_COLLATE=C         LC_MONETARY=C        LC_MESSAGES=C       
 [7] LC_PAPER=C           LC_NAME=C            LC_ADDRESS=C        
[10] LC_TELEPHONE=C       LC_MEASUREMENT=C ""LC_IDENTIFICATION"" =C 

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] tools_2.14.1



造成的原因推测是XML包编码方式与操作系统的字符编码相关。 有高手知道的具体原因的请帮忙解答下。