为啥pytorch loss越来越高?

Posted

技术标签:

【中文标题】为啥pytorch loss越来越高?【英文标题】:why pytorch loss grow higher and higher?为什么pytorch loss越来越高? 【发布时间】:2021-08-28 09:01:07 【问题描述】:

我使用nn.BCEWithLogitsLoss() 当我训练我的模型时,损失越来越大,为什么?我该如何解决这个问题?

基本代码:

loss_fn = nn.BCEWithLogitsLoss()

def train_loop(dataloader, model, loss_fn, optimizer):
    for batch, (X, y) in enumerate(dataloader):
        #X,y=X.to(device), y.to(device)
        m = nn.Sigmoid()
        predict=model(X.float())
        loss=loss_fn(m(predict),y.unsqueeze(1).float())

        optimizer.zero_grad()
        loss.backward()# Calculate Gradients
        optimizer.step()# Update Weights

完整代码:

import pandas as pd
import numpy as np
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import torch.nn as nn

class myDataset(Dataset):
    def __init__(self,data,label):#, annotations_file, img_dir, transform=None, target_transform=None):
        df = pd.read_csv(data, encoding='gbk')
        df = df.fillna(value=0)
        self.data = np.array(df)

        df = pd.read_csv(label, encoding='gbk')
        df = df.fillna(value=0)
        self.label = np.array(df).reshape(-1)

        #self.transform = transform
        #self.target_transform = target_transform

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        return self.data[idx], self.label[idx]

class Network(nn.Module):
    def __init__(self):
        super(Network, self).__init__()
        self.flatten = nn.Flatten()
        self.network = nn.Sequential(
            #nn.Conv2d(in_channels=1, out_channels=6,kernel_size=5),
            nn.Linear(27, 100),
            nn.ReLU(),
            nn.Linear(100, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 1),
            nn.ReLU()
        )
    def forward(self, x):
        x=self.flatten(x)
        return self.network(x)

def train_loop(dataloader, model, loss_fn, optimizer):
    for batch, (X, y) in enumerate(dataloader):
        #X,y=X.to(device), y.to(device)
        m = nn.Sigmoid()
        predict=model(X.float())
        loss=loss_fn(m(predict),y.unsqueeze(1).float())

        optimizer.zero_grad()
        loss.backward()# Calculate Gradients
        optimizer.step()# Update Weights

        if batch % 100 == 0:
            loss, current = loss.item(), batch * len(X)
            print(f"loss: loss:>7f  [current:>5d/len(dataloader.dataset):>5d]")

model=Network()
batch_size = 64
learning_rate = 1e-3
epochs = 5
loss_fn = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

trainDataloader = DataLoader(myDataset("mydata/traindata.csv","mydata/trainlabel.csv"),batch_size=batch_size,shuffle=True)
train_loop(trainDataloader, model, loss_fn, optimizer)

【问题讨论】:

【参考方案1】:

我认为你不需要这条线m = nn.Sigmoid()

文档上说:This loss combines a Sigmoid layer and the BCELoss in one single class.你可以查一下损失here。

def train_loop(dataloader, model, loss_fn, optimizer):
    for batch, (X, y) in enumerate(dataloader):
        #X,y=X.to(device), y.to(device)
        predict=model(X.float())
        loss=loss_fn(predict,y.unsqueeze(1).float())

【讨论】:

以上是关于为啥pytorch loss越来越高?的主要内容,如果未能解决你的问题,请参考以下文章

[深度学习][pytorch][原创]crnn在高版本pytorch上训练loss为nan解决办法

PyTorch学习----01

pytorch显存越来越多的一个自己没注意的原因

为啥损失减少但准确性也降低(Pytorch,LSTM)?

cnn训练准确率很高,测试准确率很低(loss有一直下降)是为啥?

pytorch loss