NN回归损失值不下降

NN regression loss value not decreasing

我正在使用 Pytorch 训练神经网络来预测 Boston dataset 的预期价格。 网络看起来像这样:

from sklearn.datasets import load_boston
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.optim as optim

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(13, 128)
        self.fc2 = nn.Linear(128, 64)
        self.fc3 = nn.Linear(64, 32)
        self.fc4 = nn.Linear(32, 16)
        self.fc5 = nn.Linear(16,1)

    def forward(self, x):
       x = self.fc1(x)
       x = self.fc2(x)
       x = F.relu(x)
       x = self.fc3(x)
       x = F.relu(x)
       x = self.fc4(x)
       x = F.relu(x)
       return self.fc5(x)

和数据加载器:

class BostonData(Dataset):
    __xs = []
    __ys = []

    def __init__(self, train = True):
        df = load_boston()
        index = int(len(df["data"]) * 0.7)
        if train:
            self.__xs = df["data"][0:index]
            self.__ys = df["target"][0:index]
        else:
            self.__xs = df["data"][index:]
            self.__ys = df["target"][index:]

    def __getitem__(self, index):
        return self.__xs[index], self.__ys[index]

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

在我的第一次尝试中,我没有添加 ReLU 单元,但经过一些研究后,我发现添加它们是一种常见的做法,但事实并非如此为我锻炼。

这里是训练代码:

dset_train = BostonData(train = True)
dset_test = BostonData(train = False)
train_loader = DataLoader(dset_train, batch_size=30, shuffle=True)
test_loader = DataLoader(dset_train, batch_size=30, shuffle=True)


optimizer = optim.Adam(net.parameters(), lr = 0.001)
criterion = torch.nn.MSELoss() 
EPOCHS = 10000

lloss = []

for epoch in range(EPOCHS):
    for trainbatch in train_loader:
        X,y = trainbatch
        net.zero_grad()
        output = net(X.float())
        loss = criterion(output, y)
        loss.backward()
        optimizer.step()
    lloss.append(loss)
    print(loss)

经过 10k 个 epoch 后,损失图如下所示

我没有看到任何明显的减少。 我不知道我是否搞砸了 torch.nn.MSELoss()optimizer 或网络拓扑,所以任何帮助将不胜感激。

编辑: 改变学习率和规范化数据对我不起作用。我添加了行 self.__xs = (self.__xs - self.__xs.mean()) / self.__xs.std() 并更改为 lr = 0.01。损失图与第一个非常相似。

lr = 0.01 并在 1000 个纪元后归一化的相同图:

你每个时期追加一次 lloss,这是正确的,但你追加 loss(仅使用最后一批),你应该追加 avg_train_loss

尝试:

for epoch in range(EPOCHS):
    avg_train_loss = 0
    for trainbatch in train_loader:
        X,y = trainbatch
        net.zero_grad()
        output = net(X.float())
        loss = criterion(output, y)
        loss.backward()
        optimizer.step()
        avg_train_loss += loss.item() / len(train_loader)
    lloss.append(avg_train_loss)