Created
December 6, 2016 13:44
-
-
Save hussius/a2a85121d20fd41a34ee2e0689b3be12 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tensorflow) | |
tf$reset_default_graph() | |
x_data <- runif(100, min=0, max=1) | |
y_data <- x_data * 0.1 + 0.3 + rnorm(n, mean=0, sd=0.025) | |
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0)) | |
b <- tf$Variable(tf$zeros(shape(1L))) | |
y <- W * x_data + b | |
lmfit <- lm(y_data~x_data) | |
plot(x_data, y_data) | |
abline(a=lmfit$coefficients[1], b=lmfit$coefficients[2],col="green") | |
# Minimize the mean squared errors. | |
loss <- tf$reduce_mean((y - y_data) ^ 2) | |
optimizer <- tf$train$GradientDescentOptimizer(0.5) | |
train <- optimizer$minimize(loss) | |
# Launch the graph and initialize the variables. | |
sess = tf$Session() | |
sess$run(tf$initialize_all_variables()) | |
# Fit the line (Learns best fit is W: 0.1, b: 0.3) | |
for (step in 1:201) { | |
sess$run(train) | |
if (step %% 20 == 0) | |
cat(step, "-", sess$run(W), sess$run(b), "\n") | |
b_ <- sess$run(b) | |
W_ <- sess$run(W) | |
plot(x_data,y_data) | |
abline(a=lmfit$coefficients[1], b=lmfit$coefficients[2],col="green") | |
lines(x_data, b_+W_*x_data, col="red") | |
Sys.sleep(0.1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment