【问题标题】:How to define ration of summary with hugging face transformers pipeline?如何定义带有拥抱面变压器管道的摘要配给?
【发布时间】:2020-07-05 16:41:08
【问题描述】:

我正在使用下面的代码来总结一篇使用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

【问题讨论】:

    标签: pytorch huggingface-transformers


    【解决方案1】:

    (请注意,此答案基于变压器 2.6 版的文档)

    到目前为止,关于管道功能的文档似乎还很浅薄,这就是我们必须深入挖掘的原因。在调用 Python 对象时,它在内部引用了自己的 __call__ 属性,我们可以找到 here for the summarization pipeline

    请注意,它允许我们(类似于底层的BartForConditionalGeneration model)指定min_lengthmax_length,这就是为什么我们可以简单地调用类似的东西

    summarizer(text, min_length = 0.1 * len(text), max_length = 0.2 * len(text)
    

    这将为您提供大约 10-20% 长度的原始数据的摘要,但您当然可以根据自己的喜好进行更改。请注意,max_lengthBartForConditionalGeneration 的默认值为 20(截至目前,min_length 未记录,但默认为 0),而汇总管道具有值 min_length=21max_length=142

    【讨论】:

    • 感谢您的回答!请检查编辑@dennlinger
    • 那你能指定text的值吗?你在哪个版本的transformers 上运行?
    • 这是文本 - pastebin.com/XTy2gWQf,我正在使用 transformers==2.6.0
    • 预训练的 Bart 仅支持最多 1024 个标记的输入长度。因此,问题显然是您提到的文字太长。 (为了将来参考,由于 pastebin 链接过期,文本跨越了几个段落)。此时最好的选择是分成不同的部分,然后对每个部分进行总结。
    • 啊。在 BERT 中,1 个令牌定义为什么?
    猜你喜欢
    • 2020-03-02
    • 2018-01-02
    • 2022-06-28
    • 2023-02-14
    • 2021-12-12
    • 2020-07-22
    • 2020-10-12
    • 2020-06-17
    • 2021-12-11
    相关资源
    最近更新 更多