损失未涵盖新元

时间:2019-02-02 22:14:16

标签: pytorch

我正在使用PyTorch线性回归损失,但我的SGD损失没有收敛。 用例-

  • 我使用MNIST数据集,并实现图像分类进行分类手写数字0和1。
  • 然后使用Logistic回归模型: model = nn.Linear(input_size,num_classes)
  • 创建了自定义损失函数。
  • 训练模型,其中我将标签从0,1转换为-1,1。转换标签从0,1到1,1
  • 确定损失。

  total_step = len(train_loader)
    for epoch in range(num_epochs):
       for i, (images, labels) in enumerate(train_loader):
            # Reshape images to (batch_size, input_size)
            images = images.reshape(-1, 28*28) 
            #Convert labels from 0,1 to -1,1
            labels = Variable(2*(labels.float()-0.5))
            # Forward pass
            outputs = model(images)
            # we need maximum value of two class prediction
            oneout = torch.max(outputs.data,1)[0]
            loss = loss_criteria(oneout, labels)       

        # Backward and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()        

        if (i+1) % 100 == 0:
           print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
               .format(epoch+1, num_epochs, i+1, total_step, loss.item()))

损输出:0.8445,0.6883,0.7976,0.8133,0.8289,0.7195。如您所见,损失没有收敛。 预期结果 : 0.8445,0.8289,0.8133,0.7976,0.7195,0.6883一路零....

0 个答案:

没有答案