deep-learning - 我们是否应该小写输入数据以(预)使用拥抱脸训练 BERT 无大小写模型?
问题描述
我们是否应该小写输入数据以(预)使用拥抱脸训练 BERT 无大小写模型?我查看了 Thomas Wolf 的回复(https://github.com/huggingface/transformers/issues/92#issuecomment-444677920),但不完全确定他是否是这个意思。
如果我们小写文本会发生什么?
解决方案
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]])
推荐阅读
- azure-data-explorer - 将不同行的数据组合成一个字符串
- laravel - 将图像保存到数据库
- azure - 如何解决 azure iot readmessage 设备中的 Unhandle Exception:System.Net Socket.SocketException?
- python - 安装 pip 的问题(命令提示符)
- python-3.x - 在 **kwargs 中使用 f 字符串时出现语法错误
- php - 试图获取非对象 cms 传输的属性
- javascript - 如何修复“无法读取未定义的属性‘MakeCellsEditable’”错误
- android - 从 Okhttp3 响应中检索 JSON 对象
- r - ggplot中轴标签上括号内只有一个字符的上标
- python - 如何在 ctypes 中接收来自 .dll 的回调函数消息?