TensorFlow tutorial 1.0

In [1]:

'''This is a modified version of Google's TensorFlow(TF) Tutorial. Here we elaborate the tutorial little more to simplify the learning.. about the TF variable, placeholder and linear optimization model.
-- Mohendra Roy
'''

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
sess = tf.Session()

# initial variable for weights and biases
w = tf.Variable(3.0)
b = tf.Variable(4.0)
init = tf.global_variables_initializer()  # handler to initiate the global variable
sess.run(init)
wini = sess.run(w)
bini = sess.run(b)

# initial placeholder for input data and labels
x = tf.placeholder(tf.float32)  # for input data
y = tf.placeholder(tf.float32)  # for label

# linear model
lm = x*w + b

# Setting the desired goal
loss = tf.reduce_sum(tf.square(lm - y))
lossini = sess.run(loss, {x: [1, 2, 3], y: [0, -1, -2]})

''' Our desired goal is to reduce the difference between the training input and training label '''

''' To achieve this we will use a optimization method using the Gradient Descent Optimizer algorithm '''

# Optimization with Gradient Descent Optimizer

op = tf.train.GradientDescentOptimizer(0.01)  # The number here is the step at which the optimizer will descent
ob = op.minimize(loss)  # The objective(ob) is to minimize the loss

''' Our main objective is to reduce the loss, i.e. the difference between the training input and label '''

''' Now we will  train our model in a loop to get the minimum loss by updating weight and biases '''

# Training loop
for i in range(1000):
    sess.run(ob, {x: [1, 2, 3], y: [0, -1, -2]})


wfi = sess.run(w)  # final weight after training
bfi = sess.run(b)  # final bias after training
lossfi = sess.run(loss, {x: [1, 2, 3], y: [0, -1, -2]})  # loss after the training

print("Initial weight: %s, Bias: %s, Loss: %s " % (wini, bini, lossini))  # The weight, bias and loss before training

print("Final weight: %s, Bias: %s, Loss: %s " % (wfi, bfi, lossfi))  # The weight, bias and loss after training

''' Till this we just train our 1st linear model. However we want to test the model too  '''


# Testing of the model
xin = 2
y = sess.run(lm, {x: xin})
print("The calculated test output for test input  %x is = " % xin, y)
Initial weight: 3.0, Bias: 4.0, Loss: 395.0 
Final weight: -1.00033, Bias: 1.00075, Loss: 2.40735e-07 
The calculated test output for test input  2 is =  -0.99991

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *