ML의 실용과 몇가지 팁 PART 4


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

아래의 내용은 ML의 실용과 몇가지 팁 수업에 있는 python 코드를 R로 변경한 것이다.
소스는 lab-07-4-mnist_introduction.py 이다.
library(tensorflow)
library(ggplot2)
library(reshape2)


# Check out https://www.tensorflow.org/get_started/mnist/beginners for
# more information about the mnist dataset
# devtools::install_github("rstudio/tensorflow")

# 아래 구문이 에러나면 위에 구문으로 tensorflow 재설치 필요
datasets <- tf$contrib$learn$datasets
mnist <- datasets$mnist$read_data_sets("MNIST-data", one_hot = TRUE)


# reticulate::py_config()
# tf_config()
# devtools::session_info()
# session_info()
# sessionInfo()

names(mnist)
# [1] "count"    "index"    "test"    "train"    "validation"

mnist$train
# <tensorflow.contrib.learn.python.learn.datasets.mnist.DataSet>

nb_classes = 10L

# MNIST data image of shape 28 * 28 = 784
X = tf$placeholder(tf$float32, shape=list(NULL, 784L))
# 0 - 9 digits recognition = 10 classes
Y = tf$placeholder(tf$float32, shape=list(NULL, nb_classes))

W = tf$Variable(tf$random_normal(shape=list(784L, nb_classes) ))
b = tf$Variable(tf$random_normal(shape = list(nb_classes) ))


# Hypothesis (using softmax)
hypothesis = tf$nn$softmax(logits=(tf$matmul(X, W) + b))

cost = tf$reduce_mean(input_tensor=-tf$reduce_sum(Y * tf$log(hypothesis), axis=1L))
train = tf$train$GradientDescentOptimizer(learning_rate=0.1)$minimize(cost)

is_correct = tf$equal(tf$argmax(hypothesis, 1L), tf$argmax(Y, 1L))
accuracy = tf$reduce_mean(tf$cast(is_correct, tf$float32))


# parameters
num_epochs = 15L
batch_size = 100L
num_iterations = as.numeric(mnist$train$num_examples / batch_size)

sess <- tf$Session()
    
# Initialize TensorFlow variables
sess$run(tf$global_variables_initializer())

# Training cycle
for ( epoch in 1:num_epochs) {
    avg_cost = 0

    for (i in 1:num_iterations) {
        batches = mnist$train$next_batch(batch_size)
        # length(batches)
        batch_xs <- batches[[1]]
        batch_ys <- batches[[2]]
        
        stage = sess$run(list(train, cost), feed_dict=dict(X = batch_xs, Y = batch_ys) )
        avg_cost = avg_cost + stage[[2]] / num_iterations
        
        if (i %% 100 == 1) {
            print(paste0("Epoch: ", epoch, ", Cost: ", format(avg_cost, digits = 4) ))
        }
        
    }
    
    print(paste0("LAST >> Epoch: ", epoch, ", Cost: ", format(avg_cost, digits = 4) ))
}


print("===================== Learning finished =====================")

# Test the model using test sets
print( paste0( "Accuracy: ", 
               accuracy$eval(session=sess, feed_dict=dict(X = mnist$test$images, Y = mnist$test$labels ) ) 
              ) 
       )


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

# batch_xs로 데이터를 가져 온 후 이미지 출력해야 함

n <- 36
indices <- sample(nrow(batch_xs), size = n)
data <- array(batch_xs[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")


댓글 없음:

댓글 쓰기