Sling Academy
Home/PyTorch/Using the torch.prod() and torch.cumprod() functions in PyTorch

Using the torch.prod() and torch.cumprod() functions in PyTorch

Last updated: July 08, 2023

The torch.prod() and torch.cumprod() functions in PyTorch are used to calculate the product and the cumulative product of elements in a tensor, respectively.

torch.prod()

Syntax:

torch.prod(input, dim=None, keepdim=False, *, dtype=None) -> Tensor

Where:

  • input: the input tensor
  • dim: the dimension to reduce (optional)
  • keepdim: whether to retain the reduced dimension in the output (optional, default False)
  • dtype: the desired data type of the output tensor (optional)

Example:

import torch

# Create a 2D tensor of size 3x4
x = torch.tensor([[1, 2, 3, 4],
                  [5, 6, 7, 8],
                  [9, 10, 11, 12]])

# Calculate the product of all elements in x
p = torch.prod(x)
print(p)  # tensor(479001600)

# Calculate the product of each row in x
p_row = torch.prod(x, dim=1)
print(p_row)  # tensor([   24,  1680, 11880])

# Calculate the product of each column in x and keep the dimension
p_col = torch.prod(x, dim=0, keepdim=True)
print(p_col)  # tensor([[ 45, 120, 231, 384]])

torch.cumprod()

Syntax:

torch.cumprod(input, dim, *, dtype=None, out=None) -> Tensor

Where:

  • input: the input tensor
  • dim: the dimension to do the operation over
  • dtype: the desired data type of the output tensor (optional)
  • out: the output tensor (optional)

Example:

import torch

# Create a 1D tensor of size 5
y = torch.tensor([1, 2, 3, 4, 5])

# Calculate the cumulative product of y along dimension 0
c = torch.cumprod(y, dim=0)
print(c)  
# tensor([  1,   2,   6,  24, 120])

# Create a 2D tensor of size 2x3
z = torch.tensor([[1, -2, -3],
                  [4, -5, -6]])

# Calculate the cumulative product of z along dimension 1
c_row = torch.cumprod(z, dim=1)
print(c_row)  
# tensor([[  1,  -2,   6],
#         [  4, -20, 120]])

That’s it. Happy coding & enjoy your day!

Next Article: How to Reshape a Tensor in PyTorch (with Examples)

Previous Article: PyTorch: Find the Sum and Mean of a Tensor

Series: Working with Tensors in PyTorch

PyTorch

You May Also Like

  • Addressing "UserWarning: floor_divide is deprecated, and will be removed in a future version" in PyTorch Tensor Arithmetic
  • In-Depth: Convolutional Neural Networks (CNNs) for PyTorch Image Classification
  • Implementing Ensemble Classification Methods with PyTorch
  • Using Quantization-Aware Training in PyTorch to Achieve Efficient Deployment
  • Accelerating Cloud Deployments by Exporting PyTorch Models to ONNX
  • Automated Model Compression in PyTorch with Distiller Framework
  • Transforming PyTorch Models into Edge-Optimized Formats using TVM
  • Deploying PyTorch Models to AWS Lambda for Serverless Inference
  • Scaling Up Production Systems with PyTorch Distributed Model Serving
  • Applying Structured Pruning Techniques in PyTorch to Shrink Overparameterized Models
  • Integrating PyTorch with TensorRT for High-Performance Model Serving
  • Leveraging Neural Architecture Search and PyTorch for Compact Model Design
  • Building End-to-End Model Deployment Pipelines with PyTorch and Docker
  • Implementing Mixed Precision Training in PyTorch to Reduce Memory Footprint
  • Converting PyTorch Models to TorchScript for Production Environments
  • Deploying PyTorch Models to iOS and Android for Real-Time Applications
  • Combining Pruning and Quantization in PyTorch for Extreme Model Compression
  • Using PyTorch’s Dynamic Quantization to Speed Up Transformer Inference
  • Applying Post-Training Quantization in PyTorch for Edge Device Efficiency