PyTorch Element Wise Multiplication

PyTorch Element Wise Multiplication - Calculate the element wise multiplication to get the Hadamard Product

PyTorch Element Wise Multiplication - Calculate the element wise multiplication to get the Hadamard Product

Video Transcript


We import PyTorch.

import torch


Then we print the PyTorch version we are using.

print(torch.__version__)

We are using PyTorch 0.2.0_4.


In this video, we will do element-wise multiplication of matrices in PyTorch to get the Hadamard product.

We will create two PyTorch tensors and then show how to do the element-wise multiplication of the two of them.


Let’s get started.


First, we create our first PyTorch tensor using the PyTorch rand functionality.

random_tensor_one_ex = (torch.rand(2, 3, 4) * 10).int()

The size is going to be 2x3x4.

We’re going to cast it to an int and we’re going to assign it to the Python variable, random_tensor_one_ex.


Let’s print our first tensor to see what we created.

print(random_tensor_one_ex)

We see that it’s a PyTorch IntTensor of size 2x3x4, the numbers are between 0 and 10, and they are random.


Next, we create our second PyTorch tensor the same way.

random_tensor_two_ex = (torch.rand(2, 3, 4) * 10).int()

This time, we’re assigning it to the Python variable, random_tensor_two_ex.

Again, it’s torch.rand, 2x3x4, times 10, and casting it to an int.


Let’s print our second tensor to see what we got.

print(random_tensor_two_ex)

We see again that we have a PyTorch IntTensor of size 2x3x4, and it’s a random tensor that has numbers between 0 and 10.


To calculate the element-wise multiplication of the two tensors to get the Hadamard product, we’re going to use the asterisk symbol.


So we multiply random_tensor_one_ex times random_tensor_two_ex using the asterisk symbol and we’re going to set it equal to the hadamard_product_ex Python variable.

hadamard_product_ex = random_tensor_one_ex * random_tensor_two_ex


Let’s print out the result

print(hadamard_product_ex)

And we see that it is between 0 and the biggest number looks to be 63.

So if it’s element-wise, our biggest number should have been 9 or 10, so it could have been up to 100.

Let’s check the last row.


So here we see 5.

Here, we see 8.

So we should expect a 40.

That’s what we get.


We see a 7.

We see a 3.

So we get a 21, which is what we would expect.


We see a 6 and 8.

That’s 48.


Lastly, we see a 9 x 1.

We see a 9.


So using the asterisk symbol, we were able to calculate the Hadamard product of the two tensors.

So it did the element-wise multiplication.


The other thing to note is that random_tensor_one_ex was size 2x3x4, random_tensor_two_ex was 2x3x4, and our element-wise multiplication was also 2x3x4, which is what we would expect.


That is how you can calculate the element-wise multiplication of tensors and matrices in PyTorch to get the Hadamard product.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.