Why my train and valid Loss keep growing as the epochs go on?

username: Sam_Fst
cooked: <p>With each epochs my train loss increases and I don’t know where the error in the training code is, does anyone have any ideas?</p>
<p>for e in range(epochs):<br>
# keep track of training and validation loss<br>
train_loss = 0.0<br>
valid_loss = 0.0<br>
running_loss = 0.0<br>
running_corrects = 0.0</p>
<pre><code>###################
# TRAIN THE MODEL #
###################
model.train()
cont = 0
for inputs, label in (dataloaders['train']):
    # IF GPU is availible
    if train_on_gpu:
        inputs, label = inputs.cuda(), label.cuda()
    
    #inputs, labels = i ,data
    
    
    optimizer.zero_grad()

    with torch.set_grad_enabled(True):
        logps = model(inputs)
        _, preds = torch.max(logps, 1) # tecnica nova de validacao
        loss = criterion(logps, label)
        loss.backward()
        optimizer.step()
    #*inputs.size(0)
    running_loss += loss.item()
    print("running_loss = %f , interaction = %i " % (running_loss,cont) )
    running_corrects += torch.sum(preds == label.data)
    RL_vector.append(running_loss)
    cont += 1
    
    
###################
# VALID THE MODEL #
###################
model.eval()
for inputs, label in (dataloaders['valid']):
    # IF GPU is availible
    if train_on_gpu:
        inputs, label = inputs.cuda(), label.cuda()
    
    with torch.no_grad():
        logps = model(inputs)
        _, preds = torch.max(logps, 1) # tecnica nova de validacao
        loss = criterion(logps, label)
        # update average validation loss 
    valid_loss += loss.item()
    VL_vector.append(valid_loss)

# calculate average losses

epoch_loss_train = running_loss / dataset_sizes['train']
epoch_acc_train = running_corrects.double() / dataset_sizes['train']

epoch_loss_valid = valid_loss / dataset_sizes['valid']


print('{} Loss: {:.4f} \tAcc: {:.4f}'.format('train', epoch_loss_train, epoch_acc_train))
print('{} \tLoss: {:.4f} '.format('valid', epoch_loss_valid))
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/7d98a165777eb92e28d4ef3601f9bd30945648f5.png" alt="Capturar" data-base62-sha1="hV4GY4rVm26dPQoeipAmwFH4Xrf" width="333" height="82"></p>

username: ptrblck
cooked: <p>Both losses are decreasing, which is generally fine.<br>
Which criterion are you using and what kind of use case are you currently working on, as the range looks a bit different.</p>

username: Sam_Fst
cooked: <p>I fix the problem. I put model.fc = classifier in my models.densenet121 and I thinks this is the source of the erro. But I do not know why , what is the difference between model.fc and model.classifier ?</p>

username: ptrblck
cooked: <p><code>model.fc</code> and <code>model.classifier</code> are just the internal names for some submodules.<br>
If you print the model, you’ll find the name of the last layer, which you could replace with a custom one for your use case:</p>
<pre><code class="lang-python">model = models.densenet121()
print(model)
</code></pre>
<p>In this example, the <code>densenet121</code> uses the attribute name <code>classifier</code> for the last <code>nn.Linear</code> layer, so you should use this attribute name.<br>
If you just assign a custom linear layer to <code>model.fc</code> it won’t be used and trained without changing the <code>forward</code> method.</p>