PyTorch - RuntimeError:Assertion' cur_target> = 0&& cur_target< n_classes'失败

时间:2018-04-23 00:26:51

标签: python python-2.7 pandas dataframe pytorch

我试图与PyTorch建立神经网络,以预测学生的期末考试成绩。我已经这样做了 -

# Hyper Parameters
input_size = 2
hidden_size = 50
num_classes =21
num_epochs = 500
batch_size = 5
learning_rate = 0.1

# define a customise torch dataset
class DataFrameDataset(torch.utils.data.Dataset):
def __init__(self, df):
    self.data_tensor = torch.Tensor(df.as_matrix())

# a function to get items by index
def __getitem__(self, index):
    obj = self.data_tensor[index]
    input = self.data_tensor[index][0:-1]
    target = self.data_tensor[index][-1] - 1
    return input, target

# a function to count samples
def __len__(self):
    n, _ = self.data_tensor.shape
    return n

# load all data
data_i = pd.read_csv('dataset/student-mat.csv', header=None,delimiter=";")
data = data_i.iloc[:,30:33]

# normalise input data
for column in data:
# the last column is target
if column != data.shape[1] - 1:
    data[column] = data.loc[:, [column]].apply(lambda x: (x - x.mean()) / x.std())

# randomly split data into training set (80%) and testing set (20%)
msk = np.random.rand(len(data)) < 0.8
train_data = data[msk]
test_data = data[~msk]

# define train dataset and a data loader
train_dataset = DataFrameDataset(df=train_data)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

# Neural Network
class Net(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
    super(Net, self).__init__()
    self.fc1 = nn.Linear(input_size, hidden_size)
    self.sigmoid = nn.Sigmoid()
    self.fc2 = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    out = self.fc1(x)
    out = self.sigmoid(out)
    out = self.fc2(out)
    return out


net = Net(input_size, hidden_size, num_classes)

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Rprop(net.parameters(), lr=learning_rate)

# store all losses for visualisation
all_losses = []

# train the model by batch
for epoch in range(num_epochs):
    for step, (batch_x, batch_y) in enumerate(train_loader):
        # convert torch tensor to Variable
        X = Variable(batch_x)
        Y = Variable(batch_y.long())

        # Forward + Backward + Optimize
        optimizer.zero_grad()  # zero the gradient buffer
        outputs = net(X)
        loss = criterion(outputs, Y)
        all_losses.append(loss.data[0])
        loss.backward()
        optimizer.step()

        if epoch % 50 == 0:
            _, predicted = torch.max(outputs, 1)
            # calculate and print accuracy
            total = predicted.size(0)
            correct = predicted.data.numpy() == Y.data.numpy()

            print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Accuracy: %.2f %%'
                  % (epoch + 1, num_epochs, step + 1,
                      len(train_data) // batch_size + 1,
                      loss.data[0], 100 * sum(correct)/total))

我在第loss = criterion(outputs, Y)行收到错误,上面写着 - RuntimeError: Assertion 'cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/torch/lib/THNN/generic/ClassNLLCriterion.c:62

我无法弄清楚我做错了什么,因为我对此很陌生,而且我已经在这里检查了其他帖子,但是,他们不会这样做。似乎有所帮助。 data数据框看起来像 -

     30  31  32

0     5   6   6
1     5   5   6
2     7   8  10
3    15  14  15
4     6  10  10
5    15  15  15

任何人都可以告诉我我做错了什么以及如何纠正它。任何帮助表示赞赏!谢谢! :)

2 个答案:

答案 0 :(得分:1)

我的程序中有同样的错误 我只是意识到问题出在我网络中的输出节点数上 在我的程序中,我模型的输出节点数不等于数据标签数 输出的数量为1,目标标签的数量为10。然后我将输出的数量更改为10,没有错误

答案 1 :(得分:0)

假设您的代码是正确的,当类标签不在0到MAX范围内(在您的情况下为20)时会出现此问题。