개요(Abstract)
아래의 내용은 hunkim님의 모두를 위한 머신러닝/딥러닝 강의에서 tensorFlow 사용 예제를 R로 변경한 내용이다.
아래의 내용은 hunkim님의 모두를 위한 머신러닝/딥러닝 강의에서 tensorFlow 사용 예제를 R로 변경한 내용이다.
hunkim님의 강좌 주소는 http://hunkim.github.io/ml/ 이다.
아래의 내용은 ML의 실용과 몇가지 팁 수업에 있는 python 코드를 R로 변경한 것이다.
소스는 lab-09-4-xor_tensorboard.py 이다.
아래의 내용은 tensorboard를 실행하는 예제이다.
훈련 단계에서 merged_summary에 값 설정 중 에러가 발생할 경우는 R session을 재시작 해야 한다.
소스는 lab-09-4-xor_tensorboard.py 이다.
아래의 내용은 tensorboard를 실행하는 예제이다.
훈련 단계에서 merged_summary에 값 설정 중 에러가 발생할 경우는 R session을 재시작 해야 한다.
library(tensorflow)
x_data = matrix(c(0, 0, 0, 1, 1, 0, 1, 1), byrow = 2, ncol = 2)
y_data = matrix(c(0, 1, 1, 0))
X = tf$placeholder(tf$float32, shape=list(NULL, 2))
Y = tf$placeholder(tf$float32, shape=list(NULL, 1))
# with 구문은 tensorboard에서 그래프 분리 용
with (tf$name_scope("Layer1"),{
W1 = tf$Variable(tf$random_normal(shape=list(2L, 10L)), name="weight1")
b1 = tf$Variable(tf$random_normal(shape=list(10L)), name="bias1")
layer1 = tf$sigmoid(tf$matmul(X, W1) + b1)
tf$summary$histogram("W1", W1)
tf$summary$histogram("b1", b1)
tf$summary$histogram("Layer1", layer1)
})
with (tf$name_scope("Layer2"),{
W2 = tf$Variable(tf$random_normal(shape=list(10L, 1L)), name="weight4")
b2 = tf$Variable(tf$random_normal(shape=list(1L)), name="bias4")
hypothesis = tf$sigmoid(tf$matmul(layer1, W2) + b2)
tf$summary$histogram("W2", W2)
tf$summary$histogram("b2", b2)
tf$summary$histogram("Hypothesis", hypothesis)
})
# cost/loss function
with (tf$name_scope("cost"),{
cost = - tf$reduce_mean(Y * tf$log(hypothesis) + (1 - Y) * tf$log(1 - hypothesis))
tf$summary$scalar("Cost", cost)
})
# cost/loss function
with (tf$name_scope("Train") %as% scope,{
train = tf$train$GradientDescentOptimizer(learning_rate=0.1)$minimize(cost)
})
# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf$cast(hypothesis > 0.5, dtype=tf$float32)
accuracy = tf$reduce_mean(tf$cast(tf$equal(predicted, Y), dtype=tf$float32))
sess <- tf$Session()
merged_summary = tf$summary$merge_all()
writer = tf$summary$FileWriter("/tmp/logs/xor_logs_r0_01")
writer$add_graph(sess$graph) # Show the graph
# Initialize TensorFlow variables
sess$run(tf$global_variables_initializer())
for (step in 1: 10001) {
stage = sess$run(list(merged_summary, cost, train), feed_dict=dict(X = x_data, Y = y_data))
summary <- stage[[1]]
writer$add_summary(summary, global_step=step)
if (step %% 100 == 0) {
cat("step:", step, "cost:", stage[[2]], "\n" )
}
}
writer$close()
# Accuracy report
pred = sess$run(list(hypothesis, predicted, accuracy), feed_dict=dict(X = x_data, Y = y_data))
names(pred) <- c("Hypothesis", "Correct", "Accuracy")
(pred)
sess$close()
# tensorboard 실행
# tensorboard(log_dir = "/tmp/logs/xor_logs_r0_01")
print("END OF CODE")
댓글 없음:
댓글 쓰기