Chapter 13 Neural Network2 (W12)

뉴런: 신경세포

뉴런

Figure 13.1: 뉴런

인공신경망

인공신경망

Figure 13.2: 인공신경망

13.1 인공신경망이란? (Artificial Neural Network?)

  • 인간의 뇌 기능을 모방한 네트워크
  • 인간의 사고방식을 컴퓨터에 학습시키는 것

13.1.1 Input Layer: 입력계층

  • 자극 부분
  • x1, x2, x3…

13.1.2 Hidden Layer: 은닉계층

  • 블랙박스라고도 함
  • 여러개의 층을 가질 수 있으나, 일반적으로 1개나 2개
  • 많으면 과적합 (Overfitting)문제가 발생함
    • 과적합
      • 모델이 너무 복잡함
      • 학습데이터에서는 결과가 잘 나옴
      • 실제데이터에서는 결과가 잘 안나옴
      • 왜? 학습데이터는 실제뎅터의 부분집합이므로 편향 될 수 있고 모든 속성을 가지지 않음

13.1.3 출력계층: Output Layer

  • 반응 부분
  • y1, y2, y3…
  • 은닉층의 모든 단위들과 연결
  • 출력은 어떻게 결정하는가?
    • 활성함수 (activation function)
      • 계단 함수, 부호함수,
      • 시그모이드 함수
      • 선형함수

13.2 경영학과 인공신경망

  • 재무, 회계, 마케팅 등과 같은 다양한 부문에 활용가능
  • 인공신경망 모델에서 결정된 가중치를 기반으로 의사결정에 도움을 줌

13.3 예제

  • neural network package
    • nnet
    • neuralnet
  • nnet: neural network package
  • caret
    • 지난번에 배운 의사결정나무 (Decision Tree)에서도 사용했음
    • 훈련데이터, 테스트 데이터를 나누기 위해 –> seperate data (training: test)
  • sys timezone 에러가 날 경우
    • Sys.setenv(TZ=‘GMT’)

13.3.1 nnet 패키지

## ================================================================
## 01. 환경설정 및 데이터 불러오기
## ================================================================
library(nnet)
data(iris)

# nnet 종속변수 입력을 위한 종속변수 변환
species.ind <- class.ind(iris$Species)
iris <- cbind(iris, species.ind)

## ================================================================
## 02. 훈련 및 검증 데이터 분리
## ================================================================
train.idx <- sample(1:150, 100)

iris.train.df <- iris[train.idx, ]
iris.test.df <- iris[-train.idx, ]

## ================================================================
## 03. 신경망 모형 적합
## ================================================================

iris.nn <- nnet(x = iris.train.df[, c(1:4)], y = iris.train.df[, 
    c(6:8)], size = 10, softmax = TRUE)
## # weights:  83
## initial  value 120.965425 
## iter  10 value 47.031199
## iter  20 value 11.742304
## iter  30 value 6.657025
## iter  40 value 4.812066
## iter  50 value 4.585421
## iter  60 value 4.570077
## iter  70 value 4.569501
## final  value 4.569500 
## converged
## ================================================================
## 04. 검증 데이터 예측 및 모형 평가
## ================================================================
## 훈련데이터
iris.pred <- predict(iris.nn, iris.train.df[, c(1:4)], type = "class")
table(iris.pred, iris.train.df$Species)
##             
## iris.pred    setosa versicolor virginica
##   setosa         32          0         0
##   versicolor      0         32         0
##   virginica       0          1        35
# 검증데이터
iris.pred <- predict(iris.nn, iris.test.df[, c(1:4)], type = "class")
table(iris.pred, iris.test.df$Species)
##             
## iris.pred    setosa versicolor virginica
##   setosa         18          1         0
##   versicolor      0         16         0
##   virginica       0          0        15
## ================================================================
## 05. 신경망 시각화
## ================================================================
## 시각화 R 코드 함수 다운로드
library(devtools)
source_url("https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r")
## SHA-1 hash of file is 74c80bd5ddbc17ab3ae5ece9c0ed9beb612e87ef
# 신경망 모형 시각화
library(reshape2)
plot.nnet(iris.nn)
## Loading required package: scales
## 
## Attaching package: 'scales'
## The following object is masked from 'package:plotrix':
## 
##     rescale
## Loading required package: reshape
## 
## Attaching package: 'reshape'
## The following objects are masked from 'package:reshape2':
## 
##     colsplit, melt, recast
## The following objects are masked from 'package:plyr':
## 
##     rename, round_any

검증

