Section 1 - Instruction

Welcome to creating tensors in code! Now that you understand tensor shapes, let's learn how to make them using PyTorch, a popular library for machine learning. PyTorch makes it easy to create and manipulate tensors efficiently.

Engagement Message

Have you heard of PyTorch before?

Section 2 - Instruction

NumPy arrays and PyTorch tensors are actually very similar—they both represent multi-dimensional data.

You can easily convert a NumPy array to a PyTorch tensor with torch.from_numpy(numpy_array), and back with tensor.numpy(). This makes it simple to move data between scientific Python code (NumPy) and machine learning code (PyTorch).

Engagement Message

Isn't this useful?

Section 2 - Instruction

The most intuitive way to create tensors is from existing data like lists.

In PyTorch: torch.tensor([1, 2, 3]) creates a 1D tensor from a list.

Engagement Message

What shape would you get from torch.tensor([[1, 2], [3, 4]])?

Section 3 - Instruction

Sometimes you need tensors filled with specific values. Common patterns include:

Zeros: torch.zeros((3, 4)) creates a 3x4 tensor of zeros
Ones: torch.ones((2, 2)) creates a 2x2 tensor of ones

Engagement Message

Why might you want a tensor filled with zeros when starting a calculation?

Section 4 - Instruction

Random tensors are crucial for machine learning, especially for initializing model weights.

torch.rand((2, 3)) creates random values between 0 and 1.
torch.randn((2, 3)) creates random values from a normal distribution.

Engagement Message

When might random initialization be better than starting with zeros?

Section 5 - Instruction

Data types matter for memory and computation speed. Common types include:

  • torch.float32: 32-bit floating point (most common in ML)
  • torch.int64: 64-bit integers
  • torch.bool: True/False values

You specify like: torch.zeros((2, 2), dtype=torch.float32)

Engagement Message

What data type would you use for storing pixel intensities (0-255)?

Section 6 - Instruction

Let's inspect a tensor after creating it from a Python list. This helps you understand both the data and its shape.

This will output:

Engagement Message

Does this make sense?

Section 7 - Practice

Type

Swipe Left or Right

Practice Question

Which creation method would you use for each scenario? Match each use case with the best tensor creation approach.

Labels

  • Left Label: Zeros/Ones
  • Right Label: Random Creation

Left Label Items

  • Initializing bias parameters to zero
  • Creating a mask to hide certain values
  • Starting with a clean slate for accumulation
  • Setting up a binary classification target

Right Label Items

  • Initializing neural network weights
  • Generating synthetic training data
  • Adding noise to existing data
  • Creating diverse starting conditions
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal