Error in cross entropy loss in last data loader iteration

 for e in range(args.epochs):
    models['backbone'].train()
    models['module'].train()
    train_loss = 0.0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        inputs, targets = inputs.to(device), targets.to(device)
        outputs, features = models['backbone'](inputs)
        print(outputs.shape)
        print(targets.shape)
        target_loss = criterion(outputs, targets)

        if args.epochs > args.epochs_loss:
            # After args.epochs_loss, stop the gradient from the loss prediction module propagated to the target model.
            features[0] = features[0].detach()
            features[1] = features[1].detach()
            features[2] = features[2].detach()
            features[3] = features[3].detach()
            features[4] = features[4].detach()

At the end of the last iteration of the data loader, specifically at the loss = criterion(outputs, targets) line, I received the following error “AttributeError: ‘tuple’ object has no attribute ‘log_softmax’”. Is there any way to solve this problem?

Hi @bryan123, since you do not provide the definition of criterion, we cannot help you any further. However python is given you a hint: criterion returns a tuple, not a tensor. The softmax function can be applied only to tensors. Please check your criterion’s definition and the return statement

Hi @lfsalasg, I have just resolved the problem. In the validation loader iterations, I only returned outputs (there should have been outputs and features) which caused the error.