Python ProgrammingPython Programming

Welcome to the fourth tutorial of TensorFlow tutorial series.

In the last tutorial we learnt what is tensors in TensorFlow. We also learnt how to create and process tensors using various functions.

Now we have a few tensors ready to be used, we can apply more interesting operations such as addition, multiplication, rounding or comparing.

TensorFlow Operations

TensorFlow brings all the tools for us to get set up with numerical calculations and adding such calculations to our graphs. TensorFlow has a list of methods for implementing mathematical calculations on tensors. Each method is represented by a function of the tf package, and each function returns a tensor.

This tutorial presents a large section of the tensor's available operations:

1) Arithmetic operations

In below example we perform addition, subtraction, multiplication, division and modulo operations on two tensors.

##
# TensorFlow program to perform basic tensor operations.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[5, 10, 15], [4, 8, 12], [6, 12, 18]])
t2 = tf.constant([[5, 5, 5], [2, 2, 2], [3, 3, 3]])
 
tensor_sum = tf.add(t1, t2)  # Add two tensors.
 
tensor_sub = tf.subtract(t1, t2)  # Subtract two tensors.
 
tensor_mul = tf.multiply(t1, t2)  # Multiples two tensors.
 
tensor_divide = tf.divide(t1, t2)  # Divides two tensors without roundoff.
 
tensor_div = tf.div(t1, t2)  # Divides two tensors with roundoff.
 
tensor_mod = tf.div(t1, t2)  # Find reminder the modulo operation.
 
sess = tf.Session()
 
print(sess.run(t1))
print(sess.run(t2))
 
print("\n----------- SUM ----------------\n")
print(sess.run(tensor_sum))
print("\n------------ SUBTRACT ----------\n")
print(sess.run(tensor_sub))
print("\n------------ MULTIPLY ----------\n")
print(sess.run(tensor_mul))
print("\n------------ DIVIDE ------------\n")
print(sess.run(tensor_divide))
print("\n------------ DIVIDE ------------\n")
print(sess.run(tensor_div))
print("\n------------ MODULO ------------\n")
print(sess.run(tensor_mod))
Sample output of above program, shown as below:
Basic mathematics operations

In above example both tensors t1 and t2 are of int32 type. Whenever your perform above operations make sure both tensors must have the same data-type (bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string). Other wise system will an throw exception of data-type mismatch e.g. "Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'."

2) Add Multiple Tensors

The function add_n function used to add multiple tensors of same shape and data type.

##
# TensorFlow program to add multiple tensors.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
 
t1 = tf.constant([[5, 5, 5], [2, 2, 2], [3, 3, 3]])
t2 = tf.constant([[1, 1, 1], [4, 4, 4], [6, 6, 6]])
t3 = tf.constant([[7, 7, 7], [9, 9, 9], [5, 5, 5]])
 
add_multiple_tensor = tf.add_n([t1, t2, t3], name="add_n")
 
sess = tf.Session()
 
print("\n----------- add_n ----------------\n")
print(sess.run(add_multiple_tensor))
 
Sample output of above program, shown as below:
Add multiple tensors

3) Find Square, Square-root and Round the values of a Tensor

The following source code snippet shows an example use of square, sqrt and round.

##
# TensorFlow program to sqaure sqrt and round a tensor.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
 
t1 = tf.constant([[5, 5, 5], [2, 2, 2], [3, 3, 3]])
square_tensor = tf.square(t1, "Square")
 
t2 = tf.constant([[150.0, 250.0, 350.0], [400, 800, 1200]])
sqrt_tensor = tf.sqrt(t2, "Sqrt")
 
round_tensor = tf.round(sqrt_tensor, "Round")
 
sess = tf.Session()
 
print("\n----------- Square ----------------\n")
print(sess.run(square_tensor))
 
print("\n----------- Sqrt ----------------\n")
print(sess.run(sqrt_tensor))
 
print("\n----------- Round ----------------\n")
print(sess.run(round_tensor))
 
Sample output of above program, shown as below:
Find Square, Square-root and Round the values of a Tensor

4) Maximum and Minimum Tensor

The maximum and minimum functions used to create a tensor with largest and smallest elements respectively by comparing two tensors. Both tensors must be of same data-type and size.

