使用Transformers框架对BERT在SequenceClassification上进行微调

时间:2020-11-12 14:24:53

标签: python nlp bert-language-model huggingface-transformers

我目前正在对序列分类任务的BERT模型进行微调。为此,我使用了转换器框架。这需要在培训师中进行批量输入:https://huggingface.co/transformers/_modules/transformers/trainer.html

此处介绍了微调的工作方式:https://huggingface.co/transformers/custom_datasets.html 我认为该批处理必须看起来像我创建的一样,但是由于某些原因,我一直在出错。图片显示了数据集中的单个项目。

如果我将标签添加为张量,则将标签转换为张量的模型的一部分会出现错误。但是,当我将标签添加为列表时,我得到:预期输入batch_size(16)以匹配目标batch_size(2016)。 将批处理分配给BERT模型的正确方法是什么?

What my dataSet object looks like

这是我初始化模型的方式:

training_args = TrainingArguments(
     output_dir='C:',          # output directory
     num_train_epochs=3,              # total number of training epochs
     per_device_train_batch_size=16,  # batch size per device during training
     per_device_eval_batch_size=64,   # batch size for evaluation
     warmup_steps=500,                # number of warmup steps for learning rate scheduler
     weight_decay=0.01,               # strength of weight decay
     logging_dir='C:',            # directory for storing logs
     logging_steps=10,
 )

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
data_collator = DataCollatorForTokenClassification(tokenizer)
 
trainer = Trainer(
    model=model,                           # the instantiated ? Transformers model to be trained
    args=training_args,                    # training arguments, defined above
    data_collator=data_collator, #
    train_dataset=train_dataset,           # training dataset
    eval_dataset=test_dataset              # evaluation dataset
 )

trainer.train()   

0 个答案:

没有答案