IndexError: Target 17 is out of bounds

nikita_s: <p>I have the following error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-54-276f5444b449&gt; in &lt;module&gt;
----&gt; 1 train_epocs(model, optimizer, train_dl, valid_dl, epochs=15)

&lt;ipython-input-51-05a28b50ed95&gt; in train_epocs(model, optimizer, train_dl, val_dl, epochs, C)
     11             y_bb = y_bb.long()
     12             out_class, out_bb = model(x)
---&gt; 13             loss_class = F.cross_entropy(out_class, y_class, reduction="sum")
     14             loss_bb = F.l1_loss(out_bb, y_bb, reduction="none").sum(1)
     15             loss_bb = loss_bb.sum()

~\anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2822     if size_average is not None or reduce is not None:
   2823         reduction = _Reduction.legacy_get_string(size_average, reduce)
-&gt; 2824     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
   2825 
   2826 

IndexError: Target 17 is out of bounds.
</code></pre>
<p>Here is the code:</p>
<pre><code class="lang-auto">class BB_model(nn.Module):
    def __init__(self):
        super(BB_model, self).__init__()
        resnet = models.resnet34(pretrained=True)
        layers = list(resnet.children())[:8]
        self.features1 = nn.Sequential(*layers[:6])
        self.features2 = nn.Sequential(*layers[6:])
        self.classifier = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))
        self.bb = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))
        
    def forward(self, x):
        x = self.features1(x)
        x = self.features2(x)
        x = F.relu(x)
        x = nn.AdaptiveAvgPool2d((1,1))(x)
        x = x.view(x.shape[0], -1)
        return self.classifier(x), self.bb(x)
</code></pre>
<pre><code class="lang-auto">def update_optimizer(optimizer, lr):
    for i, param_group in enumerate(optimizer.param_groups):
        param_group["lr"] = lr
</code></pre>
<pre><code class="lang-auto">def train_epocs(model, optimizer, train_dl, val_dl, epochs=10,C=1000):
    idx = 0
    for i in range(epochs):
        model.train()
        total = 0
        sum_loss = 0
        for x, y_class, y_bb in train_dl:
            batch = y_class.shape[0]
            x = x.float()
            y_class = y_class.long().squeeze()
            y_bb = y_bb.long()
            out_class, out_bb = model(x)
            loss_class = F.cross_entropy(out_class, y_class, reduction="sum")
            loss_bb = F.l1_loss(out_bb, y_bb, reduction="none").sum(1)
            loss_bb = loss_bb.sum()
            loss = loss_class + loss_bb/C
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            idx += 1
            total += batch
            sum_loss += loss.item()
        train_loss = sum_loss/total
        val_loss, val_acc = val_metrics(model, valid_dl, C)
        print("train_loss %.3f val_loss %.3f val_acc %.3f" % (train_loss, val_loss, val_acc))
    return sum_loss/total
</code></pre>
<pre><code class="lang-auto">def val_metrics(model, valid_dl, C=1000):
    model.eval()
    total = 0
    sum_loss = 0
    correct = 0 
    for x, y_class, y_bb in valid_dl:
        batch = y_class.shape[0]
        x = x.float()
        y_class = y_class.long()
        y_bb = y_bb.float().squeeze()
        out_class, out_bb = model(x)
        loss_class = F.cross_entropy(out_class, y_class, reduction="sum")
        loss_bb = F.l1_loss(out_bb, y_bb, reduction="none").sum(1)
        loss_bb = loss_bb.sum()
        loss = loss_class + loss_bb/C
        _, pred = torch.max(out_class, 1)
        correct += pred.eq(y_class).sum().item()
        sum_loss += loss.item()
        total += batch
    return sum_loss/total, correct/total
</code></pre>
<pre><code class="lang-auto">model = BB_model()
parameters = filter(lambda p: p.requires_grad, model.parameters())
optimizer = torch.optim.Adam(parameters, lr=0.006)
</code></pre>
<p><code>train_epocs(model, optimizer, train_dl, valid_dl, epochs=15)</code></p>

tom: <p>This is to be taken literally. You model has 4 output classes and your labels has 17 (which would only work if you have &gt;= 18 output classes).</p>

nikita_s: <p>Cuz I have more classes like 27</p>

nikita_s: <p><a class="mention" href="/u/tom">@tom</a> I am new to this how to please fix it? I have 27 classes</p>

tom: <aside class="quote no-group" data-username="nikita_s" data-post="1" data-topic="131227">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikita_s/48/32791_2.png" class="avatar"> nikita_s:</div>
<blockquote>
<pre><code class="lang-auto">        self.classifier = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))
        self.bb = nn.Sequential(nn.BatchNorm1d(512), nn.Linear(512, 4))
</code></pre>
</blockquote>
</aside>
<p>The 4 at the end in classifier makes it 4 classes. If you want, there is some discussion about classifiers in our book that might be worthwhile and accessible (they are reading it currently over at <a href="https://community.wandb.ai/c/community-events/pytorch-book/32" class="inline-onebox" rel="noopener nofollow ugc">PyTorch Book Reading Group - W&amp;B Community</a>).</p>
<p>I don’t know what the bb wants.</p>