Hi guys, I am trying to learn Pytorch by using it for the Titanic kaggle competition. I completed the intro CIFAR-10 tutorial and decided to try to build a simple fully connected neural net. The problem is that I keep running into this error: ‘Assertion `cur_target >= 0 && cur_target < n_classes’ failed.’. I think I am loading the data correctly (I tried to apply the Data Loading and Processing tutorial) and I believe I have correctly set up the neural net. Of course, there’s always the chance that I’m missing something completely obvious and it’s a quick fix. I have attached my jupyter notebook.
btw I am running this on macOS High Sierra with Python 3.6 and Anaconda without CUDA.
Thanks!
Brian
import torch
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

train_file = open("./data/train.csv", "r")

train = pd.read_csv(train_file)

def process(df):
    df_X = df.join(pd.get_dummies(train['Pclass'], prefix="class"))
    df_X = df_X.join(pd.get_dummies(train['Sex']))
    df_X = df_X.join(pd.get_dummies(train['Embarked'], prefix="port"))
    df_X = df_X.join(pd.get_dummies(train['SibSp'], prefix="sibsp"))
    df_X = df_X.join(pd.get_dummies(train['Parch'], prefix="parch"))
    
    df_Y = train['Survived']
    
    df_X.drop(['Survived', 'Name', 'PassengerId', 'Sex', 'Embarked',
                  'SibSp', 'Parch', 'Cabin', 'Ticket'], axis=1, inplace=True)
    
    return df_X, df_Y

train_X, train_y = process(train)
print(train_X.shape)
print(train_Y.shape)

(891, 25)
(891,)

train_X.head()



    .dataframe thead tr:only-child th {
        text-align: right;
    }
.dataframe thead th {
    text-align: left;
}

.dataframe tbody tr th {
    vertical-align: top;
}


Expand Table
  
    
      
      Pclass
      Age
      Fare
      class_1
      class_2
      class_3
      female
      male
      port_C
      port_Q
      ...
      sibsp_4
      sibsp_5
      sibsp_8
      parch_0
      parch_1
      parch_2
      parch_3
      parch_4
      parch_5
      parch_6
    
  
  
    
      0
      3
      22.0
      7.2500
      0
      0
      1
      0
      1
      0
      0
      ...
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
    
    
      1
      1
      38.0
      71.2833
      1
      0
      0
      1
      0
      1
      0
      ...
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
    
    
      2
      3
      26.0
      7.9250
      0
      0
      1
      1
      0
      0
      0
      ...
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
    
    
      3
      1
      35.0
      53.1000
      1
      0
      0
      1
      0
      0
      0
      ...
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
    
    
      4
      3
      35.0
      8.0500
      0
      0
      1
      0
      1
      0
      0
      ...
      0
      0
      0
      1
      0
      0
      0
      0
      0
      0
    
  

5 rows × 25 columns

train_Y.head()

0    0
1    1
2    1
3    1
4    0
Name: Survived, dtype: int64

import os
from torch.utils.data import Dataset, DataLoader
class TitanicDataset(Dataset):

    def __init__(self, X, Y):
        self.X = X
        self.Y = Y

    def __len__(self):
        return len(self.X)

    def __getitem__(self, idx):
        X = self.X.iloc[idx].as_matrix().astype('double')
        X = X.reshape(-1, 25)
        Y = self.Y.iloc[idx].astype('double')

        return torch.from_numpy(X).double(), int(Y)

from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(25, 100)
        self.fc2 = nn.Linear(100, 100)
        self.fc3 = nn.Linear(100, 50)
        self.fc4 = nn.Linear(50, 20)
        self.fc5 = nn.Linear(20, 10)
        self.fc6 = nn.Linear(10, 1)

    def forward(self, x):
        x = self.fc1(x).clamp(min=0)
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = F.relu(self.fc4(x))
        x = F.relu(self.fc5(x))
        x = self.fc6(x)
        return x


net = Net()

import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

dataset = TitanicDataset(train_X, train_Y)
dataloader = DataLoader(dataset, batch_size=4,
                        shuffle=True, num_workers=4)
print(dataloader.dataset[0][0].shape)
print(dataloader.dataset[0][0].size())
print(type(dataloader.dataset[0][0]))
print(type(dataloader.dataset[0][1]))

torch.Size([1, 25])
torch.Size([1, 25])
<class 'torch.DoubleTensor'>
<class 'int'>

for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(dataloader, 0):
        # get the inputs
        inputs, labels = data

        # wrap them in Variable
        inputs, labels = Variable(inputs.float()), Variable(labels.float())

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        labels = labels.long()
        loss = criterion(outputs[:,0], labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.data[0]
        if i % 100 == 99:    # print every 100 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 100))
            running_loss = 0.0
''
print('Finished Training')

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-375-f4e44e3f5cbb> in <module>()
     15         outputs = net(inputs)
     16         labels = labels.long()
---> 17         loss = criterion(outputs[:,0], labels)
     18         loss.backward()
     19         optimizer.step()


/Users/brian/anaconda/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)


/Users/brian/anaconda/lib/python3.6/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    480         _assert_no_grad(target)
    481         return F.cross_entropy(input, target, self.weight, self.size_average,
--> 482                                self.ignore_index)
    483 
    484 


/Users/brian/anaconda/lib/python3.6/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index)
    744                 True, the loss is averaged over non-ignored targets.
    745     """
--> 746     return nll_loss(log_softmax(input), target, weight, size_average, ignore_index)
    747 
    748 


/Users/brian/anaconda/lib/python3.6/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index)
    670     dim = input.dim()
    671     if dim == 2:
--> 672         return _functions.thnn.NLLLoss.apply(input, target, weight, size_average, ignore_index)
    673     elif dim == 4:
    674         return _functions.thnn.NLLLoss2d.apply(input, target, weight, size_average, ignore_index)


/Users/brian/anaconda/lib/python3.6/site-packages/torch/nn/_functions/thnn/auto.py in forward(ctx, input, target, *args)
     45         output = input.new(1)
     46         getattr(ctx._backend, update_output.name)(ctx._backend.library_state, input, target,
---> 47                                                   output, *ctx.additional_args)
     48         return output
     49 


RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed.  at /Users/soumith/miniconda2/conda-bld/pytorch_1503975723910/work/torch/lib/THNN/generic/ClassNLLCriterion.c:62