【发布时间】:2021-11-23 03:56:27
【问题描述】:
我正在尝试使用 BERT 模型进行文本分类。由于文本是关于科学文本的,我打算使用 SicBERT 预训练模型:https://github.com/allenai/scibert
我遇到了一些限制,我想知道是否有任何解决方案:
-
当我想做标记化和批处理时,它只允许我使用 max_length。有没有办法使用更多的令牌。 512的这个限制不是意味着我在训练期间实际上没有使用所有的文本信息吗?使用所有文本的任何解决方案?
-
我尝试将此预训练库与其他模型(例如 DeBERTa 或 RoBERTa)一起使用。但它不让我。我只与 BERT 合作过。反正我能做到吗?
-
我知道这是一个普遍的问题,但有什么建议可以改进我的微调(从数据到超参数等)?目前,我的准确率约为 75%。谢谢
代码:
tokenizer = BertTokenizer.from_pretrained('allenai/scibert_scivocab_uncased')
encoded_data_train = tokenizer.batch_encode_plus(
df_train.text.values,
add_special_tokens=True,
return_attention_mask=True,
padding=True,
max_length=256
)
input_ids_train = encoded_data_train['input_ids']
attention_masks_train = encoded_data_train['attention_mask']
labels_train = torch.tensor(df_train.label.values)
dataset_train = TensorDataset(input_ids_train, attention_masks_train, labels_train)
dataloader_train = DataLoader(dataset_train,
sampler=RandomSampler(dataset_train),
batch_size=batch_size)
model = BertForSequenceClassification.from_pretrained('allenai/scibert_scivocab_uncased',
num_labels=len(labels),
output_attentions=False,
output_hidden_states=False)
epochs = 1
optimizer = AdamW(model.parameters(), lr=1e-5, eps=1e-8)
scheduler = get_linear_schedule_with_warmup(optimizer,
num_training_steps=len(dataloader_train)*epochs)
【问题讨论】:
标签: nlp pytorch text-classification huggingface-transformers bert-language-model