Open In App

Introduction to TensorFlow

TensorFlow is an open-source machine learning library developed by Google. TensorFlow is used to build and train deep learning models as it facilitates the creation of computational graphs and efficient execution on various hardware platforms. The article provides an comprehensive overview of tensorflow.

TensorFlow

TensorFlow is basically a software library for numerical computation using data flow graphs where:

Consider the diagram given below: Here, add is a node which represents addition operation. a and b are input tensors and c is the resultant tensor. This flexible architecture allows you to deploy computation to one or more CPUs or GPUs in a desktop, server, or mobile device with a single API!



How to install TensorFlow?

An easy-to-follow guide for TensorFlow installation is available.

Here you can refer to this article – How to Install Python Tensorflow in Windows?

Once you can ensure a successful installation by running this command in python interpreter:

import tensorflow as tf

Computational Graph

Any TensorFlow Core program can be divided into two discrete sections:

Now, let us write our very first TensorFlow program to understand above concept: 




#include <iostream>
using namespace std;
 
int main() {
 
    cout << "GFG!";
    return 0;
}




# importing tensorflow
import tensorflow as tf
 
# creating nodes in computation graph
node1 = tf.constant(3, dtype=tf.int32)
node2 = tf.constant(5, dtype=tf.int32)
node3 = tf.add(node1, node2)
 
# create tensorflow session object
sess = tf.compat.v1.Session()
 
# evaluating node3 and printing the result
print("sum of node1 and node2 is :",sess.run(node3))
# closing the session
sess.close()

Output:

Sum of node1 and node2 is: 8

Let us try to understand above code:

node1 = tf.constant(3, dtype=tf.int32)
node2 = tf.constant(5, dtype=tf.int32)
node3 = tf.add(node1, node2)
sess = tf.Session()
print("Sum of node1 and node2 is:",sess.run(node3))
sess.close()

Note: Another(and better) method of working with sessions is to use with block like this:

with tf.Session() as sess:
print("Sum of node1 and node2 is:",sess.run(node3))

The benefit of this approach is that you do not need to close the session explicitly as it gets automatically closed once control goes out of the scope of with block.

Variables

TensorFlow has Variable nodes too which can hold variable data. They are mainly used to hold and update parameters of a training model. Variables are in-memory buffers containing tensors. They must be explicitly initialized and can be saved to disk during and after training. You can later restore saved values to exercise or analyze the model. An important difference to note between a constant and Variable is:

A constant’s value is stored in the graph and its value is replicated wherever the graph is loaded. A variable is stored separately, and may live on a parameter server.

Given below is an example using Variable




# importing tensorflow
import tensorflow as tf
 
# creating nodes in computation graph
node = tf.Variable(tf.zeros([2,2]))
 
# running computation graph
with tf.Session() as sess:
 
    # initialize all global variables
    sess.run(tf.global_variables_initializer())
 
    # evaluating node
    print("Tensor value before addition:\n",sess.run(node))
 
    # elementwise addition to tensor
    node = node.assign(node + tf.ones([2,2]))
 
    # evaluate node again
    print("Tensor value after addition:\n", sess.run(node))

Output:

Tensor value before addition:
[[ 0. 0.]
[ 0. 0.]]
Tensor value after addition:
[[ 1. 1.]
[ 1. 1.]]

In above program:

node = tf.Variable(tf.zeros([2,2]))
sess.run(tf.global_variables_initializer())
node = node.assign(node + tf.ones([2,2]))

Placeholders

A graph can be parameterized to accept external inputs, known as placeholders. A placeholder is a promise to provide a value later. While evaluating the graph involving placeholder nodes, a feed_dict parameter is passed to the session’s run method to specify Tensors that provide concrete values to these placeholders. Consider the example given below: 




# importing tensorflow
import tensorflow as tf
 
# creating nodes in computation graph
a = tf.placeholder(tf.int32, shape=(3,1))
b = tf.placeholder(tf.int32, shape=(1,3))
c = tf.matmul(a,b)
 
# running computation graph
with tf.Session() as sess:
    print(sess.run(c, feed_dict={a:[[3],[2],[1]], b:[[1,2,3]]}))

Output:

[[3 6 9]
[2 4 6]
[1 2 3]]

Let us try to understand above program:

a = tf.placeholder(tf.int32, shape=(3,1))
b = tf.placeholder(tf.int32, shape=(1,3))
c = tf.matmul(a,b)
print(sess.run(c, feed_dict={a:[[3],[2],[1]], b:[[1,2,3]]}))

Linear Regression model using TensorFlow

Given below is an implementation of a Linear Regression model using TensorFlow Core API. 




# importing the dependencies
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
 
# Model Parameters
learning_rate = 0.01
training_epochs = 2000
display_step = 200
 
# Training Data
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
n_samples = train_X.shape[0]
 
# Test Data
test_X = np.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
test_y = np.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
 
# Set placeholders for feature and target vectors
X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
 
# Set model weights and bias
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
 
# Construct a linear model
linear_model = W*X + b
 
# Mean squared error
cost = tf.reduce_sum(tf.square(linear_model - y)) / (2*n_samples)
 
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
 
# Initializing the variables
init = tf.global_variables_initializer()
 
