0
0
PyTorchml~15 mins

Tensor creation (torch.tensor, zeros, ones, rand) in PyTorch - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Tensor creation (torch.tensor, zeros, ones, rand)
Problem:You want to create different types of tensors in PyTorch for a simple neural network input. Currently, you only use torch.tensor with fixed values.
Current Metrics:You can create tensors with torch.tensor but struggle to create tensors filled with zeros, ones, or random values easily.
Issue:Limited tensor creation methods slow down experimentation and data preparation.
Your Task
Learn to create tensors using torch.zeros, torch.ones, and torch.rand to generate tensors of desired shapes and values.
Use PyTorch functions only for tensor creation.
Create tensors of shape (3, 4) for all methods.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
PyTorch
import torch

# Create tensor from fixed values
fixed_tensor = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# Create tensor filled with zeros
zeros_tensor = torch.zeros((3, 4))

# Create tensor filled with ones
ones_tensor = torch.ones((3, 4))

# Create tensor with random values between 0 and 1
rand_tensor = torch.rand((3, 4))

print('Fixed tensor:\n', fixed_tensor)
print('Zeros tensor:\n', zeros_tensor)
print('Ones tensor:\n', ones_tensor)
print('Random tensor:\n', rand_tensor)
Added torch.zeros to create a tensor filled with zeros.
Added torch.ones to create a tensor filled with ones.
Added torch.rand to create a tensor with random values.
Kept torch.tensor for fixed value tensor creation.
Results Interpretation

Before: Only torch.tensor was used, limiting tensor creation to fixed values.

After: Used torch.zeros, torch.ones, and torch.rand to create tensors filled with zeros, ones, and random values respectively, all with shape (3, 4).

Using different PyTorch tensor creation functions helps quickly generate tensors with desired values and shapes, making data preparation and experimentation easier.
Bonus Experiment
Try creating tensors with different data types like integers and floats using the dtype parameter.
💡 Hint
Use the dtype argument in tensor creation functions, e.g., torch.ones((3,4), dtype=torch.int32).