IndexError: Target 103 is out of bounds

Niraja: <p>After epoch 1 it is throwing this error. Batch_size is 50.<br>
<strong>Epoch 1, Batch 1 loss: 5.308178</strong><br>
‘’’</p>
<h1>
<a name="building-model-1" class="anchor" href="#building-model-1"></a>Building Model</h1>
<p>import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()</p>
<pre><code>    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, padding=1, stride=1)
    self.relu = nn.ReLU()
    self.maxpool = nn.MaxPool2d(kernel_size = 2)
    
    
    self.conv2 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 3, padding=1, stride=1)
    
    self.conv3 = nn.Conv2d(in_channels = 64, out_channels = 128, kernel_size = 3, padding=1, stride=1)
    
    self.conv4 = nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding=1, stride=1)
    
    #nn.Flatten()
    
    self.fc1 = nn.Linear(in_features = 16384, out_features = 8192)
    
    self.fc2 = nn.Linear(in_features = 8192, out_features = 256) 
    
    self.linear1 = nn.Linear(in_features = 256, out_features = 100) # For age class output
    self.linear2 = nn.Linear(in_features = 256, out_features = 2)   # For gender class output
 
def forward(self, x):
    out = self.conv1(x)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv2(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv3(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv4(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = out.view(x.size(0),-1)
    out = F.sigmoid(self.fc1(out))
    out = F.sigmoid(self.fc2(out))
    label1 = torch.sigmoid(self.linear1(out))                 # Age output
    label2 = torch.sigmoid(self.linear2(out))  # Gender output
    
    return {'label1': label1, 'label2': label2} 
</code></pre>
<p>‘’’<br>
‘’’<br>
import torch.optim as optim</p>
<h1>
<a name="for-multilabel-outputage-2" class="anchor" href="#for-multilabel-outputage-2"></a>For multilabel output:Age</h1>
<p>criterion_multioutput = nn.CrossEntropyLoss()</p>
<h1>
<a name="for-binary-outputgender-3" class="anchor" href="#for-binary-outputgender-3"></a>For binary output:Gender</h1>
<p>criterion_binary = nn.CrossEntropyLoss()</p>
<p>optimizer = optim.SGD(net.parameters(), lr=0.001)<br>
‘’’<br>
‘’’<br>
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):<br>
“”“returns trained model”“”<br>
# initialize tracker for minimum validation loss<br>
valid_loss_min = np.Inf<br>
for epoch in range(1, n_epochs):<br>
train_loss = 0.0<br>
valid_loss = 0.0</p>
<pre><code>    # train the model #
    model.train()
   
    for batch_idx, sample_batched in enumerate(train_dataloader):
        
        image, label1, label2 = sample_batched
        # importing data and moving to CPU
</code></pre>
<h1>
<a name="image-label1-label2-sample_batchedx_traintodevice-4" class="anchor" href="#image-label1-label2-sample_batchedx_traintodevice-4"></a>image, label1, label2 = sample_batched[X_train].to(device),\</h1>
<h1>
<a name="sample_batchedy_age_traintodevice-5" class="anchor" href="#sample_batchedy_age_traintodevice-5"></a>sample_batched[y_age_train].to(device),\</h1>
<h1>
<a name="sample_batchedy_gender_traintodevice-6" class="anchor" href="#sample_batchedy_gender_traintodevice-6"></a>sample_batched[y_gender_train].to(device)</h1>
<h1>
<a name="printtypesample_batched-7" class="anchor" href="#printtypesample_batched-7"></a>print(type(sample_batched))</h1>
<pre><code>        # zero the parameter gradients
        optimizer.zero_grad()
        output = model(image.float())
        output = model(image.float())
        print(output)
        label1_hat = output['label1']
        label2_hat = output['label2']
        #label3_hat=output['label3']         
        # calculate loss
        print(label1.shape)
        loss1 = criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2 = criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))     
        loss = loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
            
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched
</code></pre>
<h1>
<a name="image-label1-label2-sample_batchedx_testtodevice-8" class="anchor" href="#image-label1-label2-sample_batchedx_testtodevice-8"></a>image, label1, label2 = sample_batched[X_test].to(device),\</h1>
<h1>
<a name="sample_batchedy_age_testtodevice-9" class="anchor" href="#sample_batchedy_age_testtodevice-9"></a>sample_batched[y_age_test].to(device),\</h1>
<h1>
<a name="sample_batchedy_gender_testtodevice-10" class="anchor" href="#sample_batchedy_gender_testtodevice-10"></a>sample_batched[y_gender_test].to(device),\</h1>
<h1>
<a name="sample_batchedlabel_racetodevice-11" class="anchor" href="#sample_batchedlabel_racetodevice-11"></a><span class="hashtag">#sample_batched</span>[‘label_race’].to(device)</h1>
<h1>
<a name="printtypeimage-12" class="anchor" href="#printtypeimage-12"></a>print(type(image))</h1>
<pre><code>        output = model(image.float())
        output = model(image.float())
        label1_hat = output['label1']
        label2_hat = output['label2']
        #label3_hat=output['label3']               
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))  
        loss = loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<p>‘’’<br>
