我试图使用text_classification管道从Huggingface.transformers执行情感分析,但有些文本超过了512个令牌的限制。我希望管道自动截断超限令牌。我尝试了this线程的方法,但是它没有工作。
这是我的代码:
nlp= pipeline('sentiment-analysis',
model=AutoModelForSequenceClassification.from_pretrained(
"model",
return_dict=False),
tokenizer=AutoTokenizer.from_pretrained(
"model",
return_dict=False),
framework="pt", return_all_scores=False)
output = nlp(article)发布于 2022-03-03 23:05:50
为了让每个人都面临同样的问题,下面是我解决这个问题的方法:
tokenizer = AutoTokenizer.from_pretrained("model", return_dict=False)
nlp= pipeline('sentiment-analysis',
model=AutoModelForSequenceClassification.from_pretrained(
"model",
return_dict=False),
tokenizer=tokenizer,
framework="pt", return_all_scores=False)
encoded_input = tokenizer(article, truncation=True, max_length=512)
decoded_input = tokenizer.decode(encoded_input["input_ids"], skip_special_tokens = True)
output = nlp(decoded_input)发布于 2022-03-04 09:47:58
另外,也是解决此问题的一种更直接的方法,您可以简单地将这些参数指定为管道中的**kwargs:
from transformers import pipeline
nlp = pipeline("sentiment-analysis")
nlp(long_input, truncation=True, max_length=512)https://stackoverflow.com/questions/71344409
复制相似问题