読者です 読者をやめる 読者になる 読者になる

RでTensorFlow

data(iris)
mydata <- subset(iris,Species=="setosa")
x_data <- mydata[["Sepal.Length"]]
y_data <- mydata[["Sepal.Width"]]
glm(y_data~x_data)
Call:  glm(formula = y_data ~ x_data)

Coefficients:
(Intercept)       x_data  
    -0.5694       0.7985  

Degrees of Freedom: 49 Total (i.e. Null);  48 Residual
Null Deviance:      7.041 
Residual Deviance: 3.159        AIC: 9.8
library(tensorflow)
data(iris)
mydata <- subset(iris,Species=="setosa")
x_data <- mydata[["Sepal.Length"]]
y_data <- mydata[["Sepal.Width"]]

W <- tf$Variable(tf$random_uniform(shape(1L), -1.0, 1.0))
b <- tf$Variable(tf$zeros(shape(1L)))
y <- W * x_data + b

loss <- tf$reduce_mean((y - y_data) ^ 2)
optimizer <- tf$train$GradientDescentOptimizer(0.02)
train <- optimizer$minimize(loss)

sess = tf$Session()
sess$run(tf$global_variables_initializer())

for (step in 1:30000) {
  sess$run(train)
  if (step %% 5000 == 0)
    cat(step, "-", sess$run(W), sess$run(b), "\n")
}
5000 - 0.7517334 -0.3340828 
10000 - 0.7800725 -0.4766114 
15000 - 0.7912492 -0.5328233 
20000 - 0.7956573 -0.5549932 
25000 - 0.7973959 -0.5637373 
30000 - 0.7980812 -0.567184