mrdbourke / pytorch-deep-learning

Materials for the Learn PyTorch for Deep Learning: Zero to Mastery course.
https://learnpytorch.io
MIT License
10.44k stars 3.08k forks source link

loss remains the same #399

Open Pritush09 opened 1 year ago

Pritush09 commented 1 year ago

Discussed in https://github.com/mrdbourke/pytorch-deep-learning/discussions/398

Originally posted by **Pritush09** April 13, 2023 class LinearRegressionModel2(nn.Module): def __init__(self): super().__init__() self.linear_layer = nn.Linear(in_features=1,out_features=1) # refer above # Define the forward computation (input data x flows through nn.Linear()) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.linear_layer(x) # Set the manual seed when creating the model (this isn't always need but is used for demonstrative purposes, try commenting it out and seeing what happens) torch.manual_seed(42) model_1 = LinearRegressionModel2() model_1, model_1.state_dict() # Create the loss function loss_fn = nn.MSELoss() # MAE loss is same as L1Loss # Create the optimizer optimizer = torch.optim.SGD(params=model_0.parameters(), # parameters of target model to optimize lr=0.01) # learning rate (how much the optimizer should change parameters at each step, higher=more (less stable), lower=less (might take a long time)) torch.manual_seed(42) # Set the number of epochs epochs = 1000 # Put data on the available device # Without this, error will happen (not all model/data on device) X_train = X_train.to(device) X_test = X_test.to(device) y_train = y_train.to(device) y_test = y_test.to(device) for epoch in range(epochs): ### Training model_1.train() # train mode is on by default after construction # 1. Forward pass y_pred = model_1(X_train) # 2. Calculate loss loss = loss_fn(y_pred, y_train) # 3. Zero grad optimizer optimizer.zero_grad() # 4. Loss backward loss.backward() # 5. Step the optimizer optimizer.step() ### Testing model_1.eval() # put the model in evaluation mode for testing (inference) # 1. Forward pass with torch.inference_mode(): test_pred = model_1(X_test) # 2. Calculate the loss test_loss = loss_fn(test_pred, y_test) if epoch % 100 == 0: print(f"Epoch: {epoch} | Train loss: {loss} | Test loss: {test_loss}") and after these steps loss is not going down
HrushikeshChaphalkar commented 1 year ago

While defining the model, in the init method, you should pass the argument (self, x) instead of (self).


class LinearRegressionModel2(nn.Module):
    def __init__(self, x):
        super().init()
        self.linear_layer = nn.Linear(in_features=1,out_features=1) # refer above```
mar-ina-thal commented 1 year ago

Hello , i had similar issue while using nn.L1Loss(), but when i switched to nn.MSELoss() it worked fine. Can you please explain in which case do we have to add x in the init function?

While defining the model, in the init method, you should pass the argument (self, x) instead of (self).

class LinearRegressionModel2(nn.Module):
    def __init__(self, x):
        super().init()
        self.linear_layer = nn.Linear(in_features=1,out_features=1) # refer above```