【问题标题】:Validation losses increasing after a few epochs验证损失在几个 epoch 后增加
【发布时间】:2022-01-10 21:56:09
【问题描述】:

我正在构建一个小型 CNN 模型,以使用 Plant Village 数据集预测植物作物病害。它由有病和无病的39类不同物种组成。

CNN 模型

class CropDetectCNN(nn.Module):
    # initialize the class and the parameters
    def __init__(self):
        super(CropDetectCNN, self).__init__()

        # convolutional layer 1 & max pool layer 1
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 16, kernel_size=3),
            nn.MaxPool2d(kernel_size=2))

        # convolutional layer 2 & max pool layer 2
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=3, padding=1, stride=2),
            nn.MaxPool2d(kernel_size=2))

        #Fully connected layer
        self.fc = nn.Linear(32*28*28, 39)
        
        

    # Feed forward the network
    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.fc(out)
        return out


model = CropDetectCNN()

培训

criterion = nn.CrossEntropyLoss()  # this include softmax + cross entropy loss
optimizer = torch.optim.Adam(model.parameters())

def batch_gd(model, criterion, train_loader, validation_loader, epochs):
    train_losses = np.zeros(epochs)
    test_losses = np.zeros(epochs)
    validation_losses = np.zeros(epochs)
    
    for e in range(epochs):
        t0 = datetime.now()
        train_loss = []
        model.train()
        for inputs, targets in train_loader:
            inputs, targets = inputs.to(device), targets.to(device)

            optimizer.zero_grad()

            output = model(inputs)

            loss = criterion(output, targets)

            train_loss.append(loss.item())  # torch to numpy world

            loss.backward()
            optimizer.step()
            
        
        train_loss = np.mean(train_loss)

        validation_loss = []

        for inputs, targets in validation_loader:
            
            model.eval()

            inputs, targets = inputs.to(device), targets.to(device)

            output = model(inputs)

            loss = criterion(output, targets)

            validation_loss.append(loss.item())  # torch to numpy world
        
        
        
        validation_loss = np.mean(validation_loss)

        train_losses[e] = train_loss
        validation_losses[e] = validation_loss

        dt = datetime.now() - t0

        print(
            f"Epoch : {e+1}/{epochs} Train_loss: {train_loss:.3f} Validation_loss: {validation_loss:.3f} Duration: {dt}"
        )

    return train_losses, validation_losses

# Running the function
train_losses, validation_losses = batch_gd(
    model, criterion, train_loader, validation_loader, 5
)

# And theses are results:
Epoch : 1/5 Train_loss: 1.164 Validation_loss: 0.861 Duration: 0:10:59.968168
Epoch : 2/5 Train_loss: 0.515 Validation_loss: 0.816 Duration: 0:10:49.199842
Epoch : 3/5 Train_loss: 0.241 Validation_loss: 1.007 Duration: 0:09:56.334155
Epoch : 4/5 Train_loss: 0.156 Validation_loss: 1.147 Duration: 0:10:12.625819
Epoch : 5/5 Train_loss: 0.135 Validation_loss: 1.603 Duration: 0:09:56.746308

验证损失不应该随着 epochs 减少吗?那为什么是先减后增呢?

我应该如何设置 epoch 的数量,为什么?

非常感谢任何帮助!

【问题讨论】:

  • 您在这篇文章中似乎有几个问题 - 请在不同的简洁帖子中提出每个问题。随意链接上下文的问题
  • 好的,我会尽力纠正,谢谢!

标签: python neural-network pytorch conv-neural-network


【解决方案1】:

当您的验证损失减少后上升时,您将面临“过度拟合”的现象。你应该在那个时候停止训练并尝试使用一些技巧来避免过度拟合。

当你的梯度在推理过程中不断更新时,可能会得到不同的预测,所以try explicitly "stop" them from updating with torch.no_grad()

【讨论】:

  • 好的,我明白了。所以我必须将 epoch 的数量减少到 2,因为它在之后开始增加?关于预测,在验证可以解决之前添加with torch.no_grad():?但在 90 分钟结束后,我会为我的第二个问题创建另一个帖子。
  • So I have to reduce the number of epochs to 2 - 在中断训练过程之前,我最好降低学习率,看看误差是否会降低
  • 我知道我在 Adam 优化器中使用了自适应学习率:optimizer = torch.optim.Adam(model.parameters())。我应该如何降低学习率?
  • 在torch.optim.Adam 中设置lr=1e-4 或lr=3e-4 pytorch.org/docs/stable/generated/torch.optim.Adam.html
猜你喜欢
  • 2022-11-15
  • 2019-08-14
  • 2021-04-13
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2018-09-06
  • 2020-03-27
  • 1970-01-01
相关资源
最近更新 更多