Tensor and Tensor Operations in TensorFlow 2.0

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

[latexpage]

In this chapter, you will get introduced to tensors and their operations in TensorFlow 2.0. A tensor is a vector (or matrix) of n-dimensions where all elements have the same data type. In other words, a tensor is a collection of feature vectors (i.e., arrays) of n-dimensions. Some of the examples of tensors are:

$\left[[ 1] \ \ [ 2]\right]$      
$\left[[ 1 \ \ 2] \ [ 3 \ \ 4]\right]$      
$\left[[ 1 \ \ 2 \ \ 3] \ [ 4 \ \ 5 \ \ 6]\right]$      

TensorFlow 2.0 supports the following 4 types of tensors: Variable, Constant, Placeholder and SparseTensor. Tensor objects will have the following three properties:

  • Data (numpy)
  • Dimension (shape)
  • Data type (dtype)

Except for the Variable, all other tensor types are immutable, i.e., their value will not change during opertions. Here, we will focus primarily on the Variable and Constant tensors.

Creating Tensors in TensorFlow 2.0

While creating a tensor object, we need to specify the Data and Data Type.

var = tf.Variable([1, 2], tf.int32)             # Variable Tensor of data type int32
const = tf.constant([1.1, 2.2], tf.float64)     # Constant Tensor of data type float64

print(f'Variable: {var}')                       # Print Variable
print(f'Contant: {const}')                      # Print Constant

print(f'\nData of Variable: {var.numpy()}')     # Print Data of Variable as NumPy array
print(f'Data of Constant: {const.numpy()}')     # Print Data of Constant as NumPy array

print(f'\nShape of Variable: {var.shape}')      # Print Shape of Variable as a tuple
print(f'Shape of Constant: {const.shape}')      # Print Shape of Constant as a tuple

print(f'\nData type of Variable: {var.dtype}')  # Print data type of Variable
print(f'Data type of Constant: {const.dtype}')  # Print data type of Constant
Variable: <tf.Variable 'Variable:0' shape=(2,) dtype=int32, numpy=array([1, 2], dtype=int32)>
Contant: [1.1 2.2]

Data of Variable: [1 2]
Data of Constant: [1.1 2.2]

Shape of Variable: (2,)
Shape of Constant: (2,)

Data type of Variable: <dtype: 'int32'>
Data type of Constant: <dtype: 'float64'>

Tensor Operations in TensorFlow 2.0

Various arithmetic operations such as addition, subtraction, multiplication, etc. can be performed on tensors.

# Taking two tensors of type int32
tensor_a = tf.Variable([[1,2]], dtype = tf.int32)
tensor_b = tf.Variable([[3, 4]], dtype = tf.int32)

# Arithmetic Operations
tensor_add = tf.add(tensor_a, tensor_b)             # a + b
print(f'Addition: {tensor_add}')

tensor_sub = tf.subtract(tensor_b, tensor_a)        # b - a
print(f'Subtraction: {tensor_sub}')

tensor_mul = tf.multiply(tensor_a, tensor_b)        # a * b
print(f'Multiplication: {tensor_mul}')

tensor_div = tf.divide(tensor_a, tensor_b)          # a / b
print(f'Division: {tensor_div}')

tensor_pow = tf.pow(tensor_a, tensor_b)             # a ^ b
print(f'Power: {tensor_pow}')
Addition: [[4 6]]
Subtraction: [[2 2]]
Multiplication: [[3 8]]
Division: [[0.33333333 0.5       ]]
Power: [[ 1 16]]

Tensors can also be reshaped similar to NumPy arrays.

tensor_a = tf.ones((6 ,6), dtype = tf.int32)  # Create a tensor of shape (6, 6) with all values 1

print(f'Initial Tensor:\n {tensor_a.numpy()}')
print(f'Initial Shape: {tensor_a.shape}')

# Reshape the tensor into shape (9, 4)
reshaped_a = tf.reshape(tensor_a, (9, 4))
print(f'\nReshaped Tensor:\n {reshaped_a.numpy()}')
print(f'Reshaped Shape: {reshaped_a.shape}')
Initial Tensor:
 [[1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]
 [1 1 1 1 1 1]]
Initial Shape: (6, 6)

Reshaped Tensor:
 [[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
Reshaped Shape: (9, 4)

This is it for the basics of Tensors and Tensor Operations in TensorFlow 2.0. In the next chapter, you will learn to perform Linear Regressing using TensorFlow 2.0.


Tensor and Tensor Operations in TensorFlow 2.0Tensor and Tensor Operations in TensorFlow 2.0

Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in:

  1. Introduction to Python (Free Course) - 1,000,000+ students already enrolled!
  2. Introduction to Data Science  in Python- 400,000+ students already enrolled!
  3. Introduction to TensorFlow for Deep Learning with Python - 90,000+ students already enrolled!
  4. Data Science and Machine Learning Bootcamp with R - 70,000+ students already enrolled!

Leave a Comment