Size in Feet(x) | Price($ in 1000s) (Y) |
---|---|
1000 | 450 |
1500 | 520 |
850 | 340 |
... | ... |
y=mx + c
where m is the slope and c are the constant.In the linear regression model, slope will become the Weight and the constant will act as bias. The basic model will be ho=Weight*x + bias
. where ,
import numpy as np
import tensorflow as tf
# declaring and initializing Weights and bias
Weight=tf.Variable([.3], dtype=tf.float32)
bias=tf.Variable([-.3], dtype=tf.float32)
# defining x and y parameters and making it placeholder since at first we don't know it's value
x=tf.placeholder(tf.float32)
linear_model=Weight * x + bias
y=tf.placeholder(tf.float32)
Now, we need to predict Weight and bias. To predict the Weight and bias, we will use the training data.
`J(Weight,bias)=(1/(2m))\sum_{i=1}^m (h_o(x^i) -y^i)^2`
We can measure the accuracy of our hypothesis function by using a cost function. This takes average difference of all results of the hypothesis with inputs from x's and the actual output y's
# In order to calulate loss
loss=tf.reduce_sum(tf.square(linear_model - y)) # calculates sum of the squares
# optimizer
optimizer=tf.train.GradientDescentOptimizer(0.01)
train=optimizer.minimize(loss)
# training values
x_trainingData=[1,2,3,4]
y_trainingData=[0,-1,-2,-3]
# training
init=tf.global_variables_initializer() # To initialize all Variable
sess=tf.Session()
sess.run(init) # reset values to wrong
# loop
for i in range(1000):
sess.run(train,{x:x_trainingData, y:y_trainingData})
curr_Weight, curr_bias, curr_loss=sess.run([Weight, bias, loss],{x:x_trainingData, y:y_trainingData})
print("W: %s b: %s loss: %s"%(curr_Weight, curr_bias, curr_loss))
We need top get weight, bias and loss, and print accordinly.
import numpy as np
import tensorflow as tf
# declaring and initializing Weights and bias
Weight=tf.Variable([.3], dtype=tf.float32)
bias=tf.Variable([-.3], dtype=tf.float32)
# defining x and y parameters and making it placeholder since at first we don't know it's value
x=tf.placeholder(tf.float32)
linear_model=Weight * x + bias
y=tf.placeholder(tf.float32)
# In order to calulate loss
loss=tf.reduce_sum(tf.square(linear_model - y)) # calculates sum of the squares
# optimizer
optimizer=tf.train.GradientDescentOptimizer(0.01)
train=optimizer.minimize(loss)
# training values
x_trainingData=[1,2,3,4]
y_trainingData=[0,-1,-2,-3]
# training
init=tf.global_variables_initializer() # To initialize all Variable
sess=tf.Session()
sess.run(init) # reset values to wrong
# loop
for i in range(1000):
sess.run(train,{x:x_trainingData, y:y_trainingData})
curr_Weight, curr_bias, curr_loss=sess.run([Weight, bias, loss],{x:x_trainingData, y:y_trainingData})
print("W: %s b: %s loss: %s"%(curr_Weight, curr_bias, curr_loss))
W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11