huggingface-transformers - 抱脸总结

标签 huggingface-transformers huggingface-tokenizers

我正在用变形金刚练习总结文本。
按照以下教程:https://huggingface.co/transformers/usage.html#summarization

from transformers import pipeline

summarizer = pipeline("summarization")

ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison.  Her next court appearance is scheduled for May 18.
"""

print(summarizer(ARTICLE, max_length=130, min_length=30))
我得到了预期的摘要文本,但是当我尝试另一个模型时(在教程中他们使用了 T5):
from transformers import AutoModelWithLMHead, AutoTokenizer

model = AutoModelWithLMHead.from_pretrained("t5-base")
tokenizer = AutoTokenizer.from_pretrained("t5-base")

# T5 uses a max_length of 512 so we cut the article to 512 tokens.
inputs = tokenizer.encode("summarize: " + ARTICLE, return_tensors="pt", max_length=512)
outputs = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
print(outputs)
我只得到这种输出:
tensor([[    0,    16,   792,     6,  1207,  3483,   235,     7,    65,   118,
          4464,   335,   648,     6,    28,  4169,    13,   160,  5281,     7,
         16198,   344,  5247,    11,  4407,     3,     5,   255,    19,  6141,
            12,   341,    36,  4464,    12,   662,  1076,     6,    11,    44,
            80,    97,     6,   255,  4464,  2641,  1076,    44,   728,     3,
             5]])
我想最后得到T5的总结文本,就像第一个教程一样。
我正在使用 Google Colab。

最佳答案

pipeline 类隐藏了使用模型所需执行的许多步骤。一般来说,模型不知道实际的单词,他们知道数字。 tokenizer 是将这些数字(称为 id)映射到实际单词的对象。当您打印变量 inputs 时,您还将看到这些数字:

#print(inputs)
tensor([[21603,    10,   368,  1060,    41,   254, 17235,    61, 10555,   301,
         13662,  1386,  3483,   235,     7,    47,  1902,   203,   625,     6,
           255,   530,  4464,    16,  1244, 13263,  1334,     6,   368,  1060,
             5,    71,   215,   865,     6,   255,   530,  4464,   541,    16,
          1244, 13263,  1334,     6,    68,    12,     3,     9,   315,   388,
            11,   406,  1227,  1967,    75,    53,   160,   166,  2553,     5,
          3462,   507,   477,   227,    24,  5281,     6,   255,   530,  1560,
          4513,   780,   541,     5,    37,    29,     6,  1386,  3483,   235,
             7, 10126,    96,   196,   103,   121,   874,    72,   648,     6,
          1664,   163,   441,   192,  1274,    13,   284,   119,     5,    86,
          8693,   255,  4464,   728,    72,     6,    48,    97,    16,     8,
          4027,    29,   226,     5,    86,    46,   917,    21,     3,     9,
          5281,  3344,     6,   255,  4568,    34,    47,   160,    96, 14672,
            11,   163,   121,  5281,     5,  1386,  3483,   235,     7,     6,
           230,  6352,     6,    19,  5008,   192,  4336, 12052,    13,    96,
          1647,    49,    53,     3,     9,  6136,  5009,    21,  9479,    16,
             8,   166,  1952,   976,     3, 13215,    12,   160,  6136,  6643,
            30,     8,  2735,  5281,  3344,   917,     6,  1315,    12,  1614,
          2691,     5,   749,     7,    15,  1071,  5535,   243,     8,  5281,
             7,   130,   294,    13,    46, 10653, 13236,     5,   461,  1701,
             6,   255,     3, 30827,    26,    59, 10945,    44,  1015,  8531,
          2243,    16,     8,  4027,    29,   226,     6,  1315,    12,   160,
          4917,     6, 14702, 16634,     6,   113, 14833,    12,  1670,   856,
             5,   621,  3140,  1614,     6,  1386,  3483,   235,     7,    47,
         10195,    11,  4977,    28, 14806,    13,   313,    11,  4336,     3,
           929,     7,  3968,    21,     3, 18280, 14801,    53,   139,     8,
           368,  1060, 24435,   190,    46,  3583,  7189,     6,   243, 29271,
          6206,  1954,  2185, 21180,     6,     3,     9,  2095,  5468,     7,
         15893,     5,    86,   792,     6,  1386,  3483,   235,     7,    65,
           118,  4464,   335,   648,     6,    28,  4169,    13,   160,  5281,
             7, 16198,   344,  5247,    11,  4407,     5,   432,  6935,   893,
            16,  1244, 13263,  1334,     6,  3230,  2834,     6,   368,  5092,
            42,     8,  4027,    29,   226,     5,   451,    19,  6141,    12,
           341,    36,  4464,    12,   662,  1076,     6,    11,    44,    80,
            97,     6,   255,    47,  4464,    12,  2641,  1076,    44,   728,
             6,     3, 29905,   497,     5,   749,     7,    15,  1071,  5535,
           243,     8, 10653, 13236,  1381,   128,    13,   160,  2553,     7,
             6,   113,  5132,    21,  3574,  6198,  2637, 10545,   227,     8,
          5281,     7,     5,  2372,  7759,     7,  2817,   163,   227,   224,
          9479,     7,   130,  3754,     5,    94,    47, 19363,   823,   136,
            13,     8,  1076,    56,    36, 23489,    15,    26,     5,    37,
           495,    47,     3,  4822,    12,     8,  4027,    29,   226,  3570,
         10154,    31,     7,  2126,    57, 24123,    11,  6150,     7, 27075,
            11,     8,  1775,    13, 31032,  3684,    31,     7, 25093,  6022,
             5, 13065,    13,     8,  1076,    33,    45,    78,    18,  9341,
            96,  1271,    18,    89,   521, 11918,   121,  1440,     6,   379,
         10438,     6,  9299,     6,  5664,     6,  6697,    11,  2148,    23,
             5,  1347, 21227,  2553,     6,  9053, 11740, 13509,  2562,     6,
            47,    20, 16262,    16,  3581,    12,   112,  4262,  6697,   227,
            46,  4962,    57,     8, 16761,   332, 17262,   159,    51, 16107,
          5205,     5,   156,     3, 21217,     6,  1386,  3483,   235,     7,
          8519,    95]])
您可以使用以下命令查看从 token 到 id 的映射作为字典:
tokenizer.get_vocab()
并查看例如 368 代表 ▁New1060 ▁York (ARTICLE 字符串的前两个词)。您只需添加以下行即可将输出转换回实际单词:
tokenizer.decode(outputs[0])
输出:
in total, barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002. she is believed to still be married to four men, and at one time, she married eight men at once.

关于huggingface-transformers - 抱脸总结,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62632043/

相关文章:

tensorflow - 微调 Blenderbot

python - 尝试微调 NER 的 ReformerModelWithLMHead (google/reformer-enwik8) 时出错

python - BertForSequenceClassification 是否对 CLS 向量进行分类?

python-3.x - 在 python 中应用预训练的 facebook/bart-large-cnn 进行文本摘要

python-3.x - 拥抱脸: tokenizer for masked lm question

pytorch - 为什么 GPT2Model 的一些权重没有初始化?

huggingface-transformers - 如何知道HuggingFace的管道文本输入是否超过512个 token

nlp - BERT - 是否需要添加新的 token 以在特定领域的环境中进行训练?

python - 操作系统错误 : Can't load tokenizer