‘’’<br>
model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)<br>
‘’’<br>
IndexError                                Traceback (most recent call last)<br>
Input In [56], in &lt;cell line: 1&gt;()<br>
----&gt; 1 model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)</p>
<p>Input In [55], in train_model(model, criterion1, criterion2, optimizer, n_epochs)<br>
29 #label3_hat=output[‘label3’]<br>
30 # calculate loss<br>
31 print(label1.shape)<br>
—&gt; 32 loss1 = criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))<br>
33 loss2 = criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))<br>
34 #loss3=criterion1(label3_hat, label3.squeeze().type(torch.LongTensor))</p>
<p>File ~\anaconda3\lib\site-packages\torch\nn\modules\module.py:1130, in Module._call_impl(self, *input, **kwargs)<br>
1126 # If we don’t have any hooks, we want to skip the rest of the logic in<br>
1127 # this function, and just call forward.<br>
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1129         or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1130     return forward_call(*input, **kwargs)<br>
1131 # Do not call functions when jit is used<br>
1132 full_backward_hooks, non_full_backward_hooks = [], []</p>
<p>File ~\anaconda3\lib\site-packages\torch\nn\modules\loss.py:1164, in CrossEntropyLoss.forward(self, input, target)<br>
1163 def forward(self, input: Tensor, target: Tensor) → Tensor:<br>
 → 1164     return F.cross_entropy(input, target, weight=self.weight,<br>
1165                            ignore_index=self.ignore_index, reduction=self.reduction,<br>
1166                            label_smoothing=self.label_smoothing)</p>
<p>File ~\anaconda3\lib\site-packages\torch\nn\functional.py:3014, in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)<br>
3012 if size_average is not None or reduce is not None:<br>
3013     reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 3014 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)</p>
<p>IndexError: Target 103 is out of bounds.</p>
<p>I tried to solve this error but it could not. Thank you in advance.</p>

srishti-git1110: <p>Hi,<br>
I can see you are using nn.CrossEntropyLoss. Please refer to the <a href="https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html" rel="noopener nofollow ugc">docs</a> and make sure the labels / targets are in the range [0, N-1] where N=no. of classes.</p>
<p>It is difficult to follow your code as it is not properly formatted but as I can see your output layer has 100 and 2 nodes for two different tasks maybe. In that case, for the layer with 100 nodes the target tensor should contain values in the range [0, 99], and in the range [0,1] for the layer with 2 nodes.</p>

Niraja: <p>Yes. this code has two different tasks for age and gender detection. Label1 is for Age output has 100 nodes in output and Label2 is for Gender output has 2 nodes in output. and batch size is 50. I am using UTKFace dataset for this.<br>
When I print Label1 it shows tensor([28, 30,  3, 41, 38, 35, 35,  2, 37, 89, 24, 70, 24, 28, 65, 39,  8, 25,<br>
4, 40, 29, 89, 17, 75, 26,  2, 21, 39, 32, 43,  2, 23, 27, 40,  9, 28,<br>
90, 36, 70, 59, 26, 26, 15, 19, 12, 26, 26, 34, 25, 24]) this type of output.<br>
When I print Label2 it shows tensor([1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0,<br>
1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0,<br>
0, 1]) this type of output.<br>
When I print output it shows {‘label1’: tensor([[0.4544, 0.4799, 0.3660,  …, 0.5097, 0.4451, 0.5353],<br>
[0.4544, 0.4799, 0.3659,  …, 0.5097, 0.4451, 0.5353],<br>
[0.4544, 0.4799, 0.3660,  …, 0.5098, 0.4451, 0.5353],<br>
…,<br>
[0.4544, 0.4799, 0.3660,  …, 0.5098, 0.4451, 0.5353],<br>
[0.4544, 0.4799, 0.3660,  …, 0.5098, 0.4451, 0.5353],<br>
[0.4544, 0.4799, 0.3660,  …, 0.5097, 0.4451, 0.5353]],<br>
grad_fn=), ‘label2’: tensor([[0.5183, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5183, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5183, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5183, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5183, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5183, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4865],<br>
[0.5182, 0.4864],<br>
[0.5182, 0.4865]], grad_fn=)}</p>
<p>As I am new in this area. I have read so many errors like this but I can’t figure out how correct my model and how to solve this. Will you please guide and assist me?</p>