# table(iris.pred, iris.train.df$Species)
table(iris.pred, iris.test.df$Species)
##             
## iris.pred    setosa versicolor virginica
##   setosa         18          1         0
##   versicolor      0         16         0
##   virginica       0          0        15

13.3.2 neuralnet 패키지

library(neuralnet)
data(iris)
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 
# neuralnet은 식에서 '.'을 지원하지 않기 때문에 아래와
# 같이 식을 문자열로 생성.
formula <- as.formula(paste("Species ~ ", paste(names(iris)[-length(iris)], 
    collapse = "+")))
formula
## Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
formula <- as.formula(paste("setosa + versicolor + virginica ~ ", 
    paste(names(iris)[-length(iris)], collapse = "+")))
formula
## setosa + versicolor + virginica ~ Sepal.Length + Sepal.Width + 
##     Petal.Length + Petal.Width
trainData <- cbind(iris[, 1:4], class.ind(iris$Species))
head(trainData)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width setosa versicolor
## 1          5.1         3.5          1.4         0.2      1          0
## 2          4.9         3.0          1.4         0.2      1          0
## 3          4.7         3.2          1.3         0.2      1          0
## 4          4.6         3.1          1.5         0.2      1          0
## 5          5.0         3.6          1.4         0.2      1          0
## 6          5.4         3.9          1.7         0.4      1          0
##   virginica
## 1         0
## 2         0
## 3         0
## 4         0
## 5         0
## 6         0
multi_model <- neuralnet(formula, trainData, hidden = 3)
plot(multi_model)

