# Implement Matrix Product Between 3D and 2D Tensor in PyTorch – PyTorch Tutorial

By | September 7, 2022

When we are using pytorch to build a model, we have to calculate the matrix product between 3D and 2D tensor. For example:

x = [batch_size, length, dim] = [32, 50, 200]

w = [dim, dim] = [200, 100]

y = f(x, w) = [32, 200, 100]

In this tutorial, we will introduce you how to do.

For example:

import torch

x = torch.randn(32, 50, 200)
w = torch.randn(200, 100)
#[50,200] * [200,100]

y = torch.matmul(x, w)

print(y.shape)

Run this code, we can see:

torch.Size([32, 50, 100])

It means that we can use torch.matmul() function to calculate the the matrix product between 3D and 2D tensor in pytorch.

It is easy to do.