Basic Building Blocks of TensorFlow: Basic Operations with Tensors

Basic Building Blocks of TensorFlow: Basic Operations with Tensors

As per Wikipedia, here is the definition of Tensors: 

“In mathematics, a tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects related to a vector space. Tensors may map between different objects such as vectors, scalars, and even other tensors. There are many types of tensors, including scalars and vectors (which are the simplest tensors), dual vectors, multilinear maps between vector spaces, and even some operations such as the dot product.”

I am assuming you are familiar with the Numpy arrays that can be scalar or vectors. That means that can be single dimensional or multidimensional. Tensors have those same properties as well. 

In Tensorflow, we have very well composed functionalities and we do not need to deal with any low-level operations. But it is always a good idea to learn some basic operations to better understand the library. So, this tutorial will give you some very basic idea of the Tensors. 

All the operations are pretty self explanatory. So I will move pretty fast here. First, I will do the necessary imports and generate a Numpy array or a range from 0 to 9:

import tensorflow as tf
import numpy as np
x = np.arange(12)
x

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

Making this Numpy array into a tensor:

x = tf.constant(x)
x

Output:

<tf.Tensor: shape=(12,), dtype=int64, 
numpy=array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])>

Applying tf.square() to square all the elements here:

x = tf.square(x)
x

Output:

<tf.Tensor: shape=(12,), dtype=int64, 
numpy=array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121])>

We can reshape this tensor to make it two-dimensional:

x= tf.reshape(x, shape = (3, 4))
x

Output:

<tf.Tensor: shape=(3, 4), dtype=int64, numpy=
array([[ 0, 1, 4, 9],
[ 16, 25, 36, 49],
[ 64, 81, 100, 121]])>

All the data in x are integers. Let’s see how to change them to floats:

x= tf.cast(x, tf.float32)
x

Output:

<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0., 1., 4., 9.],
[ 16., 25., 36., 49.],
[ 64., 81., 100., 121.]], dtype=float32)>

We can practice some mathematical operations on tensors. For that creating a new tensor y:

y = tf.constant(5, dtype = tf.float32)
y

Output:

<tf.Tensor: shape=(), dtype=float32, numpy=5.0>

If we multiply x with y, each element in x will be multiplied by y as y is a scaler, pretty much like the broadcasting concept in Numpy.

tf.multiply(x, y)

Output:

<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 0., 5., 20., 45.],
[ 80., 125., 180., 245.],
[320., 405., 500., 605.]], dtype=float32)>

In the same way, we can perform addition of y to x:

tf.add(x, y)

Output:

<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 5., 6., 9., 14.],
[ 21., 30., 41., 54.],
[ 69., 86., 105., 126.]], dtype=float32)>

If you have two tensors of same shape, you can do an elementwise operation as well. Let’s create a new tensor of 3×4 shape to experiment with that:

y = tf.constant([1, 2, 3, 4, 5, 6, 7, 9, 8, 7, 6, 5], dtype=tf.float32)
y = tf.reshape(y, shape=(3, 4))
y

Output:

<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 2., 3., 4.],
[5., 6., 7., 9.],
[8., 7., 6., 5.]], dtype=float32)>

Adding y to x now to do an element wise addition as x and y both are of same shape:

x+y

Output:

<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[ 1., 3., 7., 13.],
[ 21., 31., 43., 58.],
[ 72., 88., 106., 126.]], dtype=float32)>

I hope this gives the idea about the tensors. 

Conclusion

This article was to provide a basic idea about the tensors and some basic operations of tensors which is the basic building blocks of the TensorFlow. I wrote so many tutorials on deep learning with TensorFlow. I thought it is now necessary to provide some basic idea about the low level operations. 

Please feel free to follow me on Twitter, and the Facebook page, and check out my YouTube channel.

Leave a Reply

Close Menu