tensorflow :: 기본 예제

Published onesixx on

 

 

library(ggplot2)
### DATA
x <- rnorm(n=1000, mean=0.0, sd=0.55)
y <- x* 0.1 + 0.3 + rnorm(n=1000, mean=0.0, sd=0.03)
gg <- ggplot() + geom_point(aes(x,y))

library(tensorflow)
use_virtualenv("~/.pyenv/versions/tensorflowVE/")

Sys.setenv(TENSORFLOW_PYTHON="~/python")

# Model
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y_hat <- W*x + b

# MSE(mean squared errors) 최소화
loss <- tf$reduce_mean(tf$square(y_hat-y))
optimizer <- tf$train$GradientDescentOptimizer(0.5)
train <- optimizer$minimize(loss)

# 그래프 Launch
sess <- tf$Session()
# 변수 초기화
init <- tf$global_variables_initializer()
sess$run(init)

# Fitting
for(step in 1:8) {
    sess$run(train)

    cat(step, "-", sess$run(W), sess$run(b), "\n")
}
# sess$run(W); sess$run(b)
gg + geom_abline(slope=sess$run(W), intercept = sess$run(b))

 

 

 

 

Categories: DL

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.