【问题标题】:Pytorch model training loss does not improve. Are the logistic regression model parameters/weights not updating?Pytorch 模型训练损失没有改善。逻辑回归模型参数/权重没有更新吗?
【发布时间】:2021-05-05 01:23:55
【问题描述】:

我正在尝试使用以下模型对图像进行分类。训练损失似乎没有收敛/改善。你能检查一下代码,看看这可能是实现逻辑回归的模型问题吗?

一系列 10 个训练 epoch 得到的结果是:

epoch: 1, loss= -16.0369
epoch: 2, loss= -23.3950
epoch: 3, loss= -23.4226
epoch: 4, loss= -18.7254
epoch: 5, loss= -29.8720
epoch: 6, loss= -29.2601
epoch: 7, loss= -21.3710
epoch: 8, loss= -28.2535
epoch: 9, loss= -33.8465
epoch: 10, loss= -27.8332

带有优化器的模型代码:

class LogisticRegression(nn.Module):
    def __init__(self):
        super(LogisticRegression, self).__init__()
        self.linear = [] 
        self.linear.append(nn.Linear(in_features=28*28, out_features=1))
        self.linear = nn.Sequential(*self.linear)
        self.activation = nn.ReLU()
    
    def forward(self, x):
        y = self.activation(torch.sigmoid(self.linear(x)))
        return y

损失和优化器:

learn_rate = 0.01
criterion = nn.BCELoss()
optimizer = torch.optim.SGD(params = LR_model.parameters(), lr=learn_rate)

数据加载器生成“图像”和“标签” 训练循环段:

#forward 
        y_predicted = LR_model(images)
        total_loss = criterion(y_predicted, labels.unsqueeze(1))
        #backward
        total_loss.backward()
        #update
        optimizer.step()
        optimizer.zero_grad()
    # Print epoch result
    print(f'epoch: {epoch+1}, loss= {total_loss.item():.4f}')

【问题讨论】:

    标签: python machine-learning neural-network pytorch regression


    【解决方案1】:

    除了 sigmoid 激活之外,您不应使用 ReLU 激活。而是直接返回torch.sigmoid(self.linear(x))nn.BCELoss

    但是,为了数值稳定性,您应该使用nn.BCELossWithLogits:它结合了 sigmoid 层和二元交叉熵损失。在这种情况下,只输出 logits i.e. self.linear(x).

    【讨论】:

      猜你喜欢
      • 2018-01-03
      • 2022-10-07
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2019-09-18
      • 2020-01-20
      • 1970-01-01
      相关资源
      最近更新 更多