Target 3988 is out of bounds

Mohamed_Nabih: <p>import torch<br>
from torch.autograd import Variable<br>
import torch.nn as nn<br>
from fela import feat, labels<br>
from Dataloader import train_loader, test_loader, X_train, X_test, X_val, y_train, y_test, y_val</p>
<p>input_size = 13<br>
hidden1_size = 13<br>
hidden2_size = 64<br>
hidden3_size = 128<br>
hidden4_size = 256<br>
hidden5_size = 1024<br>
output_size = 3988</p>
<p>class DNN(nn.Module):</p>
<pre><code>def __init__(self, input_size, hidden1_size, hidden2_size, hidden3_size, hidden4_size, hidden5_size, output_size):
    super(DNN, self).__init__()
    self.fc1 = nn.Linear(input_size, hidden1_size)
    self.drp1 = nn.Dropout(p=0.2, inplace=False)
    self.relu1 = nn.ReLU()
    self.fc2 = nn.Linear(hidden1_size, hidden2_size)
    self.drp2 = nn.Dropout(p=0.2, inplace=False)
    self.relu2 = nn.ReLU()
    self.fc3 = nn.Linear(hidden2_size, hidden3_size)
    self.drp3 = nn.Dropout(p=0.2, inplace=False)
    self.relu3 = nn.ReLU()
    self.fc4 = nn.Linear(hidden3_size, hidden4_size)
    self.drp4 = nn.Dropout(p=0.2, inplace=False)
    self.relu4 = nn.ReLU()
    self.fc5 = nn.Linear(hidden4_size, hidden5_size)
    self.drp5 = nn.Dropout(p=0.2, inplace=False)
    self.relu5 = nn.ReLU()
    self.fc6 = nn.Linear(hidden5_size, output_size)

def forward(self, x):
    out = self.fc1(x)
    out = self.drp1(out)
    out = self.relu1(out)
    out = self.fc2(out)
    out = self.drp2(out)
    out = self.relu2(out)
    out = self.fc3(out)
    out = self.drp3(out)
    out = self.relu3(out)
    out = self.fc4(out)
    out = self.drp4(out)
    out = self.relu4(out)
    out = self.fc5(out)
    out = self.drp5(out)
    out = self.relu5(out)
    out = self.fc6(out)

    return out
</code></pre>
<p>batch_size = 10</p>
<p>n_iterations = 50</p>
<p>no_eps = n_iterations / (13 / batch_size)</p>
<p>no_epochs = int(no_eps)</p>
<p>model = DNN(input_size, hidden1_size, hidden2_size, hidden3_size, hidden4_size, hidden5_size, output_size)</p>
<p>criterion = nn.CrossEntropyLoss()</p>
<p>learning_rate = 0.0001</p>
<p>optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)</p>
<p>iter = 0</p>
<p>for epoch in range(no_epochs):</p>
<pre><code>for i, (X_train, y_train) in enumerate(train_loader):

    optimizer.zero_grad()

    outputs = model(Variable(X_train))

    loss = criterion(outputs, Variable(y_train))

    print('Iter %d --&gt; loss %f' % (i, loss.item()))

    loss.backward()

    optimizer.step()

correct = 0

total = 0

for X_test, y_test in test_loader:

    outputs = model(Variable(X_test))

    pred = outputs.argmax(dim=1, keepdim=True)

    total += y_test.size(0)

    correct += (pred.squeeze() == y_test).sum()  # pred.eq(y_test.view_as(pre  d)).sum().item()

accuracy = 100 * correct / total

print('Iteration: {}.  Accuracy: {}'.format(epoch, accuracy))
</code></pre>
<p>After some iterations, it gives me this error  {IndexError: Target 3988 is out of bounds}<br>
“Hint I am using CPU not GPU”</p>

albanD: <p>Hi,</p>
<p>Can you share the full stack trace showing where the error happens?<br>
If I had to bet, I would say that one of your <code>y_train</code> has an invalid value <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Mohamed_Nabih: <p>Thanks albaD,<br>
I checked it is actually the y_train size it was 3989</p>