When you do your first PyTorch computation, the main goal is to check if your code runs correctly and produces expected results. At this stage, metrics like loss and accuracy help you understand if your model learns well. Loss tells how far your model's predictions are from the true answers. Accuracy shows how many predictions are correct. These metrics matter because they guide you to improve your model step by step.
First PyTorch computation - Model Metrics & Evaluation
For classification tasks, a confusion matrix helps you see where your model makes mistakes. Here is an example for a simple two-class problem:
Actual \ Predicted | Positive | Negative
-------------------|----------|---------
Positive | TP=8 | FN=2
Negative | FP=1 | TN=9
This shows 8 true positives, 2 false negatives, 1 false positive, and 9 true negatives. You can calculate precision, recall, and accuracy from these numbers.
Imagine your first PyTorch model is for spam detection. Precision means how many emails marked as spam really are spam. Recall means how many actual spam emails your model finds.
If your model has high precision but low recall, it rarely marks good emails as spam but misses many spam emails. If it has high recall but low precision, it catches most spam but also marks many good emails as spam.
Depending on your goal, you choose which metric to improve. For your first computation, understanding this tradeoff helps you decide what to focus on next.
For your first PyTorch computation, good metrics mean your model learns something useful:
- Loss: Should decrease over time. If it stays the same or increases, your model might not be learning.
- Accuracy: Should improve from random guessing. For example, if you have 2 classes, random accuracy is about 50%. Getting 70% or more is good progress.
Bad metrics mean your model is not learning or your code has errors. For example, accuracy stuck at random level or loss not changing.
When starting with PyTorch, watch out for these common pitfalls:
- Accuracy paradox: High accuracy can be misleading if your data is unbalanced. For example, if 90% of data is one class, predicting that class always gives 90% accuracy but no real learning.
- Data leakage: Accidentally using test data during training can make metrics look perfect but the model will fail on new data.
- Overfitting: If training accuracy is very high but test accuracy is low, your model memorizes training data but does not generalize.
No, this model is not good for fraud detection. Even though accuracy is high, recall is very low. This means the model misses most fraud cases (only finds 12%). In fraud detection, missing fraud is very costly, so recall is more important. You should improve recall even if accuracy drops a bit.
Practice
Solution
Step 1: Understand the role of tensors in PyTorch
PyTorch tensors are like containers that hold numbers for math operations, similar to arrays.Step 2: Compare options with tensor purpose
Storing text data only, creating web pages, and managing database connections do not relate to math operations or number storage, so they are incorrect.Final Answer:
Holding numbers for math operations -> Option AQuick Check:
PyTorch tensor = numbers container [OK]
- Thinking tensors store only text
- Confusing tensors with web tools
- Assuming tensors manage databases
Solution
Step 1: Recall PyTorch tensor creation syntax
PyTorch uses torch.tensor() to create tensors from lists or arrays.Step 2: Check each option's validity
torch.vector([1, 2, 3]), torch.array([1, 2, 3]), and torch.list([1, 2, 3]) use incorrect functions that do not exist in PyTorch.Final Answer:
torch.tensor([1, 2, 3]) -> Option AQuick Check:
Use torch.tensor() to create tensors [OK]
- Using torch.array which is not a PyTorch function
- Trying torch.list or torch.vector which don't exist
- Confusing PyTorch with NumPy syntax
import torch x = torch.tensor([2, 4]) y = torch.tensor([3, 1]) z = x + y print(z)
Solution
Step 1: Add tensors element-wise
Adding tensors x and y adds each element: 2+3=5 and 4+1=5.Step 2: Confirm output format
tensor([6, 4]) and tensor([2, 4, 3, 1]) show wrong sums or concatenation, and 'Error: cannot add tensors' is incorrect as addition is valid and produces tensor([5, 5]).Final Answer:
tensor([5, 5]) -> Option BQuick Check:
2+3=5 and 4+1=5 [OK]
- Thinking addition concatenates tensors
- Mixing element-wise sum with multiplication
- Expecting an error for valid addition
import torch x = torch.tensor([1, 2, 3]) y = torch.tensor([4, 5]) z = x + y print(z)
Solution
Step 1: Check tensor sizes
x has 3 elements, y has 2 elements; PyTorch cannot add tensors of different sizes element-wise.Step 2: Identify error cause
There is no missing import statement and tensor creation syntax is correct. 'No error, code runs fine' is wrong as code raises size mismatch error.Final Answer:
Tensors have different sizes, cannot add -> Option DQuick Check:
Tensor sizes must match for addition [OK]
- Ignoring size mismatch error
- Assuming PyTorch auto-adjusts sizes
- Thinking import or syntax is wrong
import torch x = torch.tensor([1, 2, 3]) y = torch.tensor([4, 5, 6]) # Choose the correct next line
Solution
Step 1: Understand element-wise multiplication and sum
x * y multiplies each element: [1*4, 2*5, 3*6] = [4, 10, 18]. Then sum() adds all: 4+10+18=32.Step 2: Evaluate options
torch.dot(x, y).sum() applies dot product then sum again (dot product is not element-wise multiplication, and summing a scalar is redundant). x.matmul(y) does matrix multiplication, not element-wise. x + y.sum() adds x to sum of y, not multiplication.Final Answer:
result = (x * y).sum() -> Option CQuick Check:
Element-wise multiply then sum = 32 [OK]
- Using matmul instead of element-wise multiply
- Applying sum twice incorrectly
- Adding tensors instead of multiplying
Ready to prove it?
Pass the test and this pattern moves unknown