srishti-git1110: <p>Hi, sure.</p>
<p>For age, since you have formulated it as a classification task using cross entropy, the correct answer is one and only one of the 100 “categories”.</p>
<p>Now as can be seen your target tensor (the true labels) contains the correct class’ index corresponding to each point in the batch with dim <code>torch.Size([50])</code> and the output tensor contains class probabilities corresponding to each class for all points in a batch with dimension <code>torch.Size([50, 100])</code>.</p>
<p>Cross entropy loss can be simply used as -</p>
<pre><code class="lang-python">target = torch.empty(50, dtype=torch.long).random_(100)
print(target.shape) # torch.Size([50])
output = torch.randn(50, 100, requires_grad=True)
print(output.shape) # torch.Size([50, 100])
loss_fn = torch.nn.CrossEntropyLoss()
loss = loss_fn(output, target)
print(loss) # tensor(4.9516, grad_fn=&lt;NllLossBackward0&gt;)
</code></pre>
<p>Similar is the case with gender.<br>
Feel free to post further questions.<br>
Hope this helps,<br>
S</p>

Niraja: <p>Thank you. It works.</p>

Niraja: <p>Hi,<br>
My one more question is means i have to convert my target tensor (the true labels) which has dim <code>torch.Size([50])</code> to <code>torch.Size([50, 100])</code> which is output tensor.</p>

srishti-git1110: <p>Since your target tensor the contains the correct class’ index corresponding to each point in the batch, you do not need to do that.<br>
Please see the code in my last post where <code>target</code> has the shape <code>torch.Size([50])</code>.</p>
<aside class="quote no-group" data-username="srishti-git1110" data-post="4" data-topic="170184">
<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/srishti-git1110/48/52515_2.png" class="avatar"> srishti-git1110:</div>
<blockquote>
<pre><code class="lang-auto">target = torch.empty(50, dtype=torch.long).random_(100)
print(target.shape) # torch.Size([50])
</code></pre>
</blockquote>
</aside>

Niraja: <p>I got the code you gave me. But I don’t know where to change in my program according to that code which will give the answer. I can’t figure out how correct my model and how to solve this. Will you please guide and assist me?</p>

