使用Pytorch进行线性回归时损失不减少

我正在使用Pytorch处理一个线性回归问题。我使用的数据集是Kaggle的房价数据。在训练模型时,我发现损失没有减少。损失显示出一种不规则的模式。这是我在100个epoch后得到的损失值:

Epoch [10/100], Loss: 222273830912.0000Epoch [20/100], Loss: 348813688832.0000Epoch [30/100], Loss: 85658296320.0000Epoch [40/100], Loss: 290305572864.0000Epoch [50/100], Loss: 59399933952.0000Epoch [60/100], Loss: 80360054784.0000Epoch [70/100], Loss: 90352918528.0000Epoch [80/100], Loss: 534457679872.0000Epoch [90/100], Loss: 256064503808.0000Epoch [100/100], Loss: 102400483328.0000

这是我的代码:

import torchimport numpy as npfrom torch.utils.data import TensorDatasetimport torch.nn as nnfrom torch.utils.data import DataLoaderimport torch.nn.functional as Finputs = normalized_Xtargets = np.array(train_y)# Tensorsinputs = torch.from_numpy(inputs)targets = torch.from_numpy(targets)targets = targets.view(-1, 1)train_ds = TensorDataset(inputs, targets.squeeze())batch_size = 5train_dl = DataLoader(train_ds, batch_size, shuffle=True)model = nn.Linear(10, 1)# Define Loss funcloss_fn = F.mse_loss# Optimizeropt = torch.optim.SGD(model.parameters(), lr = 1e-1)num_epochs = 100model.train()for epoch in range(num_epochs):    # Train with batches of data    for xb, yb in train_dl:        # 1. Generate predictions        pred = model(xb.float())        # 2. Calculate loss        yb = yb.view(yb.size(0), -1)        loss = loss_fn(pred, yb.float())            # 3. Compute gradients        loss.backward()        # 4. Update parameters using gradients        opt.step()        # 5. Reset the gradients to zero        opt.zero_grad()    if (epoch+1) % 10 == 0:        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch +                                                   1, num_epochs,                                                    loss.item()))

回答:

我运行了您提供的代码,并得到了以下错误:

    p.py:38: UserWarning: Using a target size (torch.Size([50])) that is different to the input size (torch.Size([50, 1])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.

您的问题是由于predyb的维度不同引起的。

以下代码展示了如何解决这个问题

这个讨论详细展示了解决方案https://discuss.pytorch.org/t/target-size-torch-size-10-must-be-the-same-as-input-size-torch-size-2/72354/6

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注