Keras for R, MNIST 간단 예제

MNIST

데이터 준비

library(keras)
mnist<-dataset_mnist()
c(x_train,y_train) %<-% mnist$train
c(x_test,y_test) %<-% mnist$test
NUM_CATEGORY=10

Reshape

x_train<-x_train%>%array_reshape(c(nrow(x_train),784))

원래 데이터는 (images,width,height)의 3차원 데이터이다. images 인덱스만 남기고 width와 height를 flattern시켰다.

x_test<-x_test%>%array_reshape(c(nrow(x_test),784))

마찬가지다.

Categorical

y_train<-y_train%>%
  to_categorical(NUM_CATEGORY)
y_test<-y_test%>%
  to_categorical(NUM_CATEGORY)

Model

model<-keras_model_sequential()

레이어를 쌓자.

model%>%
  layer_dense(256,
              activation='relu',
              input_shape=c(784))%>%
  layer_dropout(0.4)%>%
  layer_dense(128,
              activation='relu')%>%
  layer_dropout(0.3)%>%
  layer_dense(10,
              activation='softmax')

모델요약

summary(model)
____________________________________________________________________________________________________
Layer (type)                                 Output Shape                            Param #        
====================================================================================================
dense_3 (Dense)                              (None, 256)                             200960         
____________________________________________________________________________________________________
dropout_2 (Dropout)                          (None, 256)                             0              
____________________________________________________________________________________________________
dense_4 (Dense)                              (None, 128)                             32896          
____________________________________________________________________________________________________
dropout_3 (Dropout)                          (None, 128)                             0              
____________________________________________________________________________________________________
dense_5 (Dense)                              (None, 10)                              1290           
====================================================================================================
Total params: 235,146
Trainable params: 235,146
Non-trainable params: 0
____________________________________________________________________________________________________

Compile

model%>%compile(
  loss='categorical_crossentropy',
  optimizer=optimizer_rmsprop(),
  metrics=c('accuracy')
)

Training

history<-model%>%
  fit(
    x_train,y_train,
    epochs=30,
    batch_size=128,
    validation_split=0.2
  )

더 학습시키자.

history<-model%>%
  fit(
    x_train,y_train,
    epochs=2,
    batch_size=128,
    validation_split=0.2
  )

Evaluation

model%>%evaluate(x_test,y_test,
                 verbose=FALSE)
$loss
[1] 2.377416

$acc
[1] 0.8518

Prediction

model%>%
  predict_classes(x_test)
   [1] 7 2 1 0 4 1 4 4 6 7 0 6 7 0 1 5 7 7 3 4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3 4 0 1 2 2 0 8 7
 [ reached getOption("max.print") -- omitted 9000 entries ]

Confusion matrix를 도출하자.

predicted=model%>%
  predict_classes(x_test)
original=y_test%>%apply(1,which.max)-1
table(original,predicted)
        predicted
original    0    1    2    3    4    5    6    7    8
       0  960    0    2    1    0    5    4    4    4
       1    0 1111    4    3    0    0    4    2   11
       2   12    0  937    8   14    2   13   19   27
       3    4    0   17  917    1   25    1   20   25
       4    1    0    2    1  958    0    8    5    7
       5    9    1    2   18    5  826   16    5   10
       6   16    3    0    1    8    6  920    1    3
       7    1    5   22    1    5    1    0  990    3
       8   10    1    6   12    8   11    9   18  899
       9   13    6    0   13  535   17    1  377   47
LS0tCnRpdGxlOiAiTU5JU1QiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCiMg642w7J207YSwIOykgOu5hAoKYGBge3J9CmxpYnJhcnkoa2VyYXMpCm1uaXN0PC1kYXRhc2V0X21uaXN0KCkKYGBgCgpgYGB7cn0KYyh4X3RyYWluLHlfdHJhaW4pICU8LSUgbW5pc3QkdHJhaW4KYyh4X3Rlc3QseV90ZXN0KSAlPC0lIG1uaXN0JHRlc3QKYGBgCmBgYHtyfQpOVU1fQ0FURUdPUlk9MTAKYGBgCgoKIyMgUmVzaGFwZQoKYGBge3J9CnhfdHJhaW48LXhfdHJhaW4lPiVhcnJheV9yZXNoYXBlKGMobnJvdyh4X3RyYWluKSw3ODQpKQpgYGAK7JuQ656YIOuNsOydtO2EsOuKlCAoaW1hZ2VzLHdpZHRoLGhlaWdodCnsnZggM+ywqOybkCDrjbDsnbTthLDsnbTri6QuIGltYWdlcyDsnbjrjbHsiqTrp4wg64Ko6riw6rOgIHdpZHRo7JmAIGhlaWdodOulvCBmbGF0dGVybuyLnOy8sOuLpC4KYGBge3J9CnhfdGVzdDwteF90ZXN0JT4lYXJyYXlfcmVzaGFwZShjKG5yb3coeF90ZXN0KSw3ODQpKQpgYGAK66eI7LCs6rCA7KeA64ukLgoKIyMgQ2F0ZWdvcmlj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X3RyYWluLHlfdHJhaW4sCiAgICBlcG9jaHM9MiwKICAgIGJhdGNoX3NpemU9MTI4LAogICAgdmFsaWRhdGlvbl9zcGxpdD0wLjIKICApCmBgYAoKIyBFdmFsdWF0aW9uCgpgYGB7cn0KbW9kZWwlPiVldmFsdWF0ZSh4X3Rlc3QseV90ZXN0LAogICAgICAgICAgICAgICAgIHZlcmJvc2U9RkFMU0UpCmBgYAoKIyBQcmVkaWN0aW9uCmBgYHtyfQptb2RlbCU+JQogIHByZWRpY3RfY2xhc3Nlcyh4X3Rlc3QpCmBgYAoKQ29uZnVzaW9uIG1hdHJpeOulvCDrj4TstpztlZjsnpAuCgpgYGB7cn0KcHJlZGljdGVkPW1vZGVsJT4lCiAgcHJlZGljdF9jbGFzc2VzKHhfdGVzdCkKb3JpZ2luYWw9eV90ZXN0JT4lYXBwbHkoMSx3aGljaC5tYXgpLTEKdGFibGUob3JpZ2luYWwscHJlZGljdGVkKQpgYGAKCg==

댓글

이 블로그의 인기 게시물

Bradley-Terry Model: paired comparison models

R에서 csv 파일 읽는 법

xlwings tutorial - 데이터 계산하여 붙여 넣기