## Debug School

rakesh kumar

Posted on • Updated on

# list out the checklist of Tensor Math Ops and Reduction Ops in tensor flow

In TensorFlow, a tensor is the collection of feature vector (Like, array) of n-dimension. For instance, if we have any 2x3 matrix with values 1 to 6, we write:

## Math Ops

``````import tensorflow as tf

# Define the tensors
a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])

# Convert the result to a NumPy array and print
print(c.numpy())
``````

Output:

``````[5 7 9]
``````

Subtraction (tf.subtract):

``````a = tf.constant([4, 5, 6])
b = tf.constant([1, 2, 3])
c = tf.subtract(a, b)
print(c.numpy())
``````

Output:

``````[3 3 3]
``````

Multiplication (tf.multiply):

``````a = tf.constant([2, 3, 4])
b = tf.constant([5, 6, 7])
c = tf.multiply(a, b)
print(c.numpy())
``````

Output:

``````[10 18 28]
``````

Division (tf.divide):

``````a = tf.constant([10, 12, 15], dtype=tf.float32)
b = tf.constant([2, 3, 5], dtype=tf.float32)
c = tf.divide(a, b)
print(c.numpy())
``````

Output:

``````[5. 4. 3.]
``````

Matrix Multiplication (tf.matmul):

``````A = tf.constant([[1, 2], [3, 4]])
B = tf.constant([[5, 6], [7, 8]])
C = tf.matmul(A, B)
print(C.numpy())
``````

Output:

``````[[19 22]
[43 50]]
``````

Matrix Transpose (tf.transpose):

``````A = tf.constant([[1, 2], [3, 4]])
B = tf.transpose(A)
print(B.numpy())
``````

Output:

``````[[1 3]
[2 4]]
``````

Element-wise Square Root (tf.sqrt):

``````a = tf.constant([4.0, 9.0, 16.0], dtype=tf.float32)
b = tf.sqrt(a)
print(b.numpy())
``````

Output:

``````[2. 3. 4.]
``````

Exponential (tf.exp):

``````a = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
b = tf.exp(a)
print(b.numpy())
``````

Output:

``````[ 2.7182817  7.389056  20.085537]
``````

Logarithm (tf.math.log):

``````a = tf.constant([2.7182817, 7.389056, 20.085537], dtype=tf.float32)
b = tf.math.log(a)
print(b.numpy())
``````

Output:

``````[1. 2. 3.]
``````

Tensor Handling and Manipulations
In this section, we will learn about Tensor Handling and Manipulations.

To begin with, let us consider the following code −

## Reduction Operations (e.g., Mean and Sum)

Example (Mean):

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
mean_value = tf.reduce_mean(a)
print(mean_value.numpy())
``````

Output:

``````3.0
``````

Example (Sum):

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
sum_value = tf.reduce_sum(a)
print(sum_value.numpy())
``````

Output:

``````15.0
``````

Reduce Mean (tf.reduce_mean):

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
mean_value = tf.reduce_mean(a)
print(mean_value.numpy())
``````

Output:

``````3.0
``````

Reduce Sum (tf.reduce_sum):

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
sum_value = tf.reduce_sum(a)
print(sum_value.numpy())
``````

Output:

``````15.0
``````

Reduce Max (tf.reduce_max):

Example:

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
max_value = tf.reduce_max(a)
print(max_value.numpy())
``````

Output:

5.0
Reduce Min (tf.reduce_min):

``````a = tf.constant([1, 2, 3, 4, 5], dtype=tf.float32)
min_value = tf.reduce_min(a)
print(min_value.numpy())
``````

Output:

``````1.0
``````

Reduce All (tf.reduce_all):

``````a = tf.constant([True, True, False, True])
all_value = tf.reduce_all(a)
print(all_value.numpy())
``````

Output:

``````False
``````

Reduce Any (tf.reduce_any):

``````a = tf.constant([True, False, False, False])
any_value = tf.reduce_any(a)
print(any_value.numpy())
``````

Output:

``````True
``````

Argmax (tf.argmax):

``````a = tf.constant([3, 1, 4, 1, 5], dtype=tf.float32)
argmax_value = tf.argmax(a)
print(argmax_value.numpy())
``````

Output:

``````4
``````

Argmin (tf.argmin):

``````a = tf.constant([3, 1, 4, 1, 5], dtype=tf.float32)
argmin_value = tf.argmin(a)
print(argmin_value.numpy())
``````
``````1
``````

Reduce L2 Norm (tf.norm):

``````a = tf.constant([3.0, 4.0], dtype=tf.float32)
l2_norm_value = tf.norm(a)
print(l2_norm_value.numpy())
``````

Output:

``````5.0
``````

Reduce Log Sum Exp (tf.reduce_logsumexp):

``````a = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
logsumexp_value = tf.reduce_logsumexp(a)
print(logsumexp_value.numpy())
``````

Output:

``````3.4076054
``````

These are some common tensor reduction operations in TensorFlow along with examples and their expected outputs. You can use these operations to compute summary statistics and derive insights from your data in various machine learning and deep learning tasks.