Tensor and their Fundamentals

Tensor and their Fundamentals

In the previous article, we have read in-depth about TensorFlow history, how it works, Architecture, and algorithm supported by Tensorflow. If you haven't read the article here's the link.

As the preparations for the conference gain momentum let’s move towards other exciting facets of Tensor.

In this Tensor and their fundamentals tutorial, we are going to cover the following:

  1. Tensorflow Installation
  2. Tensor Fundamentals
  3. What is Tensors
  4. Tensor Data Type
  5. Tensor Rank
  6. Tensorflow sessions
  7. Computation Graph
  8. Tensor Basic Operations (add, multiply..)
  9. Programming Elements in Tensorflow

Tensorflow Installation

TensorFlow is tested and supported on almost every 64-bit system. You can use Python Package manager or use the TensorFlow docker container.

Using Package Manager

Official packages available for Ubuntu, Windows, macOS, and the Raspberry Pi.

Note: Please create a Python environment before installing any of the packages. You can read here the benefits of using virtualenv

# Requires the latest pip
pip install --upgrade pip

# Current stable release for CPU and GPU
pip install tensorflow

# Or try the preview build (unstable)
pip install tf-nightly

Using docker container

The TensorFlow Docker images are already configured to run TensorFlow. A Docker container runs in a virtual environment and is the easiest way to set up GPU support.

docker pull tensorflow/tensorflow:latest  # Download latest stable image
docker run -it -p 8888:8888 tensorflow/tensorflow:latest-jupyter  # Start Jupyter server

If you stuck anywhere in installation, drop a here in TensorFlow community. We are here to help anytime 😊️.

Tensor Fundamentals

First, we’re going to take a look at the tensor object type. Then we’ll have a graphical understanding of TensorFlow to define computations. Finally, we’ll run the graphs with sessions, showing how to substitute intermediate values.

What is Tensors

In TensorFlow, data isn't stored as integers, floats, or strings. These values are encapsulated in an object called a tensor, a fancy term of multi-dimensional arrays.

In definition: A tensor is a mathematical object represented as arrays of higher dimensions. These arrays of data with different sizes and ranks get fed as input to the neural network. These are the tensors.

TensorFlowTutorial-1.jpg

You can have arrays or vectors, which are one-dimensional, or matrices, which are two-dimensional. But tensor can be more than three, four, or five-dimensional.

Let us look at an example of a tensor of [5,4] dimensions (two-dimensional).

TensorFlowTutorial-2.jpg

Next, you can see a tensor of dimensions [3,3,3] (three-dimensional).

TensorFlowTutorial-3.jpg

import tensorflow as tf

# You can create constants in TF to hold specific values
A = tf.constant(1234) 

# B is a 1-dimensional int32 tensor
B = tf.constant([123,456,789]) 

# C is a 2-dimensional int32 tensor
C = tf.constant([ [123,456,789], [222,333,444] ])

Tensor Data Type

In addition to shape and dimension, tensors also have a data type. It support from int8 to float32 as well as Boolean and string type.

The following is a list of data type:

TensorFlowTutorial-4.jpg

Tensor Rank

Tensor rank is nothing but the dimension of the tensor. It starts with zero. Zero is a scalar that doesn't have multiple entries in it. It's a single value.

For example:

s = 10 is tensor of rank 0 or a scalar.
V = [10,11,12] is tensor of rank 1 or a vector
M = [[1,3,4], [4,5,6]] is tensor of rank 2 or a matrix
T = [[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]] is a tensor of rank 3 or a tensor.

Tensorflow Sessions

Everything so far just specified the TensorFlow graph. We haven't yet computed anything. To do this, we need to start a session in which the computations will take place.

To do so, we need to create a new session Session() which is the TensorFlow inbuilt function to do so.

sess = tf.Session()

TensorFlow’s API is built around the idea of a computational graph, a way of visualizing a mathematical process.

Running a session and evaluating a tensor

The code itself creates a new session instance using tf.Session(). The sess.run() function then evaluates the tensor and returns the results.

Once you have a session open, sess.run() will evaluate the given expression and return the result of the computation.

Let's see by an hello world example:

import tensorflow as tf

# Create TensorFlow object called hello_constant
hello_constant = tf.constant('Hello World!')

with tf.Session() as sess:
    # Run the tf.constant operation in the session
    output = sess.run(hello_constant)
    print(output)

#Output
Hello World

Computation Graph

Everything in TensorFlow is based on designing a computational graph. The graph has a network of nodes, with each node operating addition, multiplication, or evaluating some multivariate equation.

Let’s dive a bit more into a computational graph and understand how computation graphs are formed,

Consider a simple computation. Let a, b, c be the variables used,

J = 3 * ( a + bc )

There are 3 distinctive computational steps to arrive at the final value J, let’s list them out,

u = b * c
v = (a + u)
J  = 3 * v

These are 3 computational steps which can be represented as a graph,

1_0pNy655CFzFoeYCO_oTMUw.png

Here are the advantages of organizing the computations as a graph,

  • Parallelism
  • Distributed execution
  • Compilation

1_SmfhKWHXHVEMg8KqNaj-uw.gif

Tensorflow Placeholder

tf.placeholder() returns a tensor that gets its value from data passed to the tf.session.run() function, allowing you to set the input right before the session runs.

The syntax is:
tf.placeholder(dtype,shape=None,name=None )
arguments:
- `dtype`: Type of data
- `shape`: dimension of the placeholder. Optional. By default, shape of the data
- `name`: Name of the placeholder. Optional            
data_placeholder_a = tf.placeholder(tf.float32, name = "data_placeholder_a")

Let’s revisit our Hello world example, but this time let’s set the value right before the session evaluates the tensor.

import tensorflow as tf
x = tf.placeholder(tf.string)

with tf.Session() as sess:
    output = sess.run(x, feed_dict={x: 'Hello World'})

Use the feed_dict parameter in tf.session.run() to set the placeholder tensor. The above example shows the tensor x is set to the string "Hello, world".

Tensorflow basic operations

Now we have covered almost thing to get started for basic operations in TensorFlow.

1. Addition

x = tf.add(5, 2)  # 7

You’ll start with the add function. The tf.add() function does exactly what you expect it to do.

2. Subtraction and Multiplication

Here’s an example with subtraction and multiplication.

x = tf.subtract(10, 4) # 6
y = tf.multiply(2, 5)  # 10

3. More Basic Operation

Following is a list of commonly used operations. The idea is the same as above. Each operation requires one or more arguments.

  • tf.div(a,b)
  • tf.pow(a,b)
  • tf.exp(a)
  • tf.sqrt(a)

Programming Elements in Tensorflow

Unlike other programming languages, TensorFlow allows you to assign data to three different data elements:

  1. Constants
  2. Variables
  3. Placeholders

Constants

Constants are parameters with values that do not change. We use the tf.constant() command to define a constant.

a = tf.constant(2.0, tf.float32)

Variables

Variables allow us to add new trainable parameters to the graph. To define a variable, we use the tf.Variable() command and initialize them before running the graph in a session.

W = tf.Variable([.3],dtype=tf.float32)
b = tf.Variable([-.3],dtype=tf.float32)
x = tf.placeholder(tf.float32)

linear_model = W*x+b

Placeholders

Placeholders allow us to feed data to a TensorFlow model from outside a model. It permits value to be assigned later. To define a placeholder, we use the tf.placeholder() command.

a = tf.placeholder(tf.float32)
b = a*2
with tf.Session() as sess:
         result = sess.run(b,feed_dict={a:3.0})

Next: In the next tutorial, we will read about Linear regression, hypothesis, cost function, and predicting house price using Tensorflow.