srishti-git1110: <p>Sure.<br>
Please try to post a minimum executable snippet that reproduces the error you are facing.</p>
<p>Also, please make sure to properly format the code. Enclose it within three backticks ```.</p>

Niraja: <p>okay.<br>
This is my data<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/7/f/7f298c8df38e4addcc20ad324bef77ed40f8c040.png" alt="image" data-base62-sha1="i8VEK6SendYzsjhof0mHPMH92Du" width="543" height="230"></p>
<p>Convert it to dataframe<br>
‘’’<br>
df = pd.DataFrame()<br>
df[‘image’], df[‘age’], df[‘gender’] = image_paths, age_labels, gender_labels<br>
X = extract_features(df[‘image’])<br>
X.shape    &gt;&gt;(10497, 1, 128, 128)<br>
X = X/255.0</p>
<p>‘’’<br>
‘’’<br>
X_train, X_test, y_age_train, y_age_test, y_gender_train, y_gender_test = train_test_split(X, label_age, label_gender, test_size=0.2)<br>
train = torch.utils.data.TensorDataset(X_train_tensor, y_age_train_tensor, y_gender_train_tensor)<br>
test = torch.utils.data.TensorDataset(X_test_tensor, y_age_test_tensor, y_gender_test_tensor)<br>
batch_size = 50<br>
train_dataloader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=True, num_workers=4)<br>
test_dataloader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=True, num_workers=4)<br>
‘’’<br>
‘’’<br>
Building Model</p>
<p>import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()</p>
<pre><code>    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, padding=1, stride=1)
    self.relu = nn.ReLU()
    self.maxpool = nn.MaxPool2d(kernel_size = 2)
    
    
    self.conv2 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 3, padding=1, stride=1)
    
    self.conv3 = nn.Conv2d(in_channels = 64, out_channels = 128, kernel_size = 3, padding=1, stride=1)
    
    self.conv4 = nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding=1, stride=1)
    
    #nn.Flatten()
    
    self.fc1 = nn.Linear(in_features = 16384, out_features = 8192)
    
    self.fc2 = nn.Linear(in_features = 8192, out_features = 512) 
    
    self.linear1 = nn.Linear(in_features = 512, out_features = 100) # For age class output
    self.linear2 = nn.Linear(in_features = 512, out_features = 2)   # For gender class output
 
def forward(self, x):
    out = self.conv1(x)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv2(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv3(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv4(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = out.view(x.size(0),-1)
    out = F.sigmoid(self.fc1(out))
    out = F.sigmoid(self.fc2(out))
    label1 = self.linear1(out)                 # Age output
    label2 = torch.sigmoid(self.linear2(out))  # Gender output
    
    return {'label1': label1, 'label2': label2}      
</code></pre>
<p>criterion_multioutput = nn.CrossEntropyLoss()<br>
criterion_binary = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=0.001)<br>
‘’’<br>
‘’’</p>
<p>def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):</p>
<pre><code># initialize tracker for minimum validation loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
    train_loss = 0.0
    valid_loss = 0.0
    
    # train the model 
    model.train()
   
    for batch_idx, sample_batched in enumerate(train_dataloader):
        
        image, label1, label2 = sample_batched

                                             
        # zero the parameter gradients
        optimizer.zero_grad()
        output = model(image.float())
        
        print("output label1 shape: ",output['label1'].ndim)
        #print("output label2 shape: ",output['label2'].shape)
        #print(type(output['label1']))
        label1_hat = output['label1']
        label2_hat = output['label2']
        
        
        #print("label1 shape: ",label1.shape)
        #print("label2 shape: ",label2.shape)
        #print("Label1: ", label1)
        age_label1 = label1
        print(label1.ndim)
        gender_label2 = label2
        print(gender_label2)
        
        # calculate loss
        loss1 = criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2 = criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
           
        loss = loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
            
            
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched

        output = model(image.float())
        
        label1_hat = output['label1']
        label2_hat = output['label2']
                 
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.LongTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.LongTensor))
        
          
        loss = loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<p>model_conv = train_model(net, criterion_multioutput, criterion_binary, optimizer)<br>
‘’’</p>

Niraja: <p>Hi,<br>
I make some changes in training the model<br>
‘’’<br>
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):</p>
<pre><code># initialize tracker for minimum validation loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
    train_loss = 0.0
    valid_loss = 0.0
    
    # train the model #
    model.train()
    
    
   

    for batch_idx, sample_batched in enumerate(train_dataloader):
        
        image, label1, label2 = sample_batched
        
        target = torch.empty(50, dtype=torch.long).random_(100) # as per code you given
        label1 = torch.randn(50, 100, requires_grad=True)           # as per code you given
    
        target = torch.empty(50, dtype=torch.long).random_(2)   # as per code you given
        label2 = torch.randn(50, 2, requires_grad=True)              # as per code you given

                                             
        # zero the parameter gradients
        optimizer.zero_grad()
        output = model(image.float())
        
        #print("output label1 shape: ",output['label1'].shape)    #####
        #print("output label2 shape: ",output['label2'].shape)   ######
        #print(type(output['label1']))                            ########
        #print(output['label2'])                                  ########
        
        label1_hat = output['label1']
        label2_hat = output['label2']
        
        
        
        #print("label1 shape: ",label1.shape)                   ##########
        #print("label2 shape: ",label2.shape)                   ###########
        #print("Label1: ", label1)                              #############
        #age_label1 = label1                                     ##########
        #print(label1.shape)                                     ########
        #print("label2: ", label2)                               ############
        #gender_label2 = label2                                  ############
        #print(gender_label2)                                   ###############
        
        # calculate loss
        loss1 = criterion1(label1_hat, label1.squeeze().type(torch.FloatTensor))
        loss2 = criterion2(label2_hat, label2.squeeze().type(torch.FloatTensor))
           
        loss = loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
            
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched

        output = model(image.float())
        
        label1_hat = output['label1']
        label2_hat = output['label2']
                 
        # calculate loss
        loss1=criterion1(label1_hat, label1.squeeze().type(torch.FloatTensor))
        loss2=criterion2(label2_hat, label2.squeeze().type(torch.FloatTensor))
        
          
        loss = loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<p>‘’’<br>
Is this correct.</p>

srishti-git1110: <p>Thanks for the code. Could you please specify the exact error you are now facing with this code?<br>
As for the changes you’ve made in your last post, those do not make sense as you’ll be using labels from your own data and not some random labels.</p>
<p>My code was an example to explain the API.</p>

Niraja: <p>If we consider last to last post. Then it shows IndexError: Target 110 is out of bounds. And this error shows after 1st epoch. .<br>
But how to make changes as per example of your code.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/8/282d5a7316520e4f3588c6fe48c7728642a42421.png" data-download-href="https://discuss.pytorch.org/uploads/default/282d5a7316520e4f3588c6fe48c7728642a42421" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/282d5a7316520e4f3588c6fe48c7728642a42421_2_690x417.png" alt="image" data-base62-sha1="5JqgBDk4yywyf02joNOw4rd95g5" width="690" height="417" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/282d5a7316520e4f3588c6fe48c7728642a42421_2_690x417.png, https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/282d5a7316520e4f3588c6fe48c7728642a42421_2_1035x625.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/8/282d5a7316520e4f3588c6fe48c7728642a42421_2_1380x834.png 2x" data-dominant-color="ECECE0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1407×851 125 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Niraja: <p>Hello mam,<br>
After the 1st epoch, it throws this error: “IndexError: Target 110 is out of bounds.”<br>
I am using multitask output - 100 classes are for age prediction output i.e. label1 and 2 classes are for gender prediction output i.e. label2. My batch size is 50.<br>
I trying my best to solve this error. But, I still did not get what changes needs to be made.<br>
Can you please explain how to fix the issue?<br>
‘’’<br>
train = torch.utils.data.TensorDataset(X_train_tensor, y_age_train_tensor, y_gender_train_tensor)<br>
test = torch.utils.data.TensorDataset(X_test_tensor, y_age_test_tensor, y_gender_test_tensor)<br>
batch_size = 50<br>
train_dataloader = torch.utils.data.DataLoader(train, batch_size=batch_size, shuffle=True, num_workers=4)<br>
test_dataloader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=True, num_workers=4)<br>
import torch.optim as optim<br>
criterion_multioutput = nn.CrossEntropyLoss()    # For multilabel output:Age<br>
criterion_binary = nn.CrossEntropyLoss()        # For binary output:Gender<br>
optimizer = optim.Adam(net.parameters(), lr=0.001)<br>
‘’’<br>
‘’’</p>
<h1>
<a name="building-model-1" class="anchor" href="#building-model-1"></a>Building Model</h1>
<p>import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()</p>
<pre><code>    self.conv1 = nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, padding=1, stride=1)
    self.relu = nn.ReLU()
    self.maxpool = nn.MaxPool2d(kernel_size = 2)
    
    
    self.conv2 = nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 3, padding=1, stride=1)
    
    self.conv3 = nn.Conv2d(in_channels = 64, out_channels = 128, kernel_size = 3, padding=1, stride=1)
    
    self.conv4 = nn.Conv2d(in_channels = 128, out_channels = 256, kernel_size = 3, padding=1, stride=1)
    
    #nn.Flatten()
    
    self.fc1 = nn.Linear(in_features = 256*8*8, out_features = 8192)
    
    self.fc2 = nn.Linear(in_features = 8192, out_features = 512) 
    
    self.linear1 = nn.Linear(in_features = 512, out_features = 100) # For age class output
    self.linear2 = nn.Linear(in_features = 512, out_features = 2)   # For gender class output
 
def forward(self, x):
    out = self.conv1(x)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv2(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv3(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    out = self.conv4(out)
    out = self.relu(out)
    out = self.maxpool(out)
    
    #print(out.shape)
    out = out.view(x.size(0),-1)
    out = F.sigmoid(self.fc1(out))
    out = F.sigmoid(self.fc2(out))
    label1 = self.linear1(out)                 # Age output
    label2 = torch.sigmoid(self.linear2(out))  # Gender output
    
    return {'label1': label1, 'label2': label2}      
</code></pre>
<p>‘’’<br>
‘’’<br>
import torch.optim as optim<br>
criterion_multioutput = nn.CrossEntropyLoss()    # For multilabel output:Age<br>
criterion_binary = nn.CrossEntropyLoss()        # For binary output:Gender<br>
optimizer = optim.Adam(net.parameters(), lr=0.001)<br>
‘’’<br>
‘’’<br>
def train_model(model, criterion1, criterion2, optimizer, n_epochs=25):</p>
<pre><code># initialize tracker for minimum validation loss
valid_loss_min = np.Inf
for epoch in range(1, n_epochs):
    train_loss = 0.0
    valid_loss = 0.0
    
    # train the model #
    model.train()    

    for batch_idx, sample_batched in enumerate(train_dataloader):
        
        image, label1, label2 = sample_batched

        # zero the parameter gradients
        optimizer.zero_grad()
        output = model(image.float())
        
        print("output label1 shape: ",output['label1'].shape) ##&gt;output label1 :  torch.Size([50, 100])
        print("output label2 shape: ",output['label2'].shape) ##&gt;output label2 :  torch.Size([50, 2])
        #print(type(output['label1']))                            ########
        #print(output['label2'])                                  ########
        
        label1_hat = output['label1']
        label2_hat = output['label2']
        
        print("label1 shape: ",label1.shape)      ########## &gt; label1 shape:  torch.Size([50])
        print("label2 shape: ",label2.shape)      ########### &gt; label2 shape:  torch.Size([50])
        #print("Label1: ", label1)                        #############
        #print("label2: ", label2)                          ############
        max_label1 = torch.max(label1)          #############
        print("max_label1: ", max_label1)       ##########
        min_label1 = torch.min(label1)          ###########
        print("min_label1: ", min_label1)       ###############

        # calculate loss
        loss1 = criterion1(label1_hat, label1)
        loss2 = criterion2(label2_hat, label2)
           
        loss = loss1+loss2
        # back prop
        loss.backward()
        # grad
        optimizer.step()
        train_loss = train_loss + ((1 / (batch_idx + 1)) * (loss.data - train_loss))
        if batch_idx % 50 == 0:
            print('Epoch %d, Batch %d loss: %.6f' %
              (epoch, batch_idx + 1, train_loss))
            
    # validate the model #
    model.eval()

    for batch_idx, sample_batched in enumerate(test_dataloader):
        image, label1, label2 = sample_batched

        output = model(image.float())
        
        label1_hat = output['label1']
        label2_hat = output['label2']
                 
        # calculate loss
        loss1=criterion1(label1_hat, label1)
        loss2=criterion2(label2_hat, label2)
      
        loss = loss1+loss2
        valid_loss = valid_loss + ((1 / (batch_idx + 1)) * (loss.data - valid_loss))
    
    # print training/validation statistics 
    print('Epoch: {} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(
        epoch, train_loss, valid_loss))
    
    ## TODO: save the model if validation loss has decreased
    if valid_loss &lt; valid_loss_min:
        torch.save(model, 'model.pt')
        print('Validation loss decreased ({:.6f} --&gt; {:.6f}).  Saving model ...'.format(
        valid_loss_min,
        valid_loss))
        valid_loss_min = valid_loss
# return trained model
return model
</code></pre>
<p>‘’’</p>

srishti-git1110: <aside class="quote no-group" data-username="srishti-git1110" data-post="2" data-topic="170184">
<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/srishti-git1110/48/52515_2.png" class="avatar"> srishti-git1110:</div>
<blockquote>
<p>the target tensor should contain values in the range [0, 99],</p>
</blockquote>
</aside>
<p>Hi Niraja, as I’d mentioned earlier as well, your target tensor needs to have the values in [0, 99] only.</p>
<p>Please try and inspect the age column of your dataframe and see what data it contains. Can you post what the following code outputs?</p>
<pre><code class="lang-auto">print(df_name['age'].value_counts())
</code></pre>

Niraja: <p>print(df[‘age’].value_counts())</p>
<blockquote>
<blockquote></blockquote>
</blockquote>
<p>26  ----     810<br>
35   ----     500<br>
28   ----     490<br>
32  ----      410<br>
30  ----      391<br>
…<br>
91   ----      2<br>
101  ----     2<br>
116  ----     2<br>
111  ----     1<br>
103  ----    1<br>
Name: age, Length: 103, dtype: int64</p>

srishti-git1110: <p>Now as you can see there are values greater than 99 in this column.<br>
Figure out what all values this column contains and define your linear layer accordingly.</p>

Niraja: <p>According to this [0, nb_classes-1] I changed here age out_features to 102,  because length is 103 but still it is showing error -<br>
‘’’ self.fc1 = nn.Linear(in_features = 256<em>8</em>8, out_features = 8192)</p>
<pre><code>    self.fc2 = nn.Linear(in_features = 8192, out_features = 512) 
    
    self.linear1 = nn.Linear(in_features = 512, out_features = 102) # For age class output
    self.linear2 = nn.Linear(in_features = 512, out_features = 2)   # For gender class output
</code></pre>
<p>After this I have changed<br>
‘’’<br>
self.linear1 = nn.Linear(in_features = 512, out_features = 116) # For age class output<br>
‘’’<br>
‘’’<br>
because for age .unique() it shows following.<br>
‘’’<br>
for column in df:<br>
print(f’{column} : {df[column].unique()}‘)<br>
‘’’<br>
it shows &gt;<br>
age : [100 101 103 105  10 110 111 115 116  11  12  13  14  15  16  17  18  19<br>
1  20  21  22  23  24  25  26  27  28  29   2  30  31  32  33  34  35<br>
36  37  38  39   3  40  41  42  43  44  45  46  47  48  49   4  50  51<br>
52  53  54  55  56  57  58  59   5  60  61  62  63  64  65  66  67  68<br>
69   6  70  71  72  73  74  75  76  78  79   7  80  81  82  83  84  85<br>
86  87  88  89   8  90  91  92  93  95  96  99   9]<br>
but still after<br>
Epoch 1, Batch 1 loss: 5.499494<br>
Epoch 1, Batch 51 loss: 4.840973<br>
Epoch 1, Batch 101 loss: 4.809159<br>
this it throws IndexError: Target 116 is out of bounds. this error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/a/dad635bb30fb7454274cf781aa987005ca0d0eb1.png" data-download-href="https://discuss.pytorch.org/uploads/default/dad635bb30fb7454274cf781aa987005ca0d0eb1" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/dad635bb30fb7454274cf781aa987005ca0d0eb1_2_690x415.png" alt="image" data-base62-sha1="vdV1VE31SvCi2sssCSOHGLmWYcF" width="690" height="415" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/dad635bb30fb7454274cf781aa987005ca0d0eb1_2_690x415.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/dad635bb30fb7454274cf781aa987005ca0d0eb1_2_1035x622.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/a/dad635bb30fb7454274cf781aa987005ca0d0eb1_2_1380x830.png 2x" data-dominant-color="EEEFE5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1412×851 124 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I trying my best to solve this error. Can you please guide how to fix the issue?</p>

srishti-git1110: <aside class="quote no-group" data-username="Niraja" data-post="18" data-topic="170184">
<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/niraja/48/61924_2.png" class="avatar"> Niraja:</div>
<blockquote>
<p>[100 101 103 105 10 110 111 115 116 11 12 13 14 15 16 17 18 19<br>
1 20 21 22 23 24 25 26 27 28 29 2 30 31 32 33 34 35<br>
36 37 38 39 3 40 41 42 43 44 45 46 47 48 49 4 50 51<br>
52 53 54 55 56 57 58 59 5 60 61 62 63 64 65 66 67 68<br>
69 6 70 71 72 73 74 75 76 78 79 7 80 81 82 83 84 85<br>
86 87 88 89 8 90 91 92 93 95 96 99 9]</p>
</blockquote>
</aside>
<p>According to this, you have a total of 116 categories, so the output layer should have 116 nodes.<br>
Even then, this column cannot be used directly and you need to subtract 1 from it to be able to use CrossEntropyLoss.</p>
<pre><code class="lang-auto">df['age'] = df['age'] - 1
</code></pre>
<p>Changes this -</p>
<aside class="quote no-group" data-username="Niraja" data-post="14" data-topic="170184">
<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/niraja/48/61924_2.png" class="avatar"> Niraja:</div>
<blockquote>
<p><code>self.linear1 = nn.Linear(in_features = 512, out_features = 100) # For age class output</code></p>
</blockquote>
</aside>
<p>to<br>
<code>self.linear1 = nn.Linear(in_features = 512, out_features = 116)</code></p>

Niraja: <p>Hello mam,<br>
Thank you for your guidance. My model has been worked properly.<br>
One more question is - How to improve skills in Machine Learning and Deep Learning?</p>