Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IndexError: Pytorch-lightning CompositionalMetric require tensor.item() if dim=0 whether I did so #19777

Open
YuyaWake opened this issue Apr 15, 2024 · 0 comments
Labels
bug Something isn't working needs triage Waiting to be triaged by maintainers ver: 2.1.x

Comments

@YuyaWake
Copy link

Bug description

I am encounting the error IndexError: invalid index of a 0-dim tensor. Use tensor.item() in Python or tensor.item<T>() in C++ to convert a 0-dim tensor to a number Output is truncated.

What version are you seeing the problem on?

v2.1

How to reproduce the bug

Dataset and Model is here:


##### Dataset #####
class WrimeDataset(Dataset):
    def __init__(self, dataframe, tokenizer, max_length):
        self.tokenizer = tokenizer
        self.data = dataframe
        self.max_length = max_length

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        text = self.data.iloc[idx]['input_text']
        row = self.data.iloc[idx]
        labels = row['label'] 

        # 入力テキストの前処理
        inputs = self.tokenizer(
            "感情分析:" + text,
            max_length=self.max_length,
            padding='max_length',
            truncation=True,
            return_tensors="pt"
        )

        input_ids = inputs['input_ids'].squeeze().to(dtype=torch.long)
        attention_mask = inputs['attention_mask'].squeeze().to(dtype=torch.long)

        labels = torch.tensor(labels, dtype=torch.long)
        # shape: torch.Size([8])
        
        padding_sign = torch.full((self.max_length,), -100, dtype=torch.long)
        actual_length = min(len(inputs), self.max_length)
        padding_sign[:actual_length] = input_ids[:actual_length]
        print(f'input_ids shape: {input_ids.shape}')
        print(f'attention_mask shape: {attention_mask.shape}')
        print(f'labels shape: {labels.shape}')
        return input_ids, attention_mask, labels


