lab2

Published by onesixx on

https://tensorflow.rstudio.com/

https://github.com/hunkim/DeepLearningZeroToAll/blob/master/lab-02-1-linear_regression.py

library(tensorflow)
# Create 100 phony x, y data points, y = x * 0.1 + 0.3
# x_data <- runif(100, min=0, max=1)
# y_data <- x_data * 0.1 + 0.3

Advertising <- read.table("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv",header=T, sep=",")
x_data <- Advertising$TV
y_data <- Advertising$Sales

# Try to find values for W and b that compute y_data = W * x_data + b
W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b

# Minimize the mean squared errors.
loss <- tf$reduce_mean((y - y_data) ^ 2)
optimizer <- tf$train$GradientDescentOptimizer(0.000034)
train <- optimizer$minimize(loss)

# Launch the graph and initialize the variables.
sess = tf$Session()
sess$run(tf$global_variables_initializer())

# Fit the line (Learns best fit is W: 0.1, b: 0.3)
# for (step in 1:365000) {
#   sess$run(train)
#   if (step %% 300 == 0)
#     #cat(step, "|cost:", sess$run(loss), "|b:", sess$run(b),"|W:", sess$run(W), "\n")
#     cat(step,"|",sess$run(loss),"|",sess$run(b),"|",sess$run(W),"\n")
# }


(fit.a <- lm(y_data ~ x_data))

########################################################################################

capture.output( 
    for (step in 1:365000) {
      sess$run(train)
      if (step %% 300 == 0)
        #cat(step, "|cost:", sess$run(loss), "|b:", sess$run(b),"|W:", sess$run(W), "\n")
        cat(step,"|",sess$run(loss),"|",sess$run(b),"|",sess$run(W),"\n")
    },
file = "foo.txt")


library(rgl)
FOO <- read.table("./foo.txt",header=FALSE, sep="|")

plot3d( 
    FOO$V3, FOO$V4, FOO$V2,
    xlab="b", ylab="W", zlab="LOSS", type = "p",
    lwd = 100, col = c("red","blue"), radius = 0.1
)                      
rglwidget()

 

Categories: DeepLearning

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x