Computational Graph

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

  • Building the computational graph. A computational graph is nothing but a series of TensorFlow operations arranged into a graph of nodes.
  • Running the computational graph. To actually evaluate the nodes, we must run the computational graph within a session. A session encapsulates the control and state of the TensorFlow runtime.

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

C++




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


Python




# 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:

  • Step 1 : Create a computational graph By creating computational graph, we mean defining the nodes. Tensorflow provides different types of nodes for a variety of tasks. Each node takes zero or more tensors as inputs and produces a tensor as an output.
    • In above program, the nodes node1 and node2 are of tf.constant type. A constant node takes no inputs, and it outputs a value it stores internally. Note that we can also specify the data type of output tensor using dtype argument.
node1 = tf.constant(3, dtype=tf.int32)
node2 = tf.constant(5, dtype=tf.int32)
  • node3 is of tf.add type. It takes two tensors as input and returns their sum as output tensor.
node3 = tf.add(node1, node2)
  • Step 2 : Run the computational graph In order to run the computational graph, we need to create a session. To create a session, we simply do:
sess = tf.Session()
  • Now, we can invoke the run method of session object to perform computations on any node:
print("Sum of node1 and node2 is:",sess.run(node3))
  • Here, node3 gets evaluated which further invokes node1 and node2. Finally, we close the session using:
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.

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.

Table of Content

  • TensorFlow
  • How to install TensorFlow?
  • The Computational Graph
  • Variables
  • Placeholders
  • Linear Regression model using TensorFlow
  • tf.contrib.learn
  • What are TensorFlow APIs?

Similar Reads

TensorFlow

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

How to install TensorFlow?

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

Computational Graph

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

Variables

...

Placeholders

...

Linear Regression model using TensorFlow

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:...

tf.contrib.learn

...

What are TensorFlow APIs?

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:...