Performing Basic Math Operations

Now, that you know how to create tensors, it's time to learn to manipulate or perform basic operations on tensors.

Here's a list of common operations that we use while crunching data with arrays and numbers:

  1. Update a value.
  2. Basic arithmetic operation on two tensors(vectors or matrices).
  3. Finding minimum/maximum value in a tensor.
  4. Computing mean.

Let's cover them one-by-one here:

We defined two tensors:

a = tf.constant([[1,2], [3,4]])  # a constant tensor
b = tf.Variable([[5,6], [7,8]])  # a variable tensor  
print(a)
print(b)

1. Update a Value - Changing a tensor!

In order to update a value, first try to update tensor a using the assign() method.

##let's try to update the 1st element of the 1st row
a[0,0].assign(10)

Whoops! did you see that? It isn't allowing us to assign any new value to the constant tensor.

Let's try to do the same with b:

##let's try to update the 1st element of the 1st row
b[0,0].assign(10)

Voila! It worked. So, this gives us some insight into the difference between constant and Variable object.

2. Adding two tensors

## elementwise addition
print(a + b). # try out for -, *, /
print(tf.add(a, b))

Arithmentic operators perform elementwise computation.

If you want to multiply matrices, you can use tf.matmul or @

## matrix multiplication
tf.matmul(a, b)

Or @

## matrix multiplication
a @ b

3. Finding minimum/maximum value or index of the min/max value in a tensor

##finding the min/max
print(f"Index of the smallest value in the tensor: {tf.argmin(a)}")
print(f"Largest value in the tensor: {tf.reduce_min(b)}")

4. Computing mean of the data

##finding the min/max
print(f"Mean: {tf.reduce_mean(b)}")

This gives you the mean of the 2D tensor b.

Output:

Next Steps!

You have seen how easy it is to add multiply and play around with variable tensors. All of this is possible because of a technique called broadcasting that you can read more about. 

Apart from mathematical operations, you must also know how to access individual elements, how to create a subset of an N-dimensional tensor and how to play across multiple dimensions/axes.

Hop onto the next step!

Discussion
Excellent tutorial for beginners. Waiting for the next lab on training your first neural network.

5

1