简单的Pytorch示例 - 训练损失不会减少

时间:2018-02-08 00:27:07

标签: python pytorch

我刚刚开始尝试学习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

1 个答案:

答案 0 :(得分:2)

嗯,我刚改变了一句话:

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

添加torch.from_numpy(否则,它会抛出错误,因此也不会运行),我得到的学习曲线看起来像这样: enter image description here