TensorFlow was originally developed by researchers and engineers working on the Google Brain Team within Google’s Machine Intelligence research organization for the purposes of conducting machine learning and deep neural networks research, but the system is general enough to be applicable in a wide variety of other domains as well.
Here’s a simple example of making up some data in two dimensions and then fitting a line to it:
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
plot(x_data,y_data)
(We know that W should be 0.1 and b 0.3, but TensorFlow will figure that out for us.)
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.5)
train <- optimizer$minimize(loss)
sess = tf$Session()
sess$run(tf$global_variables_initializer())
(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")
}
## 20 - 0.1233591 0.2883742
## 40 - 0.1051201 0.2974517
## 60 - 0.1011223 0.2994414
## 80 - 0.100246 0.2998776
## 100 - 0.1000539 0.2999732
## 120 - 0.1000118 0.2999941
## 140 - 0.1000026 0.2999987
## 160 - 0.1000006 0.2999997
## 180 - 0.1000001 0.2999999
## 200 - 0.1000001 0.3
Y = W*x + b
x = seq(0,1,0.01)
w=0.1
b=0.3
y = w*x + b
plot(x,y)