【问题标题】:Shall we lower case input data for (pre) training a BERT uncased model using huggingface?我们是否应该小写输入数据以(预)使用拥抱脸训练 BERT 无大小写模型?
【发布时间】:2020-10-09 11:40:29
【问题描述】:

我们是否应该小写输入数据以(预)使用拥抱脸训练 BERT 无大小写模型?我查看了 Thomas Wolf (https://github.com/huggingface/transformers/issues/92#issuecomment-444677920) 的回复,但不完全确定他是否是这个意思。

如果我们将文本小写会发生什么?

【问题讨论】:

  • 分词器应该为你做这件事。

标签: deep-learning nlp pytorch huggingface-transformers


【解决方案1】:

Tokenizer 会处理这个问题。

一个简单的例子:

import torch
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased', max_length = 10, padding_side = 'right')

input_ids = torch.tensor(tokenizer.encode('this is a cat', add_special_tokens=True, max_length = 10, pad_to_max_length = True)).unsqueeze(0)
print(input_ids)

input_ids = torch.tensor(tokenizer.encode('This is a Cat', add_special_tokens=True, max_length = 10, pad_to_max_length = True)).unsqueeze(0)
print(input_ids)

输出:

tensor([[ 101, 2023, 2003, 1037, 4937,  102,    0,    0,    0,    0]])
tensor([[ 101, 2023, 2003, 1037, 4937,  102,    0,    0,    0,    0]])

但如果是装箱的,

tokenizer = BertTokenizer.from_pretrained('bert-base-cased', max_length = 10, padding_side = 'right')

input_ids = torch.tensor(tokenizer.encode('this is a cat', add_special_tokens=True, max_length = 10, pad_to_max_length = True)).unsqueeze(0)
print(input_ids)

input_ids = torch.tensor(tokenizer.encode('This is a Cat', add_special_tokens=True, max_length = 10, pad_to_max_length = True)).unsqueeze(0)
print(input_ids)
tensor([[ 101, 1142, 1110,  170, 5855,  102,    0,    0,    0,    0]])

tensor([[ 101, 1188, 1110,  170, 8572,  102,    0,    0,    0,    0]])

【讨论】:

  • 谢谢,我也是这么想的!
  • 一般来说,在模型的 tokenizer_config.json 属性 do_lower_case 中指定了对大小写处理的分词器行为。
【解决方案2】:

我认为 bert-base-uncased 模型会将文本小写,而不管您传递给模型的内容如何。您还可以尝试使用玩具数据集并使用 BERT 标记器打印标记以便确认。

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 2021-08-16
    • 2020-05-27
    • 2022-01-23
    • 2021-10-07
    • 2017-10-25
    • 2020-05-06
    • 2023-01-31
    相关资源
    最近更新 更多