AttributeError: 'tuple' object has no attribute 'log_softmax'

 def training(self, epoch):
        train_loss = 0.0
        self.model.train()
        tbar = tqdm(self.trainloader)
        for i, (image, target) in enumerate(tbar):
            self.scheduler(self.optimizer, i, epoch, self.best_pred)
            self.optimizer.zero_grad()
            outputs = self.model(image)
            loss = self.criterion(outputs, target)
            loss.backward()
            self.optimizer.step()
            train_loss += loss.item()
            tbar.set_description('Train loss: %.3f' % (train_loss / (i + 1)))

Was this the mistake before? How to modify it?

I don’t know how your models are implemented, so in case you are stuck debugging it, please post a minimal executable code snippet so that I can take a look.

Hello, I haven’t been in touch with the code for a long time and don’t understand what it means, GitHub - junfu1115/DANet: Dual Attention Network for Scene Segmentation (CVPR2019) This is the code link I reproduced. The reproduced code is:experiments segmentation train.py
Do you have time to help? Thank you very much.

Could you try to minimize the code and try to isolate the failing part only, please?
Based on your description it seems that the loss calculation is wrong, so I would guess that the model definition as well as some random data would be sufficient to reproduce the issue.

Thank you for your reply. I’ll try my best.

Hello!Do you have time to help? My report here is wrong
from option import Options
When I use pip install option, I still report an error

Traceback (most recent call last):
  File "/home/pxg/DAN/DANet05/experiments/segmentation/train.py", line 24, in <module>
    from option import Options
ImportError: cannot import name 'Options'

It seems the options Python package cannot import the Options module. In case you are using a script with the same name, rename it and try to rerun the import.

if __name__ == "__main__":
    args = Options().parse()
    torch.manual_seed(args.seed)
    trainer = Trainer(args)

It seems that options is called here. Similar packages are options and options. Importing these two packages will also report errors.

Thank you for your previous reply. I know where the problem is.

Hi, I am getting an AttributeError: ‘tuple’ object has no attribute ‘loss’. I provided the code snippet where error is, but unable to rectify it.

 for batch in loop:
        # initialize calculated gradients (from prev step)
        optim.zero_grad()
        # pull all tensor batches required for training

        input_ids = batch['input_ids'].to(device)
        attention_mask = batch['attention_mask'].to(device)
        token_type_ids = batch['token_type_ids'].to(device)
        labels = batch['labels'].to(device)
        # process
        outputs = model(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
        # extract loss
        loss = outputs.loss   # Error line
        # calculate loss for every parameter that needs grad update
        loss.backward()

Any suggestion?

Your model’s forward method seems to return a tuple, which you could index to get the entries, while it seems you are expecting to get an object containing a .loss attribute.
Could you check the return statement in forward and see what is returned?