pytorch-lightningを使用していて,以下のエラーが出ています.:
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.
そこで,登場するテンソルのうち形状がスカラーのものはすべて.item()
としたのですが,同様のエラーが出ます.
このモデルは8クラス分類を行う問題で,ラベルは0か1です.
具体的には,8つの感情について,感情が存在すれば1,そうでなければ0といった具合です.
何かお気づきの点がある方はもちろん,こうしてみたらどうか,などの提案でも構いませんのでどうぞご回答のほどよろしくお願いいたします.
データセットとモデルは以下の通りです:
##### 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
また,エラーの全文は以下の通りです:
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...
エラー文の中でCompositionalMetric(lambda x: x[idx], self, None
とあるので評価指数の計算時に問題が生じているのではないかと考えています.
print
でテンソルの形状を出力させたところ,以下のような形状でした:
input_ids shape: torch.Size([10])
attention_mask shape: torch.Size([10])
labels shape: torch.Size([8])
input_ids shape: torch.Size([10])
attention_mask shape: torch.Size([10])
labels shape: torch.Size([8])
==validation==
input_ids shape: torch.Size([2, 10])
attention_mask shape: torch.Size([2, 10])
labels shape: torch.Size([2, 8])
loss shape: torch.Size([])
last_hidden_state: torch.Size([2, 768])
logits shape: torch.Size([2, 8])
preds shape: torch.Size([2, 8])
2はbatch_size
を表しており,10はmax_length
です.
何か少しでもお気づきの点がある方がいらっしゃいましたら,ご教示いただけますと幸いです.
また,環境は以下の通りです:
Windows11
VSCode==1.88.1
PyTorch==2.1.2
pytorch-lightning==2.0.8
よろしくお願いいたします.
2024/05/08追記
torchmetricsでの評価指数の計算をあきらめsklearnを用いたところ解決しました.
しかし,根本的な解決策ではないため引き続きお気づきの点がある方はご回答のほどよろしくお願いいたします.