Pytorch 在collate_批次(自身、特征)——>;ValueError:只能将一个元素张量转换为Python标量
我试图在commonsense_qa数据集上使用transformers。我想使用数据作为 问题,选择1 问题、选择2等。 对于每一个选项,我也会为每一个输入传递一个问题。对于输入标识、注意标识、解码器输入标识、解码器注意标识,我执行以下操作。 最大长度=128 数据加载器是Pytorch 在collate_批次(自身、特征)——>;ValueError:只能将一个元素张量转换为Python标量,pytorch,huggingface-transformers,pytorch-dataloader,Pytorch,Huggingface Transformers,Pytorch Dataloader,我试图在commonsense_qa数据集上使用transformers。我想使用数据作为 问题,选择1 问题、选择2等。 对于每一个选项,我也会为每一个输入传递一个问题。对于输入标识、注意标识、解码器输入标识、解码器注意标识,我执行以下操作。 最大长度=128 数据加载器是 class NLPDataCollator(DataCollator): """ Extending the existing DataCollator to work wit
class NLPDataCollator(DataCollator):
"""
Extending the existing DataCollator to work with NLP dataset batches
"""
def collate_batch(self, features: List[Union[InputDataClass, Dict]]) -> Dict[str, torch.Tensor]:
first = features[0]
if isinstance(first, dict):
# NLP data sets current works presents features as lists of dictionary
# (one per example), so we will adapt the collate_batch logic for that
if "labels" in first and first["labels"] is not None:
if first["labels"].dtype == torch.int64:
labels = torch.tensor([f["labels"] for f in features], dtype=torch.long)
else:
labels = torch.tensor([f["labels"] for f in features], dtype=torch.float)
batch = {"labels": labels}
for k, v in first.items():
if k != "labels" and v is not None and not isinstance(v, str):
batch[k] = torch.stack([f[k] for f in features])
return batch
else:
# otherwise, revert to using the default collate_batch
return DefaultDataCollator().collate_batch(features)
培训师是
trainer = MultitaskTrainer(
model=model,
args=transformers.TrainingArguments(
output_dir="./models/",
overwrite_output_dir=True,
learning_rate=1e-5,
do_train=True,
do_eval=True,
num_train_epochs=3,
# Adjust batch size if this doesn't fit on the Colab GPU
per_device_train_batch_size=8,
save_steps=3000,
),
data_collator=NLPDataCollator(),
train_dataset=train_dataset,
eval_dataset = valid_dataset,
)
trainer.train()
我得到以下错误:
ValueError Traceback (most recent call last)
<ipython-input-49-68181c1063c6> in <module>()
26
27 )
---> 28 trainer.train()
8 frames
<ipython-input-39-4359c3725689> in collate_batch(self, features)
20 if "labels" in first and first["labels"] is not None:
21 if first["labels"].dtype == torch.int64:
---> 22 labels = torch.tensor([f["labels"] for f in features], dtype=torch.long)
23 else:
24 labels = torch.tensor([f["labels"] for f in features], dtype=torch.float)
ValueError: only one element tensors can be converted to Python scalars
ValueError回溯(最近一次调用)
在()
26
27 )
--->28.培训师
8帧
在collate_批次中(自身、特征)
20如果第一个和第一个[“标签”]中的“标签”不是无:
21如果第一个[“标签”].dtype==torch.int64:
--->22标签=torch.tensor([f[“标签”]表示特征中的f],dtype=torch.long)
23其他:
24个标签=torch.tensor([f[“标签”]表示特征中的f],dtype=torch.float)
ValueError:只能将一个元素张量转换为Python标量
ValueError Traceback (most recent call last)
<ipython-input-49-68181c1063c6> in <module>()
26
27 )
---> 28 trainer.train()
8 frames
<ipython-input-39-4359c3725689> in collate_batch(self, features)
20 if "labels" in first and first["labels"] is not None:
21 if first["labels"].dtype == torch.int64:
---> 22 labels = torch.tensor([f["labels"] for f in features], dtype=torch.long)
23 else:
24 labels = torch.tensor([f["labels"] for f in features], dtype=torch.float)
ValueError: only one element tensors can be converted to Python scalars