Skip to content

Instantly share code, notes, and snippets.

@madhavtummala
Created October 5, 2017 02:04
Show Gist options
  • Save madhavtummala/f5035f42ef63c0daa613d1e051d93e3c to your computer and use it in GitHub Desktop.
Save madhavtummala/f5035f42ef63c0daa613d1e051d93e3c to your computer and use it in GitHub Desktop.
Linear Model Basic ML
import tensorflow as tf
#model parameters
W = tf.Variable([0.3], dtype=tf.float32)
b = tf.Variable([-0.3], dtype=tf.float32)
#model input and output
x = tf.placeholder(tf.float32)
model_output = W*x + b
y = tf.placeholder(tf.float32)
#error
error = tf.reduce_sum(tf.square(model_output - y))
#optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(error)
#training
x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(2080798758):
sess.run(train, {x: x_train, y: y_train})
#evaluate
final_W, final_b, final_error = sess.run([W, b, error],{x: x_train, y:y_train})
print("W: %s b: %s error: %s"%(final_W,final_b,final_error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment