import torch
# Create a tensor of zeros with shape 3x3
t3 = torch.zeros(3, 3)
This shows how tensors can be moved to a GPU for faster math.
PyTorch
import torch
# Move tensor to GPU if availableif torch.cuda.is_available():
t_gpu = t2.to('cuda')
Sample Model
This program shows how tensors hold data and how PyTorch does math with them. It also checks if a GPU is available to speed up work.
PyTorch
import torch
# Create two tensors
x = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
y = torch.tensor([[5, 6], [7, 8]], dtype=torch.float32)
# Add tensors
y_sum = x + y
# Multiply tensors element-wise
product = x * y
# Print resultsprint('Sum of tensors:\n', y_sum)
print('Element-wise product:\n', product)
# Check if GPU is available and move tensorif torch.cuda.is_available():
x_gpu = x.to('cuda')
print('Tensor moved to GPU:', x_gpu.device)
else:
print('GPU not available, running on CPU')
OutputSuccess
Important Notes
Tensors are the foundation for all PyTorch operations and models.
They support automatic differentiation, which helps models learn.
Using tensors on a GPU can make training much faster.
Summary
Tensors store numbers in a way computers can easily use for math.
They work on CPUs and GPUs to speed up machine learning tasks.
Understanding tensors is key to using PyTorch effectively.
Practice
(1/5)
1. Why are tensors considered the core data structure in PyTorch?
easy
A. Because they are the only way to create variables in Python
B. Because they efficiently store and process numerical data for machine learning
C. Because they are only used for storing text data
D. Because they replace Python lists for all programming tasks
Solution
Step 1: Understand the role of tensors in PyTorch
Tensors are designed to hold numbers and perform math operations efficiently, which is essential for machine learning.
Step 2: Compare tensors with other data types
Unlike text or general variables, tensors are optimized for numerical computation on CPUs and GPUs.
Final Answer:
Because they efficiently store and process numerical data for machine learning -> Option B
Quick Check:
Tensors = Core numerical data structure [OK]
Hint: Tensors store numbers for fast math in ML [OK]
Common Mistakes:
Thinking tensors store only text
Confusing tensors with Python variables
Assuming tensors replace all Python data types
2. Which of the following is the correct way to create a tensor in PyTorch?
easy
A. torch.array([1, 2, 3])
B. torch.list([1, 2, 3])
C. torch.tensor([1, 2, 3])
D. torch.create([1, 2, 3])
Solution
Step 1: Recall PyTorch tensor creation syntax
The correct function to create a tensor from a list is torch.tensor().
Step 2: Identify incorrect options
Functions like torch.list(), torch.array(), and torch.create() do not exist in PyTorch.
Final Answer:
torch.tensor([1, 2, 3]) -> Option C
Quick Check:
Use torch.tensor() to create tensors [OK]
Hint: Use torch.tensor() to make tensors [OK]
Common Mistakes:
Using non-existent torch.list()
Confusing with numpy.array()
Trying torch.create() which is invalid
3. What will be the output of this PyTorch code?
import torch
x = torch.tensor([1, 2, 3])
y = x + 5
print(y)
medium
A. Error: cannot add int to tensor
B. [6, 7, 8]
C. tensor([1, 2, 3, 5])
D. tensor([6, 7, 8])
Solution
Step 1: Understand tensor addition with a scalar
Adding 5 to tensor x adds 5 to each element, resulting in [6, 7, 8].
Step 2: Check the print output format
PyTorch prints tensors with the 'tensor()' wrapper, so output is tensor([6, 7, 8]).
Final Answer:
tensor([6, 7, 8]) -> Option D
Quick Check:
Tensor + scalar adds to each element [OK]
Hint: Adding scalar to tensor adds to each element [OK]
Common Mistakes:
Expecting a Python list output
Thinking addition concatenates elements
Assuming error when adding int to tensor
4. Identify the error in this PyTorch code snippet:
import torch
x = torch.tensor([1, 2, 3])
y = x + '5'
print(y)
medium
A. Cannot add a string to a tensor
B. Tensor must be created with floats
C. Missing import statement
D. Tensor size mismatch error
Solution
Step 1: Analyze the addition operation
The code tries to add a string '5' to a tensor, which is not allowed.
Step 2: Understand PyTorch type rules
PyTorch tensors support addition with numbers, not strings, causing a TypeError.
Final Answer:
Cannot add a string to a tensor -> Option A
Quick Check:
Tensor + string causes error [OK]
Hint: Tensors add only numbers, not strings [OK]
Common Mistakes:
Thinking tensor needs floats only
Ignoring type mismatch in addition
Assuming missing import causes error
5. You want to speed up your machine learning model training by using GPU. Why is using tensors important in PyTorch for this?
hard
A. Because tensors can be moved to GPU memory for fast math operations
B. Because tensors automatically convert code to GPU instructions without user action
C. Because tensors store data as strings which GPUs process faster
D. Because tensors replace the need for GPUs entirely
Solution
Step 1: Understand tensor compatibility with hardware
Tensors can be moved to GPU memory, allowing fast parallel math operations.
Step 2: Clarify user control over device placement
Users must explicitly move tensors to GPU; tensors do not auto-convert code.
Final Answer:
Because tensors can be moved to GPU memory for fast math operations -> Option A
Quick Check:
Tensors + GPU = faster ML math [OK]
Hint: Move tensors to GPU for speed [OK]
Common Mistakes:
Thinking tensors auto-use GPU without code
Believing tensors store strings for speed
Assuming tensors replace GPUs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.