The story of Tensors (TensorFlow) | by Locus.Focus | Oct, 2023


I have prepared a Google Colab for the syntax of some more basic Tensor functions and operations. Also do check out the official documentation of TensorFlow for more information and knowledge.

  1. tf.constant

The tf.constant function in TensorFlow is used to create a constant tensor with a specified value. The basic syntax for creating a constant tensor is:

import tensorflow as tf # Creating a constant tensor 
my_tensor = tf.constant(value, dtype=tf.float32)

2D tensors and 3D tensors using tf.constant

import tensorflow as tf 

# Creating a 2D tensor (matrix)
matrix_2d = tf.constant([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=tf.float32)

# Creating a 3D tensor
tensor_3d = tf.constant([[[1, 2, 3],
[4, 5, 6]],
[[7, 8, 9],
[10, 11, 12]]], dtype=tf.float32)

2D and 3D tensors are commonly used in various machine learning and deep learning applications, especially when working with data like images, time series, or spatial data, which require multi-dimensional representations.

Note: Mixed type of tensors can’t be formed using tf.constant. For Example:

import tensorflow as tf
tensor_1 = tf.constant([1,2,3,4,’i’])
The above code will give this as an error.

2. Identity (Eye) Tensor:

The tf.eye function is used to create an identity (or eye) tensor. An identity tensor is a square matrix where all elements in the main diagonal are set to 1, and all other elements are 0.

Here’s an example of creating a 3×3 identity tensor using TensorFlow:

import tensorflow as tf
# Creating a 3x3 identity tensor
identity_tensor = tf.eye(3, dtype=tf.float32)

3. Zero Tensor:

The tf.zeros function is used to create a tensor filled with zeros. You can specify the shape and data type of the tensor.

Here’s an example of creating a 2×4 zero tensor with TensorFlow:

import tensorflow as tf
# Creating a 2x4 zero tensor
zero_tensor = tf.zeros([2, 4], dtype=tf.float32)

4. Rank, Shape and Size

import tensorflow as tf
# Create a tensor
tensor = tf.constant([[1, 2], [3, 4]])
# Get the rank of the tensor
rank = tf.rank(tensor)
print(“Rank of the tensor:”, rank.numpy()) # Output: 2
# Get the shape of the tensor
shape = tf.shape(tensor)
print("Shape of the tensor:", shape.numpy()) # Output: [2 2]
# Get the size of the tensor
size = tf.size(tensor)
print("Size of the tensor:", size.numpy()) # Output: 4

Understanding the rank, shape, and size of tensors is fundamental when working with TensorFlow, as it allows you to manipulate and reshape tensors effectively in various machine learning tasks.

Photo by Compare Fibre on Unsplash

5. Indexing and Slicing

In TensorFlow, you can perform indexing and slicing operations on tensors similarly to how you would in Python. You can access specific elements or sub-tensors within a tensor using indices. Here’s an overview of indexing in TensorFlow:

  • Indexing a 1D Tensor (Vector):
    You can access elements of a 1D tensor (vector) using square brackets and integer indices.

vector = tf.constant([1, 2, 3, 4, 5])
element = vector[2] # Accessing the third element (index 2)
print(element.numpy()) # Outputs: 3
sub_tensor = vector[1:4] # Access elements from index 1 to 3 (excludes 4)
print(sub_tensor.numpy()) # Outputs: [2 3 4]
  • Indexing a 2D Tensor (Matrix):
 matrix = tf.constant([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
element = matrix[1, 2] # Accessing the element at row 1, column 2
print(element.numpy()) # Outputs: 6
sub_matrix = matrix[1:3, 0:2] # Accessing a sub-matrix
print(sub_matrix.numpy())
# Outputs:
# [[4 5]
# [7 8]]

These are the fundamental ways to perform indexing and slicing in TensorFlow. You can apply similar principles for higher-dimensional tensors. Indexing is particularly useful for extracting specific data points or sub-tensors when working with complex machine learning models.

6. tf.math

In TensorFlow, you can work with complex numbers and perform various operations on them using the `tf.complex` function, as well as `tf.sqrt`, `tf.abs`, and the basic arithmetic operations (`+`, `-`, `*`, and `/`). Here’s how to use these operations with complex numbers:

import tensorflow as tf

# Creating complex numbers
z1 = tf.complex(3.0, 4.0) # 3 + 4i
z2 = tf.complex(1.0, 2.0) # 1 + 2i

# Square root
sqrt_z1 = tf.sqrt(z1)

# Absolute value
abs_z1 = tf.abs(z1)

# Addition
sum_z = z1 + z2

# Subtraction
diff_z = z1 - z2

# Multiplication
product_z = z1 * z2

# Division
quotient_z = z1 / z2

These operations allow you to work with complex numbers in TensorFlow, making it suitable for various applications, including signal processing and quantum computing simulations.

7. tf.math (Continued)

import tensorflow as tf

# Creating a sample tensor
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])

# Reduce Sum
sum_all = tf.reduce_sum(tensor) # Output: 21

# Reduce Max
max_value = tf.reduce_max(tensor) # Output: 6

# Argmax
max_index = tf.argmax(tensor) # Output: 5 (index of the maximum value)

# Power
squared = tf.pow(tensor, 2) # Output: [[ 1 4 9]
# [16 25 36]]

# Top_k
top_values, top_indices = tf.math.top_k(tensor, k=2)
# Output (Top 2 Values): [3 6]
# Output (Top 2 Indices): [2 2]

  • tf.reduce_sum(tensor) — Reduce Sum:
    The ‘tf.reduce_sum’ function is used to compute the sum of all elements in the ‘tensor’.
  • tf.reduce_max(tensor) — Reduce Max:
    The ‘tf.reduce_max’ function calculates the maximum value in the ‘tensor’.
  • tf.argmax(tensor) — Argmax:
    The ‘tf.argmax’ function returns the indices of the maximum values in the ‘tensor’.
  • tf.pow(tensor, 2)` — Power (Squared):
    The ‘tf.pow’ function is used to calculate the element-wise power of the `tensor`.
  • tf.math.top_k(tensor, k=2) — Top_k:
    The ‘tf.math.top_k’ function returns the top k values and their indices in the ‘tensor’.

These operations showcase common operations in TensorFlow for reducing, extracting information, and performing element-wise computations on tensors. They are frequently used in various machine learning and numerical applications.

8. tf.einsum (V.imp and powerful)

Suppose you have two tensors, A and B, and you want to compute their matrix multiplication (dot product). You can achieve this using tf.einsum as follows:

import tensorflow as tf
# Define two input tensors
A = tf.constant([[1, 2],
[3, 4]])
B = tf.constant([[5, 6],
[7, 8]])
# Use tf.einsum to perform matrix multiplication
result = tf.einsum('ij,jk->ik', A, B)
# Output[[19 22]
[43 50]]

In this example, 'ij,jk->ik' is the Einstein summation notation that defines the operation. It specifies that you want to compute the dot product of A and B. The result is stored in the result tensor.

You can use tf.einsum for a wide range of operations, such as element-wise multiplication, summation, contraction, and more. The notation is powerful and flexible, allowing you to express complex operations concisely. You’ll need to understand the Einstein summation notation to use tf.einsum effectively, and the TensorFlow documentation provides detailed examples and explanations to help you get started with it.

9. tf.linalg

The ‘tf.linalg’ module in TensorFlow provides a wide range of methods for linear algebra operations, making it an important tool for tasks such as solving systems of linear equations, performing matrix factorizations, computing eigenvalues and eigenvectors, and more. Here are some important methods and functions available in ‘tf.linalg’:

import tensorflow as tf

# Create sample matrices
A = tf.constant([[1.0, 2.0], [3.0, 4.0]], dtype=tf.float32)
B = tf.constant([[5.0, 6.0], [7.0, 8.0]], dtype=tf.float32)

# Matrix Transposition
transposed_A = tf.linalg.matrix_transpose(A)

# Matrix Multiplication
matmul_result = tf.linalg.matmul(A, B)

# Matrix Inversion
A_inverse = tf.linalg.inv(A)

# Solve Linear System
linear_system_solution = tf.linalg.solve(A, B)

# QR Decomposition
q, r = tf.linalg.qr(A)

# SVD (Singular Value Decomposition)
s, u, v = tf.linalg.svd(A)

# Cholesky Decomposition
cholesky_L = tf.linalg.cholesky(A)

# Eigenvalue and Eigenvector Computations
eigenvalues, eigenvectors = tf.linalg.eigh(A)

# Matrix Norm
matrix_norm = tf.linalg.norm(A)

10. Other important functions

Try running and checking the outputs of some of the following more important functions.

import tensorflow as tf

# Reshaping (tf.reshape)
original_tensor = tf.constant([1, 2, 3, 4, 5, 6])
reshaped_tensor = tf.reshape(original_tensor, (2, 3))

# Concatenating (tf.concat)
tensor1 = tf.constant([1, 2])
tensor2 = tf.constant([3, 4])
concatenated_tensor = tf.concat([tensor1, tensor2], axis=0)

# Adding Dimensions (tf.expand_dims)
original_tensor = tf.constant([1, 2, 3])
expanded_tensor = tf.expand_dims(original_tensor, axis=1)

# Padding (tf.pad)
t3 = tf.constant([[1, 2], [3, 4]])
paddings = tf.constant([[1,2],
[3,4]])
print(tf.pad(t3,paddings,'CONSTANT', constant_values = 0)) #zero is default value for constant padding
paddings = tf.constant([[2, 2,], [2, 2]])
print(tf.pad(t3,paddings,'REFLECT'))
print(tf.pad(t3,paddings,'SYMMETRIC'))



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*