如何用拥抱面变压器管道定义总结的比例?
How to define ration of summary with hugging face transformers pipeline?
我正在使用以下代码总结一篇使用 huggingface-transformer 管道的文章。使用此代码:
from transformers import pipeline
summarizer = pipeline(task="summarization" )
summary = summarizer(text)
print(summary[0]['summary_text'])
如何定义摘要和原始文章之间的比例?比如原创文章的20%?
编辑 1:我实施了您建议的解决方案,但出现以下错误。这是我使用的代码:
summarizer(text, min_length = int(0.1 * len(text)), max_length = int(0.2 * len(text)))
print(summary[0]['summary_text'])
我得到的错误:
RuntimeError Traceback (most recent call last)
<ipython-input-9-bc11c5d8eb66> in <module>()
----> 1 summarizer(text, min_length = int(0.1 * len(text)), max_length = int(0.2 * len(text)))
2 print(summary[0]['summary_text'])
13 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
1482 # remove once script supports set_grad_enabled
1483 _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1484 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
1485
1486
RuntimeError: index out of range: Tried to access index 1026 out of table with 1025 rows. at /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:418
(请注意,此答案基于 2.6 版转换器的文档)
到目前为止,关于管道功能的文档似乎还很浅薄,这就是为什么我们必须深入挖掘的原因。当调用一个Python对象时,它内部引用了自己的__call__
属性,我们可以找到here for the summarization pipeline.
请注意,它允许我们(类似于底层 BartForConditionalGeneration
model)指定 min_length
和 max_length
,这就是为什么我们可以简单地调用类似
summarizer(text, min_length = 0.1 * len(text), max_length = 0.2 * len(text)
这将为您提供原始数据大约 10-20% 长度的摘要,但您当然可以根据自己的喜好进行更改。请注意,max_length
的 BartForConditionalGeneration
的默认值为 20(截至目前,min_length
未记录,但默认为 0),而摘要管道的值为 min_length=21
和max_length=142
.
我正在使用以下代码总结一篇使用 huggingface-transformer 管道的文章。使用此代码:
from transformers import pipeline
summarizer = pipeline(task="summarization" )
summary = summarizer(text)
print(summary[0]['summary_text'])
如何定义摘要和原始文章之间的比例?比如原创文章的20%?
编辑 1:我实施了您建议的解决方案,但出现以下错误。这是我使用的代码:
summarizer(text, min_length = int(0.1 * len(text)), max_length = int(0.2 * len(text)))
print(summary[0]['summary_text'])
我得到的错误:
RuntimeError Traceback (most recent call last)
<ipython-input-9-bc11c5d8eb66> in <module>()
----> 1 summarizer(text, min_length = int(0.1 * len(text)), max_length = int(0.2 * len(text)))
2 print(summary[0]['summary_text'])
13 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
1482 # remove once script supports set_grad_enabled
1483 _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1484 return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
1485
1486
RuntimeError: index out of range: Tried to access index 1026 out of table with 1025 rows. at /pytorch/aten/src/TH/generic/THTensorEvenMoreMath.cpp:418
(请注意,此答案基于 2.6 版转换器的文档)
到目前为止,关于管道功能的文档似乎还很浅薄,这就是为什么我们必须深入挖掘的原因。当调用一个Python对象时,它内部引用了自己的__call__
属性,我们可以找到here for the summarization pipeline.
请注意,它允许我们(类似于底层 BartForConditionalGeneration
model)指定 min_length
和 max_length
,这就是为什么我们可以简单地调用类似
summarizer(text, min_length = 0.1 * len(text), max_length = 0.2 * len(text)
这将为您提供原始数据大约 10-20% 长度的摘要,但您当然可以根据自己的喜好进行更改。请注意,max_length
的 BartForConditionalGeneration
的默认值为 20(截至目前,min_length
未记录,但默认为 0),而摘要管道的值为 min_length=21
和max_length=142
.