Struggling with thread: [0,0,0] Assertion `t >= 0 && t < n_classes'

username: dbadm
cooked: <p>Hello all! I have been trying to create a neural network to analyze some datapoints. A brief description of the data would be that it is in a csv file with each row having 23 columns, the first 20 of which are actually the points themselves (x, y, z format). Anyhow, I have got a simple architecture below, but when it runs into the loss optimization function it throws this error:<br>
C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\aten\src\ATen\native\cuda\Loss.cu:250: block: [0,0,0], thread: [0,0,0] Assertion <code>t &gt;= 0 &amp;&amp; t &lt; n_classes</code> failed.</p>
<p>Code:</p>
<pre><code class="lang-auto">batch_size = 1
num_workers = 6
np.set_printoptions(threshold=np.inf)
torch.set_printoptions(threshold=torch.inf)

class ASLDataset(torch.utils.data.Dataset): # inheriting from Dataset class
    def __init__(self, csv_file, root_dir="", transform=transforms.ToTensor()):
        self.annotation_df = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

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

    def __getitem__(self, idx):
        dataPoints = []
        for i in range(20):
            dataPoints.append(eval(self.annotation_df.iloc[idx, i]))
        dataPoints = torch.tensor(dataPoints, device='cuda')
        print(str(idx) + " " + str(dataPoints.shape))
        label = self.annotation_df.iloc[idx, 21]
        label = torch.tensor(int(label), device='cuda')
        print(label)
        return dataPoints, label


train_dataset = ASLDataset('./ASL_Alph_Train.csv') #, train_transform)
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)

val_dataset = ASLDataset('./ASL_Alph_Test.csv')  # val.csv
val_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=num_workers)

classes = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'nothing', 'O', 'P', 'Q', 'R', 'S', 'space', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z') #

# for i in range(30):
#     ASLDataset.__getitem__(train_dataset, i)

class VSNetwork(nn.Module):
    def __init__(self):
        super(VSNetwork, self).__init__()

        self.conv1 = nn.Conv1d(in_channels=20, out_channels=12, kernel_size=3)
        self.fc1 = nn.Linear(12, 64)
        self.fc2 = nn.Linear(64, 1)

    def forward(self, x):
        x = self.conv1(x)
        x = x.view(x.size(0), -1)
        x = self.fc1(x)
        x = self.fc2(x)

        return x
model = VSNetwork()

loss_fn = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.001, weight_decay=0.0001) 
</code></pre>
<p>I have been searching for several hours to no avail, I would really appreciate help! God bless you all!</p>

username: ptrblck
cooked: <p>Based on the error message it seems your target contains out of bounds values so make sure all values are in the range <code>[0, nb_classes-1]</code>.</p>

username: dbadm
cooked: <p>I believe that I included my classes in my code. It’s an array so it should only start at 0. When I print out the labels in my data loader they are all from 0 to 5, which is what I expect.</p>