##
# TensorFlow program to create largest and smallest tensor.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1.5, 3.5, 4.5], [4, 4, 4], [3, 3, 3]])
t2 = tf.constant([[7.5, 7.5, 7.5], [2, 2, 2], [3, 3, 3]])
 
max_tensor = tf.maximum(t1, t2, "Largest")
min_tensor = tf.minimum(t1, t2, "Smallest")
 
sess = tf.Session()
 
print("\n----------- Maximum (Largest) ----------------\n")
print(sess.run(max_tensor))
 
print("\n----------- Minimum (Smallest) ----------------\n")
print(sess.run(min_tensor))
 
Sample output of above program, shown as below:
Find Square, Square-root and Round the values of a Tensor

5) Get index of smallest and greatest element of Tensor

The argmax and argmin functions used to get greatest and smallest element index of a given tensor.

##
# TensorFlow program to get greatest and smallest element position in tensor.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1.5, 3.5, 4.5], [4, 4, 4], [3, 3, 3]])
 
argmax_tensor = tf.argmax(t1)
argmin_tensor = tf.argmin(t1)
 
sess = tf.Session()
 
print("\n----------- argmax (Greatest) ----------------\n")
print(sess.run(argmax_tensor))
 
print("\n----------- argmin (Smallest) ----------------\n")
print(sess.run(argmin_tensor))
 
Sample output of above program, shown as below:
Get index of smallest and greatest element of Tensor

6) Calculate Ceil, Floor, Power, Exponential and Logarithm of a tensor

##
# TensorFlow program to calculate ceil, floor, power and logarithm.
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1.5, 3.5, 4.5], [4, 4, 4], [3, 3, 3]])
 
ceil_tensor = tf.ceil(t1)
floor_tensor = tf.floor(t1)
exp_tensor = tf.exp(t1)
pow_tensor = tf.pow(t1, 2)
log_tensor = tf.log(t1)
 
sess = tf.Session()
 
print("\n----------- (Ceil) ----------------\n")
print(sess.run(ceil_tensor))
 
print("\n----------- (Floor) ----------------\n")
print(sess.run(floor_tensor))
 
print("\n----------- (Exponential) ----------------\n")
print(sess.run(exp_tensor))
 
print("\n----------- (Power) ----------------\n")
print(sess.run(pow_tensor))
 
print("\n----------- (Logarithm) ----------------\n")
print(sess.run(log_tensor))
 
Sample output of above program, shown as below:
Calculate Ceil, Floor, Power, Exponential and Logarithm of a tensor

7) Examples of basic math functions

Basic math functions abs computes the absolute value of a tensor and negative computes numerical negative value element-wise.
sin, cos, tan computes respective basic trigonometric calculation on tensor element-wise.

##
# TensorFlow program to with example of basic Math Functions
 
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
 
import tensorflow as tf
 
t1 = tf.constant([[1.5, 3.5, 4.5], [4, 4, 4], [3, 3, 3]])
 
abs_tensor = tf.abs(t1)
negative_tensor = tf.negative(t1)
sign_tensor = tf.sign(t1)
 
sin_tensor = tf.sin(t1)
cos_tensor = tf.cos(t1)
tan_tensor = tf.tan(t1)
 
asin_tensor = tf.asin(sin_tensor)
acos_tensor = tf.acos(cos_tensor)
atan_tensor = tf.atan(tan_tensor)
 
sess = tf.Session()
 
print("\n----------- (abs) ----------------\n")
print(sess.run(abs_tensor))
 
print("\n----------- (negative) -----------\n")
print(sess.run(negative_tensor))
 
print("\n----------- (sign) ---------------\n")
print(sess.run(sign_tensor))
 
print("\n----------- (sin) ----------------\n")
print(sess.run(sin_tensor))
 
print("\n----------- (cos) ----------------\n")
print(sess.run(cos_tensor))
 
print("\n----------- (tan) ----------------\n")
print(sess.run(tan_tensor))
 
print("\n----------- (asin) ---------------\n")
print(sess.run(asin_tensor))
 
print("\n----------- (acos) ---------------\n")
print(sess.run(acos_tensor))
 
print("\n----------- (atan) ---------------\n")
print(sess.run(atan_tensor))
 
Sample output of above program, shown as below:
Examples of basic math functions