##### model #####
class T5Model(pl.LightningModule):
    def __init__(self, num_classes):
        super(T5Model, self).__init__()
        model_name = "sonoisa/t5-base-japanese"
        config = T5Config.from_pretrained(model_name, output_hidden_states=True)
        self.t5_model = T5ForConditionalGeneration.from_pretrained(model_name, config=config)
        self.tokenizer = T5Tokenizer.from_pretrained(model_name)
        self.acc = Accuracy(task="multiclass", num_classes=num_classes)
        self.f1 = F1Score(task="multiclass", num_classes=num_classes)
        self.qwk = CohenKappa(task="multiclass", num_classes=num_classes, weights='quadratic')
        self.class_acc = Accuracy(task='binary')
        self.class_f1 = F1Score(task='binary')
        self.classifier = nn.Linear(config.d_model, num_classes)
        self.sigmoid = nn.Sigmoid()
        self.num_classes = num_classes

        for param in self.t5_model.parameters():
            param.requires_grad = True

        for param in self.classifier.parameters():
            param.requires_grad = True
        

    def forward(self, input_ids, attention_mask=None, labels=None):
        labels = torch.tensor(labels, dtype=torch.long)
        output = self.t5_model(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
        loss = output.loss
        print(f'loss shape: {loss.shape}')
        decoder_hidden_states = output.decoder_hidden_states
        last_decoder_state = decoder_hidden_states[-1][:, -1, :]
        print(f'last_hidden_state: {last_decoder_state.shape}')
        logits = self.classifier(last_decoder_state)
        print(f'logits shape: {logits.shape}')
        preds = self.sigmoid(logits)
        print(f'preds shape: {preds.shape}')
        return loss, preds, logits
    
    def training_step(self, batch, batch_idx):
        input_ids, attention_mask, labels = batch
        print('==training==')
        print(f'input_ids shape: {input_ids.shape}')
        print(f'attention_mask shape: {attention_mask.shape}')
        print(f'labels shape: {labels.shape}')
        loss, preds, logits = self.forward(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
        self.log('train_loss', loss.item())
        return loss.item()
    
    def validation_step(self, batch, batch_idx):
        input_ids, attention_mask, labels = batch
        print('==validation==')
        print(f'input_ids shape: {input_ids.shape}')
        print(f'attention_mask shape: {attention_mask.shape}')
        print(f'labels shape: {labels.shape}')
        loss, preds, logits = self.forward(input_ids=input_ids, attention_mask=attention_mask, labels=labels)
        
        labels = labels.bool()
        self.acc.update(preds, labels)
        self.f1.update(preds, labels)
        self.qwk.update(preds, labels)
        
        self.log('val_loss', loss.item())

        return {'loss': loss.item(), 'preds': preds, 'labels': labels}
        
    def on_validation_epoch_end(self):
        # エポックの終わりにメトリクスの平均を計算し、ログに記録
        self.log('val_acc', self.acc.compute(), on_epoch=True, prog_bar=True)
        self.log('val_f1', self.f1.compute(), on_epoch=True, prog_bar=True)
        self.log('val_qwk', self.qwk.compute(), on_epoch=True, prog_bar=True)

        for i in range(self.num_classes):
            self.log(f'val_class_{i}_acc', self.class_acc[i].compute(), on_epoch=True, prog_bar=True)
            self.log(f'val_class_{i}_f1', self.class_f1[i].compute(), on_epoch=True, prog_bar=True)
            self.class_acc[i].reset()
            self.class_f1[i].reset()
            
        # メトリクスをリセット
        self.acc.reset()
        self.f1.reset()
        self.qwk.reset()

        

    def configure_optimizers(self):
        optimizer = Adafactor(
            self.parameters(),
            scale_parameter=True,
            relative_step=True,
            warmup_init=True,
            lr=None)  # Adafactorはlrを自動的に調整する
        return optimizer


### Error messages and logs

All error is here:
'''
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[84], line 31
     28 trainer = make_trainer(max_epochs=max_epochs, name='test', patience=patience)
     30 try:
---> 31     trainer.fit(model, train_dataloader, val_dataloader)
     32 except KeyError as e:
     33     print(f"KeyError: {e}")

File c:\Users\foo\anaconda3\envs\p100_venv\lib\site-packages\pytorch_lightning\trainer\trainer.py:544, in Trainer.fit(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
    542 self.state.status = TrainerStatus.RUNNING
    543 self.training = True
--> 544 call._call_and_handle_interrupt(
    545     self, self._fit_impl, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path
    546 )

File c:\Users\foo\anaconda3\envs\p100_venv\lib\site-packages\pytorch_lightning\trainer\call.py:44, in _call_and_handle_interrupt(trainer, trainer_fn, *args, **kwargs)
     42     if trainer.strategy.launcher is not None:
     43         return trainer.strategy.launcher.launch(trainer_fn, *args, trainer=trainer, **kwargs)
---> 44     return trainer_fn(*args, **kwargs)
     46 except _TunerExitException:
     47     _call_teardown_hook(trainer)

File c:\Users\foo\anaconda3\envs\p100_venv\lib\site-packages\pytorch_lightning\trainer\trainer.py:580, in Trainer._fit_impl(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)
    573 assert self.state.fn is not None
...
   1075 def __getitem__(self, idx: int) -> "CompositionalMetric":
   1076     """Construct compositional metric using the get item operator."""
-> 1077     return CompositionalMetric(lambda x: x[idx], self, None)

IndexError: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Environment

Current environment ``` Windows11 VSCode==1.88.1 PyTorch==2.2.2 pytorch-lightning==2.1.2 ```

More info

Please tell me what is involved this error if anyone knows solution.

@YuyaWake YuyaWake added bug Something isn't working needs triage Waiting to be triaged by maintainers labels Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Waiting to be triaged by maintainers ver: 2.1.x
Projects
None yet
Development

No branches or pull requests

1 participant