0
0
PyTorchml~12 mins

Tensor creation (torch.tensor, zeros, ones, rand) in PyTorch - Model Pipeline Trace

Choose your learning style9 modes available
Model Pipeline - Tensor creation (torch.tensor, zeros, ones, rand)

This pipeline shows how tensors are created using different PyTorch functions. Tensors are the basic building blocks for data in PyTorch, similar to arrays or tables in everyday life.

Data Flow - 4 Stages
1Create tensor from list
N/Atorch.tensor creates a tensor from a Python list3 rows x 3 columns
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> torch.tensor -> tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
2Create tensor of zeros
N/Atorch.zeros creates a tensor filled with zeros of given shape2 rows x 4 columns
torch.zeros(2, 4) -> tensor([[0., 0., 0., 0.], [0., 0., 0., 0.]])
3Create tensor of ones
N/Atorch.ones creates a tensor filled with ones of given shape3 rows x 2 columns
torch.ones(3, 2) -> tensor([[1., 1.], [1., 1.], [1., 1.]])
4Create tensor with random values
N/Atorch.rand creates a tensor with random values between 0 and 12 rows x 3 columns
torch.rand(2, 3) -> tensor([[0.23, 0.67, 0.12], [0.89, 0.45, 0.33]])
Training Trace - Epoch by Epoch
No training loss to show because this pipeline only creates tensors.
EpochLoss ↓Accuracy ↑Observation
1N/AN/ANo training occurs because this is tensor creation only.
Prediction Trace - 4 Layers
Layer 1: torch.tensor
Layer 2: torch.zeros
Layer 3: torch.ones
Layer 4: torch.rand
Model Quiz - 3 Questions
Test your understanding
What does torch.zeros(3, 2) produce?
AA tensor with 2 rows and 3 columns filled with zeros
BA tensor with 3 rows and 2 columns filled with ones
CA tensor with 3 rows and 2 columns filled with zeros
DA tensor with random values between 0 and 1
Key Insight
Tensor creation functions in PyTorch provide flexible ways to start working with data. Knowing how to create tensors filled with zeros, ones, or random values is essential for initializing models and data structures before training.