Skip to content

Commit

Permalink
implemneted gradient descent using tensorflow
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSomen committed Aug 11, 2018
1 parent 24923bb commit 096e05a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Tensorflow/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
#%%
X = tf.constant(housing_data_plus_bias, dtype=tf.float32,
name="X")
Y = tf.constant(housing.target.reshape(1,-1),dtype=tf.float32,
Y = tf.constant(housing.target.reshape(-1,1),dtype=tf.float32,
name='Y')
Y.shape
#%%
Expand All @@ -99,4 +99,24 @@
XT), Y)
with tf.Session() as sess:
theta_value = theta.eval()
print(theta_value)
print(theta_value)

#%%
n_epochs = 1000
learning_rate = 0.01
X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name="y")
theta = tf.Variable(tf.random_uniform([n + 1, 1], -1.0, 1.0), name="theta")
y_pred = tf.matmul(X, theta, name="predictions")
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name="mse")
gradients = 2/m * tf.matmul(tf.transpose(X), error)
training_op = tf.assign(theta, theta - learning_rate * gradients)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
if epoch % 100 == 0:
print("Epoch", epoch, "MSE =", mse.eval())
sess.run(training_op)
best_theta = theta.eval()

0 comments on commit 096e05a

Please sign in to comment.