Can we write test_step function outside the FOR epoch loop? #1055
Replies: 1 comment 3 replies
-
@SailSabnis, I noticed that ur models test losses are increasing, indicating that it is starting to overfit. This suggests that the model is learning the training set well but struggling to generalize to the test set. To overcome this issue, consider introducing BatchNorm2d layers into ur model. These can improve generalization and stabilize training by normalizing activations across each batch.
class CNNV2(nn.Module):
def __init__(self, input, output, hidden):
super(CNNV2, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels=input, out_channels=hidden, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden),
nn.ReLU(),
nn.Conv2d(in_channels=hidden, out_channels=hidden*2, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden*2),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(in_channels=hidden*2, out_channels=hidden*4, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden*4),
nn.ReLU(),
nn.Conv2d(in_channels=hidden*4, out_channels=hidden, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(hidden),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(hidden*7*7, output)
)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
return self.classifier(x)
# Instantiation of model
modelv2 = CNNV2(input=1, output=10, hidden=64) And the answer to ur question
Yes u can call test function outside the loop. It should run test at the end of final train epoch. But the purpose of doing test after train within loop is to monitor the train and test losses and accuracies. By observing these metrics, we can determine if the model is:
|
Beta Was this translation helpful? Give feedback.
-
Trying to understand why is it necessary to put the test_step function inside the For epoch loop along with the train_step function. We are not optimising anything in test_step. I have been running my model by keeping it outside and not seeing any improvement in test accuracy. So I tried putting it inside the epoch for loop and ran it for 20 epochs. Optimizer is Adam with Lr or 0.001. How to reduce the test loss to under 0.01?
Full model arch
My Confusion Matrix is all over the place PLEASE HELP!
Beta Was this translation helpful? Give feedback.
All reactions