# Element-wise Multiplication in TensorFlow Python

In this short tutorial, we will perform element-wise multiplication in TensorFlow using both TensorFlow function and without the function. The code for executing element-wise multiplication is slightly different in Tensorflow 1.x and Tensorflow 2.x. We will see the code in both the versions so that you may use whichever is suitable according to your version. Let’s get started.

### Elementwise Multiplication

Elementwise multiplication is a simple mathematical operation in which two arrays of the same dimensions are multiplied to get a third array of the same dimension. Each element of the resulting array is a result of the multiplication of each corresponding scalar in both the parent tensors. Let’s take two tensors A and B with dimensions 2×2 each and understand this operation.

```A = (a11 a12
a21 a22)
B = (b11 b12
b21 b22)
C = A*B
C = (a11*b11 a12*b12
a21*b21 a22*b22)```

As mentioned earlier, elementwise multiplication can be performed both with and without using the Tensorflow function. For Tensorflow 1.x, we will use tf.Session to run the graph. But, since tf.Session is no longer present in Tensorflow 2.x, we will have to use either tf.compat.v1.Session or tf.fucntion decorator. Let’s see the code with each variant.

### Using TensorFlow function

The function used for elementwise multiplication is tf.math.multiply(). The arguments to the function should be of the same shape. The function implicitly converts the arguments into tensors so non-tensors can also be passed. If the shape of both the tensors is not the same then it will broadcast them into a compatible shape.

TensorFlow 1.x

```import tensorflow as tf
import numpy as np

# create a graph
graph = tf.Graph()

with graph.as_default():
# Two 2x4 tensors
A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))

# Elementwise multiplication using tf funciton
C = tf.math.multiply(A,B)

# Run the session
with tf.Session(graph=graph) as session:
C = session.run(C)
print("Elementwise multiplication of A and B:\nA: {}\nB: {}\n{}".format(A,B,C))```
```Elementwise multiplication of A and B:
A: Tensor("Const:0", shape=(2, 4), dtype=int64)
B: Tensor("Const_1:0", shape=(2, 4), dtype=int64)
[[ 5 12 21 32]
[32 21 12  5]]```

### TensorFlow 2.x

Using tf.compat.v1.Session –

```# create a graph
graph = tf.Graph()

with graph.as_default():
# Two 2x4 tensors
A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))

# Elementwise multiplication using tf funciton
C = tf.math.multiply(A,B)

# create session
session = tf.compat.v1.Session(graph = graph)

# Run the session
with session:
C = session.run(C)
print("Elementwise multiplication of A and B:\nA: {}\nB: {}\n{}".format(A,B,C))```
```Elementwise multiplication of A and B:
A: Tensor("Const:0", shape=(2, 4), dtype=int32)
B: Tensor("Const_1:0", shape=(2, 4), dtype=int32)
[[ 5 12 21 32]
[32 21 12  5]]```

Using tf.fucntion decorator –

```# tf.fucntion decorator
@tf.function
def f(A,B):
# elementwise cumtiplication using tf fucntion
C = tf.math.multiply(A,B)
return C

A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))
C = f(A,B)
tf.print("Elementwise multiplication of A and B:\nA: {}\nB: {}\nResult:\n{}".format(A,B,C))
```
```Elementwise multiplication of A and B:
A: [[1 2 3 4]
[4 3 2 1]]
B: [[5 6 7 8]
[8 7 6 5]]
Result:
[[ 5 12 21 32]
[32 21 12  5]]```

### Without using the TensorFlow function

The code for elementwise multiplication for both the versions will be the same as before. The only difference is that instead of tf.math.multiply(), we will use simply A*B. Just be careful to keep the shape of both the tensors same.

TensorFlow 1.x

```# create a graph
graph = tf.Graph()

with graph.as_default():
# Two 2x4 tensors
A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))

# Elementwise multiplication without using the fucntion
C = A*B

# Run the session
with tf.Session(graph=graph) as session:
C = session.run(C)
print("Elementwise multiplication of A and B:\nA: {}\nB: {}\n{}".format(A,B,C))```
```Elementwise multiplication of A and B:
A: Tensor("Const:0", shape=(2, 4), dtype=int64)
B: Tensor("Const_1:0", shape=(2, 4), dtype=int64)
[[ 5 12 21 32]
[32 21 12  5]]```

TensorFlow 2.x

Using tf.compat.v1.Session –

```# create a graph
graph = tf.Graph()
with graph.as_default():
# Two 2x4 tensors
A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))

# Elementwise multiplication without using the function
C = A*B

# create session
session = tf.compat.v1.Session(graph = graph)

# Run the session
with session:
C = session.run(C)
print("Elementwise multiplication of A and B:\nA: {}\nB: {}\n{}".format(A,B,C))```
```Elementwise multiplication of A and B:
A: Tensor("Const:0", shape=(2, 4), dtype=int32)
B: Tensor("Const_1:0", shape=(2, 4), dtype=int32)
[[ 5 12 21 32]
[32 21 12  5]]```

Using tf.fucntion decorator –

```# tf.fucntion decorator
@tf.function
def f(A,B):
# elementwise cumtiplication without using the fucntion
C = A*B
return C

A = tf.constant(np.array([[1,2,3,4],[4,3,2,1]]))
B = tf.constant(np.array([[5,6,7,8],[8,7,6,5]]))
C = f(A,B)
tf.print("Elementwise multiplication of A and B:\nA: {}\nB: {}\nResult:\n{}".format(A,B,C))
```
```Elementwise multiplication of A and B:
A: [[1 2 3 4]
[4 3 2 1]]
B: [[5 6 7 8]
[8 7 6 5]]
Result:
[[ 5 12 21 32]
[32 21 12  5]]```

We saw all the variations in the code snippet for finding the element-wise multiplication of two tensors in TensorFlow. Now you know how to perform this operation on any version of TensorFlow.