From 096e05abf215d8d442e2d61c8c3b3056c8c5e51e Mon Sep 17 00:00:00 2001 From: LordSomen Date: Sat, 11 Aug 2018 12:28:42 +0530 Subject: [PATCH] implemneted gradient descent using tensorflow --- Tensorflow/tf.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Tensorflow/tf.py b/Tensorflow/tf.py index c33d56f..5fbe1bf 100644 --- a/Tensorflow/tf.py +++ b/Tensorflow/tf.py @@ -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 #%% @@ -99,4 +99,24 @@ XT), Y) with tf.Session() as sess: theta_value = theta.eval() - print(theta_value) \ No newline at end of file + 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() \ No newline at end of file