티스토리 뷰

차기작 : R을 배우자

XOR with package "h2o"

quantlab 2014. 11. 11. 00:48
Traing NN with h2o

h2o package로 XOR 훈련시키기 입력 및 출력 자료

input = matrix(c(0,0,1,1,0,1,0,1), ncol=2)
output = matrix(c(0,1,1,0), ncol=1)
data = cbind(input,output)

h2o 불러들이기

library(h2o)
## Warning: package 'h2o' was built under R version 3.0.3
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: rjson
## Loading required package: statmod
## Warning: package 'statmod' was built under R version 3.0.3
## Loading required package: survival
## Loading required package: splines
## Loading required package: tools
## 
## ----------------------------------------------------------------------
## 
## Your next step is to start H2O and get a connection object (named
## 'localH2O', for example):
##     > localH2O = h2o.init()
## 
## For H2O package documentation, ask for help:
##     > ??h2o
## 
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit http://docs.0xdata.com
## 
## ----------------------------------------------------------------------
## 
## 
## Attaching package: 'h2o'
## 
## The following objects are masked from 'package:base':
## 
##     ifelse, max, min, strsplit, sum, tolower, toupper
localH2O=h2o.init()
## 
## H2O is not running yet, starting it now...
## 
## Note:  In case of errors look at the following log files:
##     C:\Users\KCHAO0~1\AppData\Local\Temp\RtmpieJ7nl/h2o_kchao0987_started_from_r.out
##     C:\Users\KCHAO0~1\AppData\Local\Temp\RtmpieJ7nl/h2o_kchao0987_started_from_r.err
## 
## 
## Successfully connected to http://127.0.0.1:54321 
## 
## R is connected to H2O cluster:
##     H2O cluster uptime:         3 seconds 797 milliseconds 
##     H2O cluster version:        2.8.1.1 
##     H2O cluster name:           H2O_started_from_R 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   0.89 GB 
##     H2O cluster total cores:    2 
##     H2O cluster allowed cores:  2 
##     H2O cluster healthy:        TRUE 
## 
## Note:  As started, H2O is limited to the CRAN default of 2 CPUs.
##        Shut down and restart H2O as shown below to use all your CPUs.
##            > h2o.shutdown(localH2O)
##            > localH2O = h2o.init(nthreads = -1)

h2o에 맞게 데이터 변환

data.r <- data
data.h2o <- as.h2o(localH2O, data.r, key="data.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
input.h2o <- as.h2o(localH2O, input, key="input.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%

앞에서 했던 대로…

nn <- h2o.deeplearning(c("V1","V2"),c("V3"), data.h2o, 
                       activation="Tanh", hidden=c(2), epochs=5)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
h2o.predict(nn, input.h2o)
## IP Address: 127.0.0.1 
## Port      : 54321 
## Parsed Data Key: DeepLearningPredict_ed6be678bd454833b1d326597748526a 
## 
##   predict         X0         X1
## 1       0 0.85952228 0.14047772
## 2       0 0.93111759 0.06888234
## 3       1 0.06606984 0.93393010
## 4       1 0.13998389 0.86001605
nn <- h2o.deeplearning(c("V1","V2"),c("V3"), data.h2o, key="nn11",
                       activation="Tanh", hidden=c(2), classification=FALSE,
                       balance_classes=F, rate=0.8, epochs=100000)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=======                                                          |  11%
  |                                                                       
  |==================================                               |  52%
  |                                                                       
  |================================================================ |  99%
  |                                                                       
  |=================================================================| 100%
nn.predict <- h2o.predict(nn, input.h2o)
nn.predict
## IP Address: 127.0.0.1 
## Port      : 54321 
## Parsed Data Key: DeepLearningPredict_2c3b454accbc4ae59517c1b5e7c72458 
## 
##        predict
## 1 0.0004826823
## 2 1.0032317638
## 3 0.9986996055
## 4 0.0003991970
input = matrix(c(-1,-1,1,1,-1,1,-1,1), ncol=2)
output = matrix(c(0,1,1,0), ncol=1)
data = cbind(input,output)

data.r <- data
data.h2o <- as.h2o(localH2O, data.r, key="data.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
input.h2o <- as.h2o(localH2O, input, key="input.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
nn <- h2o.deeplearning(c("V1","V2"),c("V3"), data.h2o, key="nn11",
                       activation="Tanh", hidden=c(2), classification=FALSE,
                       balance_classes=F, rate=0.8, epochs=10000)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
nn.predict <- h2o.predict(nn, input.h2o)
nn.predict
## IP Address: 127.0.0.1 
## Port      : 54321 
## Parsed Data Key: DeepLearningPredict_9f49d1bcf37d42e9acf3e39fca7987bd 
## 
##        predict
## 1 1.901676e-06
## 2 5.086271e-01
## 3 9.999959e-01
## 4 5.086628e-01
input = matrix(c(-1,-1,1,1,-1,1,-1,1), ncol=2)
output = matrix(c(0,1,1,0), ncol=1)
data = cbind(input,output)

data.r <- data
data.h2o <- as.h2o(localH2O, data.r, key="data.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
input.h2o <- as.h2o(localH2O, input, key="input.h2o")
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
#nn <- h2o.deeplearning(c("V1","V2"),c("V3"), data.h2o, key="nn11",
#                       activation="Tanh", hidden=c(2), classification=FALSE,
#                       balance_classes=F, rate=10, epochs=1000)
nn.predict <- h2o.predict(nn, input.h2o)
nn.predict
## IP Address: 127.0.0.1 
## Port      : 54321 
## Parsed Data Key: DeepLearningPredict_0a2b36e5c1994b53b6db459fe58c5da8 
## 
##        predict
## 1 1.901676e-06
## 2 5.086271e-01
## 3 9.999959e-01
## 4 5.086628e-01

h2o.deeplearning은 rate을 1 이상으로 지정할 수 없구나!

근데 deepnet과 비교해서 상당히 빠른 듯 그리고 sigmoid function은 지원하지 않지만, Rectifier를 지원하고, L1, L2 regularization과 max_w2도 지원함…

자세한 내용은 googling으로..ㅋ

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함