gpt4 book ai didi

python - 简单的 Pytorch 示例 - 训练损失并没有减少

转载 作者:行者123 更新时间:2023-12-01 02:11:24 25 4
gpt4 key购买 nike

我刚刚开始尝试学习 pytorch,无论它如何宣传,我都觉得它令人沮丧:)

在这里,我正在运行一个简单的回归作为实验,但由于损失似乎并没有随着每个时期(在训练中)而减少,我一定做错了什么——无论是在训练中还是在我收集 MSE 的方式中?

from __future__ import division
import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.utils.data as utils_data
from torch.autograd import Variable
from torch import optim, nn
from torch.utils.data import Dataset
import torch.nn.functional as F

from sklearn.datasets import load_boston


cuda=True


#regular old numpy
boston = load_boston()

x=boston.data
y=boston.target

x.shape

training_samples = utils_data.TensorDataset(x, y)
data_loader = utils_data.DataLoader(training_samples, batch_size=10)

len(data_loader) #number of batches in an epoch

#override this
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()

#all the layers
self.fc1 = nn.Linear(x.shape[1], 50)
self.drop = nn.Dropout(p=0.2)
self.fc2 = nn.Linear(50, 1)

#
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.drop(x)
x = self.fc2(x)

return x



net=Net()
if cuda:
net.cuda()
print(net)

# create a stochastic gradient descent optimizer
optimizer = optim.Adam(net.parameters())
# create a loss function (mse)
loss = nn.MSELoss()

# create a stochastic gradient descent optimizer
optimizer = optim.Adam(net.parameters())
# create a loss function (mse)
loss = nn.MSELoss()

# run the main training loop
epochs =20
hold_loss=[]

for epoch in range(epochs):
cum_loss=0.
for batch_idx, (data, target) in enumerate(data_loader):
tr_x, tr_y = Variable(data.float()), Variable(target.float())
if cuda:
tr_x, tr_y = tr_x.cuda(), tr_y.cuda()

# Reset gradient
optimizer.zero_grad()

# Forward pass
fx = net(tr_x)
output = loss(fx, tr_y) #loss for this batch
cum_loss += output.data[0]

# Backward
output.backward()

# Update parameters based on backprop
optimizer.step()
hold_loss.append(cum_loss)
#print(epoch+1, cum_loss) #

plt.plot(np.array(hold_loss))

enter image description here

最佳答案

好吧,我刚刚更改了行:

training_samples = utils_data.TensorDataset(torch.from_numpy(x), torch.from_numpy(y))

添加torch.from_numpy(否则,它会抛出错误,因此也不会运行),我得到的学习曲线如下所示: enter image description here

关于python - 简单的 Pytorch 示例 - 训练损失并没有减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com