티스토리 뷰

Training Neural Net using deepnet
  1. 유명한 XOR
input = matrix(c(0,0,1,1,0,1,0,1), ncol=2)
output = matrix(c(0,1,1,0), ncol=1)
  1. deepnet으로 학습시키기
library(deepnet)
## Warning: package 'deepnet' was built under R version 3.1.2
nn <- nn.train(input, output, hidden=c(2))
# hidden은 hidden layer의 노드 수
# 따라서 위의 neural net은 노드는
#  input node : length(input) = 2
#  hidden layer 1 : 2
#  output node : length(output) = 1
# 따라서 2-2-1의 neural net이다.
  1. 학습이 제대로 되었나 확인한다
library(deepnet)
nn.predict(nn, input)
##        [,1]
## [1,] 0.5103
## [2,] 0.5102
## [3,] 0.5104
## [4,] 0.5102
  1. 이런 전혀 되지 않았다! 무엇이 문제인가? 다시 한번 학습을 시키자. 이번에 학습의 횟수를 늘린다.
nn <- nn.train(input, output, hidden=c(2), numepoch=100000)
## ####loss on step 10000 is : 0.125000
## ####loss on step 20000 is : 0.124997
## ####loss on step 30000 is : 0.000311
## ####loss on step 40000 is : 0.000104
## ####loss on step 50000 is : 0.000062
## ####loss on step 60000 is : 0.000044
## ####loss on step 70000 is : 0.000034
## ####loss on step 80000 is : 0.000028
## ####loss on step 90000 is : 0.000023
## ####loss on step 100000 is : 0.000020
nn.predict(nn, input)
##          [,1]
## [1,] 0.006336
## [2,] 0.992720
## [3,] 0.993992
## [4,] 0.005683
  1. 무려 10만번! 아마도 제대로 학습이 되었을 것이다. 근데 XOR 학습하는데 10만번이나 학습 해야 하는 거야?

두 가지 해법이 있다. 하나는 learningrate를 증가시킨다. learningrate의 기본값은 0.8

nn <- nn.train(input, output, hidden=c(2), learningrate=10, numepoch=10000)
## ####loss on step 10000 is : 0.000014
nn.predict(nn, input)
##          [,1]
## [1,] 0.005344
## [2,] 0.993849
## [3,] 0.994927
## [4,] 0.004803
  1. 만번의 학습으로도 제대로 된 결과를 얻을 수 있을 것이다.

다른 방법은 input을 standardize 하는 것이다.[1] 여기서 간단히 평균을 0으로 만들었다.

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

nn <- nn.train(input, output, hidden=c(2), learningrate=10, numepoch=1000)
nn.predict(nn, input)
##         [,1]
## [1,] 0.02034
## [2,] 0.97970
## [3,] 0.97970
## [4,] 0.02471
  1. 이제 1000번만으로도 원하는 output을 얻었다. 근데 1000번도 좀 많지 싶다! neural net의 복잡도를 감안해서 learning rate를 조정해 줘야 하지 않을까?

[1] 사실 이 방법을 생각해 내고 좋아했는데, 어딘가 논문에 누가 먼저 써 놓은 듯.


'차기작 : R을 배우자' 카테고리의 다른 글

training MNIST data with the package "deepnet"  (2) 2014.11.13
XOR with package "h2o"  (0) 2014.11.11
a CRF model for denoising  (0) 2014.10.04
R source for lempel-ziv complexity  (0) 2014.08.02
R studio, Git, BitBucket  (0) 2014.02.25
공지사항
최근에 올라온 글
최근에 달린 댓글
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
글 보관함