티스토리 뷰
DATA SET 출처¶
http://archive.ics.uci.edu/ml/datasets/Iris (UCI, Iris Data Set)
참고자료¶
https://www.tensorflow.org (TensorFlow)
https://tensorflow.rstudio.com (TensorFlow for R)
함께보기¶
Clustering using Autoencoder (ver.R)¶
Iris Data Set¶
head(iris)
str(iris)
summary(iris)
Handling the data set¶
set.seed(1)
test_obs <- sample(nrow(iris),50)
training_set <- iris[-test_obs,]
testing_set <- iris[test_obs,]
training_y <- training_set$Species
training_X <- training_set[,-5]
table(training_y)
testing_y <- testing_set$Species
testing_X <- testing_set[,-5]
table(testing_y)
(sd_vec <- apply(training_X,2,sd))
(mean_vec <- apply(training_X,2,mean))
training_X <- t((t(training_X)-mean_vec)/sd_vec)
testing_X <- t((t(testing_X)-mean_vec)/sd_vec)
dim(training_X)
모형에 대한 자세한 설명은 생략하도록 하겠습니다.¶
Autoencoder, elu, softmax, Adam
-> encoding : [inner product -> elu] -> [inner product -> elu] -> [inner product -> elu] -> [inner product -> elu] -> [inner product -> softmax]
-> decoding : [inner product -> elu] -> [inner product -> elu] -> [inner product -> elu] -> [inner product -> elu] -> [inner product]
Loss : squared error loss, Optimizer : Adam
require(tensorflow)
x <- tf$placeholder("float", shape(NULL, 4L))
함수 정의 : weight_variable - truncated normal distribution에서 난수 발생해서 원하는 모양으로 weight tensor를 만드는 함수.
weight_variable <- function(shape){
initial <- tf$truncated_normal(as.integer(shape))
return(tf$Variable(initial))
}
함수 정의 : bias_variable - 원하는 모양으로 bias tensor를 만드는 함수.
bias_variable <- function(shape){
initial <- tf$truncated_normal(as.integer(shape))
return(tf$Variable(initial))
}
모형 설정¶
Encoder¶
1 [inner product -> elu]
W_en1 <- weight_variable(c(4,6))
b_en1 <- bias_variable(c(1,6))
en_layer1 <- tf$nn$elu(tf$matmul(x, W_en1) + b_en1)
2 [inner product -> elu]
W_en2 <- weight_variable(c(6,6))
b_en2 <- bias_variable(c(1,6))
en_layer2 <- tf$nn$elu(tf$matmul(en_layer1, W_en2) + b_en2)
3 [inner product -> elu]
W_en3 <- weight_variable(c(6,5))
b_en3 <- bias_variable(c(1,5))
en_layer3 <- tf$nn$elu(tf$matmul(en_layer2, W_en3) + b_en3)
4 [inner product -> elu]
W_en4 <- weight_variable(c(5,4))
b_en4 <- bias_variable(c(1,4))
en_layer4 <- tf$nn$elu(tf$matmul(en_layer3, W_en4) + b_en4)
5 [inner product -> softmax]
W_en5 <- weight_variable(c(4,3))
b_en5 <- bias_variable(c(1,3))
en_layer5 <- tf$nn$softmax(tf$matmul(en_layer4, W_en5) + b_en5)
Decoder¶
1 [inner product -> elu]
W_de1 <- weight_variable(c(3,4))
b_de1 <- bias_variable(c(1,4))
de_layer1 <- tf$nn$elu(tf$matmul(en_layer5, W_de1) + b_de1)
2 [inner product -> elu]
W_de2 <- weight_variable(c(4,5))
b_de2 <- bias_variable(c(1,5))
de_layer2 <- tf$nn$elu(tf$matmul(de_layer1, W_de2) + b_de2)
3 [inner product -> elu]
W_de3 <- weight_variable(c(5,6))
b_de3 <- bias_variable(c(1,6))
de_layer3 <- tf$nn$elu(tf$matmul(de_layer2, W_de3) + b_de3)
4 [inner product -> elu]
W_de4 <- weight_variable(c(6,6))
b_de4 <- bias_variable(c(1,6))
de_layer4 <- tf$nn$elu(tf$matmul(de_layer3, W_de4) + b_de4)
5 [inner product]
W_de5 <- weight_variable(c(6,4))
b_de5 <- bias_variable(c(1,4))
de_layer5 <- tf$matmul(de_layer4, W_de5) + b_de5
Loss 와 Optimizer 설정¶
SSE <- tf$reduce_sum((de_layer5 - x)^2)
train_step <- tf$train$AdamOptimizer(1e-4)$minimize(SSE)
Session 반복 실행¶
sess <- tf$Session()
sess$run(tf$global_variables_initializer())
for(i in 0:500000){
batch_obs <- sample(100,50)
sess$run(train_step,feed_dict =
dict(x = training_X[batch_obs,]))
if(i%%10000 == 0){
train_accuracy <- sess$run(SSE, feed_dict =
dict(x = training_X))
cat("step ", i, "training accuracy ", train_accuracy , "\n")
}
}
Clustering 결과¶
training¶
result <- sess$run(tf$argmax(en_layer5, 1L), feed_dict = dict(x = training_X))
table(clu = result, true = training_y)
testing¶
result <- sess$run(tf$argmax(en_layer5, 1L), feed_dict = dict(x = testing_X))
table(clu = result, true = testing_y)
이런 결과가 나온 것에 대해서 당연하게 의문점이 생긴다.
정보적인 측면에서 setosa는 특이할만한 어떤 특징이 뚜렷하게 있는 것인데 반해 다른 것들은 뚜렷한 특징이 없는것일까?
training accuracy가 계속 줄어드는 추세이므로 학습을 더 해야 할까? 학습을 더 시키면 versicolor와 virginica도 뚜렷하게 구분해낼까?
'Tensorflow > Tensorflow for R' 카테고리의 다른 글
Tensorflow for R GPU버전 설치 on Ubuntu16.04 (0) | 2018.01.22 |
---|---|
Recurrent Neural Network (ver.R) (0) | 2017.11.28 |
Multilayer Perceptron (ver.R) (0) | 2017.09.30 |
Convolutional Neural Network (ver.R) (0) | 2017.08.14 |
단일신경망 Single Layer Neural Network (ver. R) (0) | 2017.06.24 |