IndexError: Target 1 is out of bounds

username: esta
cooked: <p>Trying to replicate the code below and I get the error “IndexError: Target 1 is out of bounds.” What could be the problem</p>
<p>class TexNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(TexNet, self).<strong>init</strong>()</p>
<pre><code>    self.fc = nn.Linear(256, 1)
    self.feature1 = nn.Sequential(
            STNet(),
            Stim(),
            Text()
            )
    self.inp = 0
    
def forward(self, x):
    x11 = self.feature1(x)
    x1 = x11.view(-1, 256)
    x1 = self.fc(x1) 
    self.inp = x1
    return x1, x11
</code></pre>
<p>model1 = TexNet().to(device)<br>
model2.train()<br>
model3.train()<br>
criterion1 = nn.CrossEntropyLoss()<br>
criterion2 = nn.MSELoss()<br>
criterion3 = nn.CrossEntropyLoss()</p>
<p>train_dataset = torchvision.datasets.ImageFolder(root=’./images/’,<br>
transform=Transform)<br>
train_loader=torch.utils.data.DataLoader(train_dataset, batch_size=1, shuffle=True)</p>
<p>for j in range (10):<br>
model1.train()</p>
<h1></h1>
<pre><code>for i, data in enumerate(train_loader):
    inputs, target = data[0].to(device), data[1].to(device)
    target_f = torch.Tensor.float(target)
</code></pre>
<h1></h1>
<pre><code>    optimizer1.zero_grad()
    optimizer2a.zero_grad()
    optimizer2b.zero_grad()
</code></pre>
<h1></h1>
<pre><code>    output1 = model1(inputs)[0]
output2a = model2(inputs)
    output2b = model3(inputs)[0]

    loss = (criterion1(F.softmax(output1),target) + criterion2(output2a, target_f) + 
            criterion3(F.softmax(output2b), target))</code></pre>

username: KFrank
cooked: <p>Hello Esta!</p>
<aside class="quote no-group quote-modified" data-username="esta" data-post="1" data-topic="96397" data-full="true">
<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/esta/48/33843_2.png" class="avatar"> esta:</div>
<blockquote>
<p>I get the error “IndexError: Target 1 is out of bounds.”</p>
<p>…</p>
<pre><code class="lang-auto">    loss = (criterion1(F.softmax(output1),target) + criterion2(output2a, target_f) + criterion3(F.softmax(output2b), target))
</code></pre>
</blockquote>
</aside>
<p>You don’t say what kind of problem you are trying to solve, you don’t<br>
tell us what <code>model2</code> and model3` are, and you don’t indicate what your<br>
data look like, so we will have to explore …</p>
<p>Print out the shapes of the tensors <code>output1</code>, <code>output2a</code>, <code>output2b</code>,<br>
<code>target</code>, and <code>target_f</code> just before you call your <code>loss</code> line.</p>
<p>(As an aside, if you are using <code>CrossEntropyLoss</code> you will most likely<br>
want to leave out the calls to <code>softmax()</code> as <code>CrossEntropyLoss</code> has,<br>
in effect, <code>softmax()</code> built in.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: esta
cooked: <p>sorry about that. Am new in pytorch. Trying to replicate the code<br>
problem: The code is trying  to extract the fixed length representation from afingerprint image.<br>
Model 1 is extracting texture features while model 2 and 3 are extracting minutiae features. Could be some customiztion of the inception v4 code</p>

username: esta
cooked: <p>code is found on the following github link<br>
</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/anubhav4sachan/fingerprint-recognition/blob/master/training.py" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/anubhav4sachan/fingerprint-recognition/blob/master/training.py" target="_blank" rel="nofollow noopener">anubhav4sachan/fingerprint-recognition/blob/master/training.py</a></h4>
<pre><code class="lang-py">from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import matplotlib.pyplot as plt
import numpy as np

from stn import STNet, stn_train, stn_test
from stem import Stem
from visualise import imshow, save, plot_pores
from texture import Texture
from minutiae import Minutiae1a, Minutiae1b

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class TextureNet(nn.Module):
    def __init__(self):
        super(TextureNet, self).__init__()
</code></pre>

  This file has been truncated. <a href="https://github.com/anubhav4sachan/fingerprint-recognition/blob/master/training.py" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>