Model definition
- Machine Learning course
- Advanced
- Lesson 26/78
- 7 min
Define a model with nn.Module.
Model definition
Our model has two parameters: the slope w and the intercept b. Both are created with requires_grad=True so that autograd computes their gradients. The model is an ordinary Python function:
w = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
def model(x):
return x * w + b # (10, 1) * (1,) + (1,) -> (10, 1)Initially the parameters are zero; the model predicts 0 for every input.
The parameter list
Collecting the parameters to train in a list lets the update and gradient-zeroing steps be written with a single loop. nn.Module keeps this list for us with parameters():
parametreler = [w, b]
for p in parametreler:
print(p.shape, p.requires_grad)Writing the model as a function is instructive in small examples; in large models nn.Module manages the layers and parameters.
Practice Try the model
While the parameters are zero, print the sum of model(x) with item(). Then, inside torch.no_grad(), set w to 3 and b to 2 (w.fill_(3), b.fill_(2)) and print with torch.equal that the prediction equals y.
Starter code
import torch
x = torch.arange(10, dtype=torch.float32).unsqueeze(1)
y = 3 * x + 2
w = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
def model(x):
return x * w + b
# buraya yazSample solution
import torch
x = torch.arange(10, dtype=torch.float32).unsqueeze(1)
y = 3 * x + 2
w = torch.zeros(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
def model(x):
return x * w + b
print(model(x).sum().item())
with torch.no_grad():
w.fill_(3)
b.fill_(2)
print(torch.equal(model(x).detach(), y))Expected output
0.0 True
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does <code>model(x)</code> give while the parameters are zero?
- 0 for every example Correct answer
- x itself
- y
- An error
Explanation: x · 0 + 0 = 0.
2 Compute the model's prediction.
def model(x):
return x * w ____ b
def model(x):
return x * w + b
Explanation: Linear model: slope × input + intercept.
3 Match the pieces.
| Term | Meaning |
|---|---|
w |
Slope |
b |
Intercept |
requires_grad=True |
To be learned |
model(x) |
Prediction |