【问题标题】:How to use SciBERT in the best manner?如何以最佳方式使用 SciBERT?
【发布时间】:2021-11-23 03:56:27
【问题描述】:

我正在尝试使用 BERT 模型进行文本分类。由于文本是关于科学文本的,我打算使用 SicBERT 预训练模型:https://github.com/allenai/scibert

我遇到了一些限制,我想知道是否有任何解决方案:

  1. 当我想做标记化和批处理时,它只允许我使用 max_length。有没有办法使用更多的令牌。 512的这个限制不是意味着我在训练期间实际上没有使用所有的文本信息吗?使用所有文本的任何解决方案?

  2. 我尝试将此预训练库与其他模型(例如 DeBERTa 或 RoBERTa)一起使用。但它不让我。我只与 BERT 合作过。反正我能做到吗?

  3. 我知道这是一个普遍的问题,但有什么建议可以改进我的微调(从数据到超参数等)?目前,我的准确率约为 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


    【解决方案1】:

    当我想做标记化和批处理时,它只允许我使用

    是的,您没有使用完整的文本。这是 BERT 和 T5 模型的限制之一,它们分别限制使用 512 和 1024 个令牌。据我所知。

    我可以建议你使用 LongformerBigbirdReformer 模型,它们可以分别处理高达 16k409664k 标记的序列长度。这些对于处理较长的文本(如科学文档)非常有用。

    我尝试将此预训练库与其他模型(例如 DeBERTa 或 RoBERTa)一起使用。但它不让我。我只与 BERT 合作过。反正我能做到吗?

    SciBERT 实际上是一个预训练的 BERT 模型。 有关将 BERT 转换为 ROBERTa 的可行性的更多详细信息,请参阅此issue

    Since you're working with a BERT model that was pre-trained, you unfortunately won't be able to change the tokenizer now from a WordPiece (BERT) to a Byte-level BPE (RoBERTa).

    我知道这是一个普遍的问题,但我可以提出任何建议 改进我的微调(从数据到超参数等)?现在, 我的准确率约为 79%。

    我会首先尝试调整最重要的超参数learning_rate。然后,我将探索AdamW 优化器和num_warmup_steps 调度器超参数的不同值。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2023-04-08
      • 2020-06-25
      • 2011-12-08
      • 2015-08-13
      • 2019-04-26
      • 1970-01-01
      • 2013-06-15
      • 2013-10-28
      相关资源
      最近更新 更多