我正在使用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.
您的问题是由于pred
和yb
的维度不同引起的。
以下代码展示了如何解决这个问题
这个讨论详细展示了解决方案https://discuss.pytorch.org/t/target-size-torch-size-10-must-be-the-same-as-input-size-torch-size-2/72354/6