# Launch the graph
with tf.Session() as sess:
    # Load initialized variables in current session
    sess.run(init)
 
    # Fit all training data
    for epoch in range(training_epochs):
 
        # perform gradient descent step
        sess.run(optimizer, feed_dict={X: train_X, y: train_y})
         
        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, y: train_y})
            print("Epoch:{0:6} \t Cost:{1:10.4} \t W:{2:6.4} \t b:{3:6.4}".
                  format(epoch+1, c, sess.run(W), sess.run(b)))
             
    # Print final parameter values
    print("Optimization Finished!")
    training_cost = sess.run(cost, feed_dict={X: train_X, y: train_y})
    print("Final training cost:", training_cost, "W:", sess.run(W), "b:",
          sess.run(b), '\n')
     
    # Graphic display
    plt.plot(train_X, train_y, 'ro', label='Original data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()
 
    # Testing the model
    testing_cost = sess.run(tf.reduce_sum(tf.square(linear_model - y)) / (2 * test_X.shape[0]),
                            feed_dict={X: test_X, y: test_y})
     
    print("Final testing cost:", testing_cost)
    print("Absolute mean square loss difference:", abs(training_cost - testing_cost))
 
    # Display fitted line on test data
    plt.plot(test_X, test_y, 'bo', label='Testing data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

Output:

Epoch:   200      Cost:    0.1715      W: 0.426      b:-0.4371
Epoch: 400 Cost: 0.1351 W:0.3884 b:-0.1706
Epoch: 600 Cost: 0.1127 W:0.3589 b:0.03849
Epoch: 800 Cost: 0.09894 W:0.3358 b:0.2025
Epoch: 1000 Cost: 0.09047 W:0.3176 b:0.3311
Epoch: 1200 Cost: 0.08526 W:0.3034 b:0.4319
Epoch: 1400 Cost: 0.08205 W:0.2922 b:0.5111
Epoch: 1600 Cost: 0.08008 W:0.2835 b:0.5731
Epoch: 1800 Cost: 0.07887 W:0.2766 b:0.6218
Epoch: 2000 Cost: 0.07812 W:0.2712 b: 0.66
Optimization Finished!
Final training cost: 0.0781221 W: 0.271219 b: 0.65996
Final testing cost: 0.0756337
Absolute mean square loss difference: 0.00248838

Let us try to understand the above code.

learning_rate = 0.01
training_epochs = 2000
display_step = 200
X = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
linear_model = W*X + b
cost = tf.reduce_sum(tf.square(linear_model - y)) / (2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
sess.run(optimizer, feed_dict={X: train_X, y: train_y})
c = sess.run(cost, feed_dict={X: train_X, y: train_y})
testing_cost = sess.run(tf.reduce_sum(tf.square(linear_model - y)) / (2 * test_X.shape[0]),
feed_dict={X: test_X, y: test_y})

tf.contrib.learn

tf.contrib.learn is a high-level TensorFlow library that simplifies the mechanics of machine learning, including the following:

Let us try to see the implementation of Linear regression on same data we used above using tf.contrib.learn




# importing the dependencies
import tensorflow as tf
import numpy as np
 
# declaring list of features
features = [tf.contrib.layers.real_valued_column("X")]
 
# creating a linear regression estimator
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
 
# training and test data
train_X = np.asarray([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,7.997,5.654,9.27,3.1])
train_y = np.asarray([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
                         2.827,3.465,1.65,2.904,2.42,2.94,1.3])
test_X = np.asarray([6.83, 4.668, 8.9, 7.91, 5.7, 8.7, 3.1, 2.1])
test_y = np.asarray([1.84, 2.273, 3.2, 2.831, 2.92, 3.24, 1.35, 1.03])
 
# function to feed dict of numpy arrays into the model for training
input_fn = tf.contrib.learn.io.numpy_input_fn({"X":train_X}, train_y,
                                              batch_size=4, num_epochs=2000)
 
# function to feed dict of numpy arrays into the model for testing
test_input_fn = tf.contrib.learn.io.numpy_input_fn({"X":test_X}, test_y)
 
# fit training data into estimator
estimator.fit(input_fn=input_fn)
 
# print value of weight and bias
W = estimator.get_variable_value('linear/X/weight')[0][0]
b = estimator.get_variable_value('linear/bias_weight')[0]
print("W:", W, "\tb:", b)
 
# evaluating the final loss
train_loss = estimator.evaluate(input_fn=input_fn)['loss']
test_loss = estimator.evaluate(input_fn=test_input_fn)['loss']
print("Final training loss:", train_loss)
print("Final testing loss:", test_loss)

Output:

W: 0.252928     b: 0.802972
Final training loss: 0.153998
Final testing loss: 0.0777036

Let us try to understand the above code.

features = [tf.contrib.layers.real_valued_column("X")]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)
input_fn = tf.contrib.learn.io.numpy_input_fn({"X":train_X}, 
train_y, batch_size=4, num_epochs=2000)
estimator.fit(input_fn=input_fn)
W = estimator.get_variable_value('linear/X/weight')[0][0]
b = estimator.get_variable_value('linear/bias_weight')[0]
train_loss = estimator.evaluate(input_fn=input_fn)['loss']
test_loss = estimator.evaluate(input_fn=test_input_fn)['loss']

What are TensorFlow APIs?

TensorFlow provides multiple APIs (Application Programming Interfaces). These can be classified into 2 major categories:

Low level API:

High level API:


Article Tags :