42. Make Predictions with Model #1167
-
As I am following the course, I have implemented basic model with very basic # Create a Linear Regression model class
class LinearRegressionModel(nn.Module): # <- almost everything in PyTorch is a nn.Module (think of this as neural network lego blocks)
def __init__(self):
super().__init__()
self.weights = nn.Parameter(torch.randn(1, # <- start with random weights (this will get adjusted as the model learns)
dtype=torch.float), # <- PyTorch loves float32 by default
requires_grad=True) # <- can we update this value with gradient descent?)
self.bias = nn.Parameter(torch.randn(1, # <- start with random bias (this will get adjusted as the model learns)
dtype=torch.float), # <- PyTorch loves float32 by default
requires_grad=True) # <- can we update this value with gradient descent?))
# Forward defines the computation in the model
def forward(self, x: torch.Tensor) -> torch.Tensor: # <- "x" is the input data (e.g. training/testing features)
return self.weights * x + self.bias # <- this is the linear regression formula (y = m*x + b) What I am not understanding is when we create an object of class and try to do prediction in inference mode, how is it triggering the forward method in below code? # Set manual seed since nn.Parameter are randomly initialized
torch.manual_seed(42)
# Create an instance of the model (this is a subclass of nn.Module that contains nn.Parameter(s))
model_0 = LinearRegressionModel()
# Make predictions with model
with torch.inference_mode():
y_preds = model_0(X_test)
y_preds Also how can we access |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi there,
This is because a For the second question, Best wishes. |
Beta Was this translation helpful? Give feedback.
Hi there,
This is because a
__call__()
method is defined in nn.Module.For the second question,
with torch.inference_mode():
alters properties related to training. You can read more about it in the documentation. If it is thewith
part of the statement you want to know more about, have a look at this discussion.Best wishes.