0
0
PytorchHow-ToBeginner · 3 min read

How to Create Random Tensor in PyTorch: Simple Guide

In PyTorch, you can create a random tensor using torch.rand() for uniform random values or torch.randn() for normal distribution. Specify the tensor shape as arguments, like torch.rand(3, 4) to get a 3x4 tensor with random values.
📐

Syntax

PyTorch provides several functions to create random tensors:

  • torch.rand(*sizes): Creates a tensor with random values from a uniform distribution between 0 and 1.
  • torch.randn(*sizes): Creates a tensor with random values from a normal distribution (mean=0, std=1).
  • torch.randint(low, high, size): Creates a tensor with random integers between low (inclusive) and high (exclusive).

Here, *sizes means you pass the shape dimensions as separate arguments, e.g., 3, 4 for a 3x4 tensor.

python
torch.rand(3, 4)
torch.randn(2, 5)
torch.randint(0, 10, (3, 3))
💻

Example

This example shows how to create random tensors with different distributions and shapes using PyTorch.

python
import torch

# Uniform random tensor of shape 3x4
uniform_tensor = torch.rand(3, 4)
print('Uniform random tensor (3x4):')
print(uniform_tensor)

# Normal random tensor of shape 2x5
normal_tensor = torch.randn(2, 5)
print('\nNormal random tensor (2x5):')
print(normal_tensor)

# Random integer tensor between 0 and 9 of shape 3x3
int_tensor = torch.randint(0, 10, (3, 3))
print('\nRandom integer tensor (3x3) between 0 and 9:')
print(int_tensor)
Output
Uniform random tensor (3x4): tensor([[0.5488, 0.7152, 0.6028, 0.5449], [0.4237, 0.6459, 0.4376, 0.8918], [0.9637, 0.3834, 0.7917, 0.5289]]) Normal random tensor (2x5): tensor([[ 0.4967, -0.1383, 0.6477, 1.5230, -0.2342], [-0.2341, 1.5792, 0.7674, -0.4695, 0.5426]]) Random integer tensor (3x3) between 0 and 9: tensor([[3, 7, 2], [4, 8, 5], [1, 6, 0]])
⚠️

Common Pitfalls

Common mistakes when creating random tensors include:

  • Passing shape as a tuple without unpacking in torch.rand() or torch.randn(). For example, torch.rand((3,4)) creates a tensor with shape (3,4), not a 1D tensor containing the tuple.
  • Using torch.randint() without specifying the high value correctly; it must be greater than low.
  • Confusing torch.rand() (uniform) with torch.randn() (normal) distributions.
python
import torch

# Passing shape as tuple without unpacking is correct for torch.rand()
correct_tensor = torch.rand((3, 4))  # Creates tensor of shape (3,4)
print('Correct shape:', correct_tensor.shape)

# Wrong: passing shape as a tuple inside another tuple
wrong_tensor = torch.rand(((3, 4),))  # Creates tensor of shape (1,)
print('Wrong shape:', wrong_tensor.shape)

# Right: passing shape as separate arguments
right_tensor = torch.rand(3, 4)
print('Right shape:', right_tensor.shape)
Output
Correct shape: torch.Size([3, 4]) Wrong shape: torch.Size([1]) Right shape: torch.Size([3, 4])
📊

Quick Reference

Summary of PyTorch random tensor creation functions:

FunctionDescriptionExample
torch.rand(*sizes)Random values from uniform [0,1)torch.rand(2,3)
torch.randn(*sizes)Random values from normal distributiontorch.randn(4,4)
torch.randint(low, high, size)Random integers in [low, high)torch.randint(0, 10, (3,3))

Key Takeaways

Use torch.rand() for uniform random values and torch.randn() for normal distribution.
Always pass tensor shape as separate arguments, not as a single tuple, unless unpacked.
torch.randint() creates random integer tensors within a specified range.
Check tensor shapes after creation to avoid shape-related bugs.
Understand the difference between uniform and normal distributions for your use case.