I am exploring Linear Regression with Tensorflow. Here is my code from this notebook.
import tensorflow as tf
import numpy as np
learning_rate = 0.01
x_train = np.linspace(-1,1,101)
y_train = 2*x_train + np.random.randn(*x_train.shape) * 0.33
X = tf.placeholder("float")
Y = tf.placeholder("float")
def model(X, w):
return tf.multiply(X,w)
w = tf.Variable(0.0, name = "weights")
training_epochs = 100
y_model = model(X,w)
cost = tf.reduce_mean(tf.square(Y-y_model))
train_op = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
for epoch in range(training_epochs):
for (x,y) in zip(x_train,y_train):
sess.run(train_op, feed_dict = {X:x, Y: y})
print(sess.run(w))
It tries to minimize a cost function. According to this question's answers, I think tf.reduce_mean()
will work like np.mean()
.
However, every time a pair of (x,y)
is fed to the train_op
, the weight w
seems to update not according to THE pair but to all previous pairs.
What is the explanation for that? Is this related to working together with the optimizer?