【问题标题】:Pytorch MSE loss function nan during training训练期间的Pytorch MSE损失函数nan
【发布时间】:2021-05-04 03:56:22
【问题描述】:

我正在尝试从波士顿数据集进行线性回归。自第一次迭代以来,MSE 损失函数为 nan。我尝试改变学习率和 batch_size 但没有用。

from torch.utils.data import TensorDataset , DataLoader
inputs  = torch.from_numpy(Features).to(torch.float32)
targets = torch.from_numpy(target).to(torch.float32)
train_ds = TensorDataset(inputs , targets)
train_dl = DataLoader(train_ds , batch_size = 5 , shuffle = True)
model = nn.Linear(13,1)
opt = optim.SGD(model.parameters(), lr=1e-5)
loss_fn = F.mse_loss
def fit(num_epochs, model, loss_fn, opt, train_dl):
    
    # Repeat for given number of epochs
    for epoch in range(num_epochs):
        
        # Train with batches of data
        for xb,yb in train_dl:
            
            # 1. Generate predictions
            pred = model(xb)
            
            # 2. Calculate loss
            loss = loss_fn(pred, yb)
            
            # 3. Compute gradients
            loss.backward()
            
            # 4. Update parameters using gradients
            opt.step()
            
            
            # 5. Reset the gradients to zero
            opt.zero_grad()
        
        # Print the progress
        if (epoch+1) % 10 == 0:
            print('Epoch [{}/{}], Loss: {}'.format(epoch+1, num_epochs, loss.item()))


fit(100, model, loss_fn , opt , train_dl)

output

【问题讨论】:

  • 乍一看,似乎是数据集(即Features)或模型初始化的问题。为了确定这一点,请将学习率设置为 0 或在每一步打印模型的预测。我想预测是 nan,因此损失变成 nan(不是相反)。
  • 首先,使用 nn.MSELoss 而不是 F.mse_loss(但我认为这不会产生影响)。其次,每 epoch 打印一次损失而不是每 10 次打印一次损失,也许一开始损失是一个数字。还要打印预测,这更重要。而且由于我们不知道您的数据是什么,因此很难提供进一步的帮助

标签: python pytorch


【解决方案1】:

注意:

  1. 使用标准化:x = (x - x.mean()) / x.std()
  2. y_train / y_test 必须是 (-1, 1) 形状。使用y_train.view(-1, 1)(如果 y_train 是 torch.Tensor 什么的)
  3. (不是你的情况,而是其他人)如果你使用torch.nn.MSELoss(reduction='sum'),那么你必须减少总和的意思。可以使用 torch.nn.MSELoss() 或在 train-loop 中完成:l = loss(y_pred, y) / y.shape[0]

例子:

    ...
    loss = torch.nn.MSELoss()  
    ...
    for epoch in range(num_epochs):  
        for x, y in train_iter:  
            y_pred = model(x)  
            l = loss(y_pred, y)  
            optimizer.zero_grad()  
            l.backward()  
            optimizer.step()  
        print("epoch {} loss: {:.4f}".format(epoch + 1, l.item()))

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2021-12-28
    相关资源
    最近更新 更多