실습: 딥러닝으로 MNIST 98%이상 해보기 PART 2 (NN)


개요(Abstract)
아래의 내용은 hunkim님의 모두를 위한 머신러닝/딥러닝 강의에서 tensorFlow 사용 예제를 R로 변경한 내용이다.
hunkim님의 강좌 주소는 http://hunkim.github.io/ml/ 이다.


아래의 내용은 신경망을 사용하는 예제이다.
여기서는 망을 3단 구성하였다.
훈련 단계에서 tensorflow에 에러가 발생할 경우는 R session을 재시작 해야 한다.
library(tensorflow)
library(ggplot2)
library(reshape2)

# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)

# parameters
learning_rate = 0.001
batch_size = 100L
num_epochs = 50
num_iterations = as.integer(mnist$train$num_examples / batch_size)


# input place holders
X = tf$placeholder(tf$float32, shape=list(NULL,784L))
Y = tf$placeholder(tf$float32, shape=list(NULL, 10L))

# weights & bias for nn layers
W1 = tf$Variable(initial_value=tf$random_normal(shape=list(784L, 256L)), name="weight1")
b1 = tf$Variable(initial_value=tf$random_normal(shape=list(256L)), name="biase1")

W2 = tf$Variable(initial_value=tf$random_normal(shape=list(256L, 256L)), name="weight2")
b2 = tf$Variable(initial_value=tf$random_normal(shape=list(256L)), name="biase2")

W3 = tf$Variable(initial_value=tf$random_normal(shape=list(256L, 10L)), name="weight3")
b3 = tf$Variable(initial_value=tf$random_normal(shape=list(10L)), name="biase3")

layer1 = tf$matmul(X, W1) + b1
layer2 = tf$matmul(layer1, W2) + b2
hypothesis = tf$matmul(layer2, W3) + b3


# softmax_cross_entropy_with_logits_v2
cost = tf$reduce_mean(
    tf$nn$softmax_cross_entropy_with_logits_v2(logits=hypothesis, labels=tf$stop_gradient(Y))
)
train = tf$train$AdamOptimizer(learning_rate=learning_rate)$minimize(cost)

correct_prediction = tf$equal(x=tf$argmax(input=hypothesis, axis=1L), y=tf$argmax(input=Y, axis=1L))
accuracy = tf$reduce_mean(tf$cast(x=correct_prediction,dtype=tf$float32))


# train my model
# sess <- tf$Session()
with (tf$Session() %as% sess, {
    # initialize
    sess$run(fetches=tf$global_variables_initializer())
    
    for (epoch in 1:num_epochs) {
        avg_cost = 0
        
        for (interation in 1:num_iterations) {
            
            batches = mnist$train$next_batch(batch_size)
            batch_xs <- batches[[1]]
            batch_ys <- batches[[2]]
            
            stage = sess$run(fetches=list(train, cost), feed_dict=dict(X = batch_xs, Y = batch_ys))
            avg_cost = avg_cost + stage[[2]] / num_iterations
        }
                
        cat("Epoch:", (epoch + 1), "Cost:", avg_cost, "\n")

    }
    
    cat("\nLearning Finished!\n")
    
    # Test model and check accuracy
    accur <- sess$run(fetches=list(accuracy), feed_dict=dict(X = mnist$test$images, Y = mnist$test$labels))
    cat("Accuracy:", accur[[1]], "\n")
    
    # Get one and predict
    r = sample(0:(mnist$test$num_examples - 1), 1)
    
    lbl <- which.max(mnist$test$labels[r,])
    cat("Label: ", lbl, "\n")
    
    pred <- sess$run(tf$argmax(hypothesis, axis=1L), feed_dict=dict(X = mnist$test$images[r:(r+1), ]))
    cat( "Prediction: ", paste0(pred) , "\n")
    
    # 신경망 기법은 sortmax 기법에 비해 예측력이 나아지지 않음
    # Learning Finished!
    # Accuracy: 0.8888 
} )



# 이미지를 그래프로 보는 방법은
# https://tensorflow.rstudio.com/tfestimators/articles/examples/mnist.html 를 참조

# 0 인 글자 중 예측 오류 글자 확인
tmp.df <- as.data.frame(cbind(pred=pred, lbls = lbls))

indices <- which(tmp.df$pred != 0 & tmp.df$lbls == 0)
ifelse(length(indices) <= 36, indices <- indices, indices <- sample(indices, 36))
indices <- indices[order(indices)]


n <- length(indices)
data <- array(mnist$test$images[indices, ], dim = c(n, 28, 28))
melted <- melt(data, varnames = c("image", "x", "y"), value.name = "intensity")
ggplot(melted, aes(x = x, y = y, fill = intensity)) +
    geom_tile() +
    scale_fill_continuous(name = "Pixel Intensity") +
    scale_y_reverse() +
    facet_wrap(~ image, nrow = sqrt(n), ncol = sqrt(n)) +
    theme(
        strip.background = element_blank(),
        strip.text.x = element_blank(),
        panel.spacing = unit(0, "lines"),
        axis.text = element_blank(),
        axis.ticks = element_blank()
    ) +
    labs(
        title = "MNIST Image Data",
        subtitle = "Visualization of a sample of images contained in MNIST data set.",
        x = NULL,
        y = NULL
    )


댓글 없음:

댓글 쓰기