我正在运行一个句子转换模型,并试图截断我的令牌,但它似乎不起作用。我的代码是
from transformers import AutoModel, AutoTokenizer
model_name = "sentence-transformers/paraphrase-MiniLM-L6-v2"
model = AutoModel.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
text_tokens = tokenizer(text, padding=True, truncation=True, return_tensors="pt")
text_embedding = model(**text_tokens)["pooler_output"]我一直收到以下警告:
Token indices sequence length is longer than the specified maximum sequence length
for this model (909 > 512). Running this sequence through the model will result in
indexing errors我想知道为什么设置truncation=True不将我的文本截断到所需的长度?
发布于 2021-08-19 20:18:00
在创建令牌程序时,需要添加max_length参数,如下所示:
text_tokens = tokenizer(text, padding=True, max_length=512, truncation=True, return_tensors="pt")
原因:
不带truncation=True参数的max_length的序列长度等于模型可接受的最大输入长度。
这个模型是1e30或1000000000000000019884624838656。您可以通过打印出tokenizer.model_max_length来检查。
根据关于truncation的Huggingface文档,
True或“only_first”截断到max_length参数指定的最大长度,或者如果没有提供max_length (max_length=None),则截断模型所接受的最大长度。
https://stackoverflow.com/questions/68850172
复制相似问题