如何在 Transformers 库中截断 Bert 分词器
How to truncate a Bert tokenizer in Transformers library
我正在使用 Scibert 预训练模型来获取各种文本的嵌入。代码如下:
from transformers import *
tokenizer = AutoTokenizer.from_pretrained('allenai/scibert_scivocab_uncased', model_max_length=512, truncation=True)
model = AutoModel.from_pretrained('allenai/scibert_scivocab_uncased')
我已经将最大长度和 t运行cation 参数添加到分词器中,但不幸的是,它们没有 t运行cate results.If I 运行通过分词器的较长文本:
inputs = tokenizer("""long text""")
我收到以下错误:
Token indices sequence length is longer than the specified maximum
sequence length for this model (605 > 512). Running this sequence
through the model will result in indexing errors
显然,由于张量序列太长,我无法通过模型 运行 执行此操作。 t运行对输入进行分类以适应最大序列长度 512 的最简单方法是什么?
truncation
不是class构造函数(class reference)的参数,而是__call__
方法的参数。因此你应该使用:
tokenizer = AutoTokenizer.from_pretrained('allenai/scibert_scivocab_uncased', model_max_length=512)
len(tokenizer(text, truncation=True).input_ids)
输出:
512
我正在使用 Scibert 预训练模型来获取各种文本的嵌入。代码如下:
from transformers import *
tokenizer = AutoTokenizer.from_pretrained('allenai/scibert_scivocab_uncased', model_max_length=512, truncation=True)
model = AutoModel.from_pretrained('allenai/scibert_scivocab_uncased')
我已经将最大长度和 t运行cation 参数添加到分词器中,但不幸的是,它们没有 t运行cate results.If I 运行通过分词器的较长文本:
inputs = tokenizer("""long text""")
我收到以下错误:
Token indices sequence length is longer than the specified maximum sequence length for this model (605 > 512). Running this sequence through the model will result in indexing errors
显然,由于张量序列太长,我无法通过模型 运行 执行此操作。 t运行对输入进行分类以适应最大序列长度 512 的最简单方法是什么?
truncation
不是class构造函数(class reference)的参数,而是__call__
方法的参数。因此你应该使用:
tokenizer = AutoTokenizer.from_pretrained('allenai/scibert_scivocab_uncased', model_max_length=512)
len(tokenizer(text, truncation=True).input_ids)
输出:
512