species_prob = compute(multi_model, iris[, 1:4])$net.result
species_prob
##                      [,1]               [,2]               [,3]
##   [1,]  1.000057707030813 -0.000102754014119  0.000002342043184
##   [2,]  1.000037412323813 -0.000061633125555  0.000008659995948
##   [3,]  1.000050211387514 -0.000090805722475  0.000003675827738
##   [4,]  1.000029471517131 -0.000065298309382  0.000005035478588
##   [5,]  1.000058304962745 -0.000104400431052  0.000002021686664
##   [6,]  1.000050100558360 -0.000092784400015  0.000003030382018
##   [7,]  1.000037425790689 -0.000079237385818  0.000003231320727
##   [8,]  1.000054338770845 -0.000097637430845  0.000002863461942
##   [9,]  0.999997938497114 -0.000022318167105  0.000008398418352
##  [10,]  1.000053680035691 -0.000095295296346  0.000003379433893
##  [11,]  1.000059218844035 -0.000105445293119  0.000001986185131
##  [12,]  1.000049961197867 -0.000094033847198  0.000002601027504
##  [13,]  1.000052226790819 -0.000091948592202  0.000003955954993
##  [14,]  1.000054316344174 -0.000098092819119  0.000002715881275
##  [15,]  1.000060298020486 -0.000107616785790  0.000001654893012
##  [16,]  1.000059806415190 -0.000106881336328  0.000001727499421
##  [17,]  1.000057378519864 -0.000101797972362  0.000002533938474
##  [18,]  1.000051821575150 -0.000092357074662  0.000003702655258
##  [19,]  1.000057155351855 -0.000101088837022  0.000002682712656
##  [20,]  1.000056678979147 -0.000102440903625  0.000002115867441
##  [21,]  1.000053637350369 -0.000094078425362  0.000003741573026
##  [22,]  1.000043086412591 -0.000082730072325  0.000003930853700
##  [23,]  1.000059380217210 -0.000106256561570  0.000001786487914
##  [24,]  0.999592883825390  0.000540501137155  0.000054905599752
##  [25,]  1.000034907668944 -0.000080776275748  0.000001965715181
##  [26,]  1.000022736141138 -0.000037830137765  0.000011397622130
##  [27,]  0.999977065066813  0.000008920808075  0.000011484966355
##  [28,]  1.000057172128502 -0.000101688013358  0.000002503066869
##  [29,]  1.000056962245505 -0.000100345467885  0.000002851491122
##  [30,]  1.000035102280847 -0.000074269764420  0.000004034816905
##  [31,]  1.000027438148908 -0.000058366833165  0.000006536149678
##  [32,]  1.000023275658471 -0.000028069311139  0.000014579347364
##  [33,]  1.000060275416487 -0.000107750805822  0.000001606435085
##  [34,]  1.000060265219336 -0.000107685576373  0.000001623363862
##  [35,]  1.000038933082996 -0.000069873503273  0.000006594422835
##  [36,]  1.000055352279090 -0.000095809610881  0.000003745790085
##  [37,]  1.000059267968918 -0.000104423080066  0.000002317078474
##  [38,]  1.000059643397744 -0.000106702204045  0.000001731594894
##  [39,]  1.000028923570512 -0.000063188799494  0.000005514446887
##  [40,]  1.000055053250196 -0.000098028615726  0.000002967081972
##  [41,]  1.000053250320202 -0.000095021081533  0.000003329130536
##  [42,]  0.998780454687770  0.002401379945875  0.000374096245729
##  [43,]  1.000045791759843 -0.000088771203913  0.000002915954326
##  [44,]  0.999487425706844  0.000583682660142  0.000035118387509
##  [45,]  1.000021526887765 -0.000067613448858  0.000001826402168
##  [46,]  0.999976785329683  0.000037179348204  0.000020118082768
##  [47,]  1.000058806136191 -0.000105637065185  0.000001797412520
##  [48,]  1.000044809836466 -0.000085647489537  0.000003571651682
##  [49,]  1.000059056791782 -0.000105345409407  0.000001966126532
##  [50,]  1.000053896431935 -0.000095406339046  0.000003413112564
##  [51,] -0.000210150987537  1.004342208882166 -0.003783363316744
##  [52,] -0.000245849622204  1.003876575568043 -0.003237441749020
##  [53,] -0.000250882124698  0.997967514013065  0.002707147356275
##  [54,] -0.000250909942631  1.003661303450536 -0.002988756465711
##  [55,] -0.000252602027148  0.995991187995093  0.004691757319344
##  [56,] -0.000248964016898  1.002923042899643 -0.002301751897053
##  [57,] -0.000253201863899  0.996481590975635  0.004192134497870
##  [58,]  0.000973052133071  1.001214667555427 -0.004687719026622
##  [59,] -0.000218162953650  1.004265410165551 -0.003694788476942
##  [60,] -0.000251113183953  1.004160301714881 -0.003514634413817
##  [61,] -0.000089535983083  1.004264770146667 -0.004069444061973
##  [62,] -0.000250421407962  1.003908828739348 -0.003251723901347
##  [63,]  0.000019741427792  1.004213915450077 -0.004046200051713
##  [64,] -0.000251905424107  0.998152941679787  0.002512097788502
##  [65,] -0.000018355339316  1.003992665140875 -0.004138344469015
##  [66,] -0.000201370287834  1.004467668236280 -0.003929411806198
##  [67,] -0.000253577056647  0.994130414688387  0.006540963346597
##  [68,]  0.000754803291637  1.001824689022496 -0.004564439882854
##  [69,] -0.000220717585274  0.869791335308692  0.130788723622740
##  [70,] -0.000043279827724  1.004090815927324 -0.004104328920576
##  [71,] -0.000136246102342  0.554071107742939  0.446230825074212
##  [72,] -0.000165209701094  1.004446339700435 -0.004014713920653
##  [73,] -0.000175059904467  0.699472705277849  0.300956968145960
##  [74,] -0.000228987787472  1.003765695310316 -0.003237703115567
##  [75,] -0.000179752003878  1.004437184524178 -0.003971172492017
##  [76,] -0.000223721662793  1.004423473536461 -0.003826053384413
##  [77,] -0.000250032023449  0.999711231687524  0.000964810404128
##  [78,] -0.000174715395534  0.698017993021939  0.302410577334285
##  [79,] -0.000253282091284  0.997099281710058  0.003579191775218
##  [80,]  0.003796546341829  0.997635621635096 -0.004906047881831
##  [81,] -0.000080212730023  1.004215158078186 -0.004077265363160
##  [82,]  0.000530042095040  1.002910369138904 -0.004301831387640
##  [83,] -0.000072046190828  1.004189185088280 -0.004084330521366
##  [84,] -0.000036945486011  0.183960291607871  0.816016130994739
##  [85,] -0.000252773549113  0.988850354376578  0.011817841421489
##  [86,] -0.000254028643886  1.002244663476543 -0.001587150522701
##  [87,] -0.000250046353339  1.002045998693082 -0.001376450145517
##  [88,] -0.000251154847729  1.001756949094558 -0.001073890448362
##  [89,] -0.000197982075018  1.004306572627713 -0.004032357141978
##  [90,] -0.000244802805880  1.004266822035936 -0.003628883698454
##  [91,] -0.000243839756898  1.003652480853514 -0.003068886117610
##  [92,] -0.000249392760835  1.002612099299157 -0.001969487141768
##  [93,] -0.000164972857789  1.004404107809636 -0.004016122496769
##  [94,]  0.000720295515931  1.002316445221534 -0.004426897321123
##  [95,] -0.000242578865183  1.004170323615589 -0.003566217256682
##  [96,] -0.000091021068294  1.003825734765755 -0.004186235645601
##  [97,] -0.000222056505224  1.004375744583858 -0.003906808830362
##  [98,] -0.000199355662396  1.004419957242795 -0.003939685601641
##  [99,]  0.001646081125560  1.000912667826840 -0.004569991975673
## [100,] -0.000221674548492  1.004431401459793 -0.003920409049274
## [101,]  0.000014792422715 -0.008958797909592  1.008765877439017
## [102,]  0.000013934992122 -0.005764685981363  1.005574560381948
## [103,]  0.000014774533527 -0.008892078266609  1.008699214702552
## [104,]  0.000013996447124 -0.005997856791354  1.005807494941253
## [105,]  0.000014789623473 -0.008948503657473  1.008755591685660
## [106,]  0.000014792020488 -0.008957312554778  1.008764392969844
## [107,] -0.000019719602928  0.119664745412402  0.880255402830331
## [108,]  0.000014737980409 -0.008755107524581  1.008562351657879
## [109,]  0.000014726413207 -0.008712106700164  1.008519396790981
## [110,]  0.000014791862713 -0.008956743560687  1.008763824756682
## [111,]  0.000003849123831  0.031841431401837  0.968001509629419
## [112,]  0.000014032147449 -0.006123256568666  1.005932818599822
## [113,]  0.000014647771279 -0.008419333555614  1.008226883981801
## [114,]  0.000014651342792 -0.008433206627444  1.008240745962066
## [115,]  0.000014790347368 -0.008951070017416  1.008758156289380
## [116,]  0.000014743703493 -0.008777413281844  1.008584651149024
## [117,]  0.000010992159505  0.005207584616165  0.994611867596643
## [118,]  0.000014788425351 -0.008944941553872  1.008752029597009
## [119,]  0.000014792470362 -0.008958952807339  1.008766032240195
## [120,] -0.000020001002842  0.120835012637147  0.879086113755910
## [121,]  0.000014783347316 -0.008925029337794  1.008732137945863
## [122,]  0.000013815072666 -0.005317804384438  1.005128076983902
## [123,]  0.000014792093475 -0.008957541264220  1.008764621442475
## [124,] -0.000015464342977  0.103868112134571  0.896038212397043
## [125,]  0.000014659805210 -0.008465975227120  1.008273479789199
## [126,]  0.000013332838342 -0.003510780719116  1.003322557368002
## [127,] -0.000052211150519  0.240876380455642  0.759150497745639
## [128,] -0.000057215217866  0.259507807304846  0.740535327390414
## [129,]  0.000014780252029 -0.008913594123537  1.008720712294633
## [130,] -0.000011124131546  0.087871221917161  0.912020505094601
## [131,]  0.000014749347400 -0.008797054776500  1.008604271497993
## [132,]  0.000014213981154 -0.006802001910561  1.006610906748458
## [133,]  0.000014789132341 -0.008946583925286  1.008753673855029
## [134,] -0.000181455175655  0.723443847524644  0.277005398068689
## [135,] -0.000005568463820  0.066925170252298  0.932947677857599
## [136,]  0.000014791294621 -0.008954540963388  1.008761624118726
## [137,]  0.000014788631911 -0.008944851398255  1.008751942730069
## [138,]  0.000009765519975  0.009768437211712  0.990054984025546
## [139,] -0.000084669959829  0.361860826025513  0.638272364035027
## [140,]  0.000014223464374 -0.006836645259283  1.006645585152378
## [141,]  0.000014790478666 -0.008951559406089  1.008758645178157
## [142,]  0.000014470272829 -0.007757421020486  1.007565556642096
## [143,]  0.000013934992122 -0.005764685981363  1.005574560381948
## [144,]  0.000014790526311 -0.008951812281595  1.008758897583230
## [145,]  0.000014791730898 -0.008956226940531  1.008763308671269
## [146,]  0.000014725803014 -0.008710359421544  1.008517656883509
## [147,]  0.000013029311348 -0.002382886055027  1.002195744255834
## [148,]  0.000012630317513 -0.000896985652979  1.000711141857962
## [149,]  0.000014748575329 -0.008796141283085  1.008603361677057
## [150,] -0.000001031329649  0.050006496193439  0.949852306748718