[Solved]Error when use 2 datasets to do transfer learning

username: Komoriii_Sen
cooked: <p>Hi there. I trained a VGG network using CIFAR10 dataset first for transfer learning.<br>
After I save the model of CIFAR 10 VGG network.<br>
I substituted the classifier due to another dataset has different class number.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/f/f9e993c1492c44ea06c5c5f69f58673b33d7ab93.png" alt="image" data-base62-sha1="zEPkgFkozqGAscTvp0WHoY3rqV5" width="409" height="150"><br>
Then I start to train the model with the new dataset.</p>
<pre><code class="lang-auto">def train(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        inputs, targets = inputs.to(device), targets.to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        optimizer.step()

        train_loss += loss.item()
        _, predicted = outputs.max(1)
        total += targets.size(0)
        correct += predicted.eq(targets).sum().item()

        progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
            % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))

train(net)
</code></pre>
<p>However, there is an error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/6fd9ab5f86d228741712eeae1f155f8a3c737dcb.png" data-download-href="https://discuss.pytorch.org/uploads/default/6fd9ab5f86d228741712eeae1f155f8a3c737dcb" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/6/6fd9ab5f86d228741712eeae1f155f8a3c737dcb.png" alt="image" data-base62-sha1="fXtlW41UOQaUWw9RL0tYSz0LYlt" width="690" height="253" data-dominant-color="161616"><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">707×260 9.53 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><br>
I cannot get the point from the error message. Can you help me fix that? Many thanks.</p>

username: ptrblck
cooked: <p>Could you run the code on the CPU and check for error messages as they might be clearer?</p>

username: Komoriii_Sen
cooked: <p>I appreciate your help. Trying to run the code on CPU, I got a clear message.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/70591712440749206520ecb5a71389b2f64f71fe.png" data-download-href="https://discuss.pytorch.org/uploads/default/70591712440749206520ecb5a71389b2f64f71fe" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/70591712440749206520ecb5a71389b2f64f71fe.png" alt="image" data-base62-sha1="g1SlMLPyhonWtrzzSDYxEkdIVDU" width="690" height="164" data-dominant-color="181818"><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">951×227 11.1 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><br>
The problem must be relevant to the changed class number between 2 datasets.</p>

username: ptrblck
cooked: <p>Yes, exactly. Check your targets to have values in the range <code>[0, nb_classes-1]</code>.</p>