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


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


아래의 내용은 softmax cross entropy를 실행하는 예제이다. 기본 학습 방법에 해당한다.
훈련 단계에서 merged_summary에 값 설정 중 에러가 발생할 경우는 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)

# 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
W = tf$Variable(initial_value=tf$random_normal(shape=list(784L, 10L)))
b = tf$Variable(initial_value=tf$random_normal(shape=list(10L)))


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

hypothesis = tf$matmul(X, W) + b

# 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) {
        # epoch <- 1
        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
            
            if (interation %% 100 == 0)
                cat("Epoch:", (epoch + 1), "Interation:", interation, "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")

    # Learning Finished!
    # Accuracy: 0.9174 

} )




# 이미지를 그래프로 보는 방법은
# 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
    )


print("END OF CODE")



댓글 없음:

댓글 쓰기