Debug School

rakesh kumar
rakesh kumar

Posted on

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

Math Ops

Addition (tf.add):

a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])
c = tf.add(a, b)
print(c.numpy())
Enter fullscreen mode Exit fullscreen mode

Output:

[5 7 9]
Enter fullscreen mode Exit fullscreen mode

Subtraction (tf.subtract):

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

Output:

[3 3 3]
Enter fullscreen mode Exit fullscreen mode

Multiplication (tf.multiply):

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

Output:

[10 18 28]
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

[5. 4. 3.]
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

[[19 22]
 [43 50]]
Enter fullscreen mode Exit fullscreen mode

Matrix Transpose (tf.transpose):

A = tf.constant([[1, 2], [3, 4]])
B = tf.transpose(A)
print(B.numpy())
Enter fullscreen mode Exit fullscreen mode

Output:

[[1 3]
 [2 4]]
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

[2. 3. 4.]
Enter fullscreen mode Exit fullscreen mode

Exponential (tf.exp):

a = tf.constant([1.0, 2.0, 3.0], dtype=tf.float32)
b = tf.exp(a)
print(b.numpy())
Enter fullscreen mode Exit fullscreen mode

Output:

[ 2.7182817  7.389056  20.085537]
Enter fullscreen mode Exit fullscreen mode

Logarithm (tf.math.log):

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

Output:

[1. 2. 3.]
Enter fullscreen mode Exit fullscreen mode

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

To begin with, let us consider the following code −

Image description

Image description

Image description

Image description

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())
Enter fullscreen mode Exit fullscreen mode

Output:

3.0
Enter fullscreen mode Exit fullscreen mode

Example (Sum):

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

Output:

15.0
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

3.0
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

15.0
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

Output:

1.0
Enter fullscreen mode Exit fullscreen mode

Reduce All (tf.reduce_all):

a = tf.constant([True, True, False, True])
all_value = tf.reduce_all(a)
print(all_value.numpy())
Enter fullscreen mode Exit fullscreen mode

Output:

False
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Reduce Any (tf.reduce_any):

a = tf.constant([True, False, False, False])
any_value = tf.reduce_any(a)
print(any_value.numpy())
Enter fullscreen mode Exit fullscreen mode

Output:

True
Enter fullscreen mode Exit fullscreen mode

Image description

Argmax (tf.argmax):

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

Output:

4
Enter fullscreen mode Exit fullscreen mode

Image description

Argmin (tf.argmin):

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

Image description

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())
Enter fullscreen mode Exit fullscreen mode

Output:

5.0
Enter fullscreen mode Exit fullscreen mode

Image description

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())
Enter fullscreen mode Exit fullscreen mode

Output:

3.4076054
Enter fullscreen mode Exit fullscreen mode

Image description

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.

Top comments (0)