Different Tastes of Matrix Multiplication in Pytorch

Cassie Guo
2 min readMay 26, 2020

--

Happy quarantine!

I recently started to refresh my knowledge on pytorch. This post is simply to document what I have learned. I will focus on different flavor of matrix multiplication in Pytorch.

Different favors (image source: https://www.mithly.net/tips-for-making-ice-cream-recipes/)
Different Flavors [image source]

First of all: basics for making ice cream

Basics (image souce)

torch.from_numpy() and torch.unsqueeze()are basics for you to manipulate data in pytorch. See below for examples.

torch.unsqueezeinserts a new tensor with size of one at the desired position. You can use it to change the dimension of existing tensor.

Level up: torch.expand

expand() can help with generating replicative tensors from singleton. See example below:

Now let’s make ice cream of different flavor!

What is your favorite flavor (image source)

Vanilla flavor of matrix multiplication: torch.matmul()

This is the basic version

a = torch.randn(2,3)
b = torch.randn(3,4)
c = torch.matmul(a,b)

Next, more fun with torch.chain_matmul

Finally, mega fun with torch.bmm

So what is your favorite?

You can check out the detailed notebook here:

Enjoy your ice cream🍦!

--

--

Cassie Guo

Data Scientist; write about data culinary (data: ingredients 🍗🥬🧂🍅; model: recipe 📖; results: delicious dish🥘) and other shenanigans