【问题标题】:Pytorch nll_loss returning a constant loss during training loopPytorch nll_loss 在训练循环期间返回恒定损失
【发布时间】:2021-08-02 19:21:40
【问题描述】:

我有一个图像二元分类问题,我想将图像分类为antbee。我已经刮掉了图像,我做了所有的清洁、重塑、转换为灰度。图像大小为200x200 一通道灰度。在跳转到Conv Nets. 之前,我首先想使用Feed Forwad NN 解决这个问题。

我在训练循环中遇到的问题是我得到一个常量 loss 我正在使用 Adam 优化器,F.log_softmax 用于网络中的最后一层以及 nll_loss 函数。到目前为止,我的代码如下所示:

FF - 网络

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(in_features , 64)
        self.fc2 = nn.Linear(64, 64)
        self.fc3 = nn.Linear(64, 32)
        self.fc4 = nn.Linear(32, 2)
        
    def forward(self, X):
        X = F.relu(self.fc1(X))
        X = F.relu(self.fc2(X))
        X = F.relu(self.fc3(X))
        X = F.log_softmax(self.fc4(X), dim=1)
        return X
    
net = Net()

训练循环。

optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
EPOCHS = 10
BATCH_SIZE = 5
for epoch in range(EPOCHS):
    print(f'Epochs: {epoch+1}/{EPOCHS}')
    for i in range(0, len(y_train), BATCH_SIZE):
        X_batch = X_train[i: i+BATCH_SIZE].view(-1,200 * 200)
        y_batch = y_train[i: i+BATCH_SIZE].type(torch.LongTensor)
        
        net.zero_grad() ## or you can say optimizer.zero_grad()
        
        outputs = net(X_batch)
        loss = F.nll_loss(outputs, y_batch)
        loss.backward()
        optimizer.step()
    print("Loss", loss)

我怀疑问题可能与我的批处理和损失函数有关。我将不胜感激任何帮助。 注意:图片为(200, 200)形状的灰度图。

【问题讨论】:

    标签: python neural-network jupyter-notebook pytorch recurrent-neural-network


    【解决方案1】:

    我一直在等待答案,但我什至无法获得评论。我自己想出了解决方案,也许这可以帮助将来的人。

    class Net(nn.Module):
        def __init__(self):
            super().__init__()
            self.fc1 = nn.Linear(200 * 200 , 64) # 200 * 200 are in_features, which is an image of shape 200*200 (gray image)
            self.fc2 = nn.Linear(64, 64)
            self.fc3 = nn.Linear(64, 32)
            self.fc4 = nn.Linear(32, 2)
            
        def forward(self, X):
            X = F.relu(self.fc1(X))
            X = F.relu(self.fc2(X))
            X = F.relu(self.fc3(X))
            X = self.fc4(X) # I removed the activation function here, 
            return X
        
    net = Net()
    
    # I changed the loss function to CrossEntropyLoss() since i didn't apply activation function on the last layer
    
    loss_function = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
    
    EPOCHS = 10
    BATCH_SIZE = 5
    for epoch in range(EPOCHS):
        print(f'Epochs: {epoch+1}/{EPOCHS}')
        for i in range(0, len(y_train), BATCH_SIZE):
            X_batch = X_train[i: i+BATCH_SIZE].view(-1, 200 * 200)
            y_batch = y_train[i: i+BATCH_SIZE].type(torch.LongTensor)
            
            net.zero_grad() ## or you can say optimizer.zero_grad()
            
            outputs = net(X_batch)
            loss = loss_function(outputs, y_batch)
            loss.backward()
            optimizer.step()
        print("Loss", loss)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 2021-08-01
      • 2017-08-24
      • 2017-11-10
      • 2020-08-30
      相关资源
      最近更新 更多