当前位置:   article > 正文

NLP-文本摘要:利用预训练模型进行文本摘要任务【transformers:pipeline、T5、BART、Pegasus】_token indices sequence length is longer than the s

token indices sequence length is longer than the specified maximum sequence

一、pipeline

可以使用pipeline快速实现文本摘要

from transformers import pipeline

summarizer = pipeline(task="summarization", model='t5-small')

text = """
        summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

result = summarizer(text, max_length=130, min_length=30, do_sample=False)

print('result = ', result)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

打印结果:

Token indices sequence length is longer than the specified maximum sequence length for this model (593 > 512). Running this sequence through the model will result in indexing errors
result =  [{'summary_text': 'a new group of bishops and archbishops will become cardinals . they come from 13 countries from every continent . the new cards are often referred to as the princes of the Catholic Church .'}]

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4

二、T5

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\t5-base')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\t5-base')

# 用T5做文本摘要任务,前面加 "summarize:"识别符
text = """
        summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')

print('inputs = ', inputs)

summary_ids = model.generate(inputs['input_ids'])

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

打印结果:

Ignored unknown kwarg option direction
inputs =  {'input_ids': tensor([[21603,    10,    41,   254, 17235,    61,  3809,     8,   511,    97,
           383,   112,     3, 16281,  4710,     6, 17384, 11065,    65,  2162,
             3,     9,   126,   563,    13, 25214,     7,    11, 11508, 11514,
         10776,     7,   356,    12,   582,   895, 10270,     7,  1636,    11,
            79,   369,    45,    66,   147,     8,   296,     5, 17384, 11065,
           243,  1771,    24,     3,    88,   133,  1520,     3,     9,  1338,
            13,   895, 10270,     7,    30,  2083,   968,    96,    26,  7920,
            84,    27,    56,   564,   627,   126, 21967,     7,   113,     6,
          1107,    45,  1179,  1440,    45,   334, 10829,     6,  6571,     8,
             3,  8482,     7, 26175,  2416,   344,     8,  2345,    13,  7332,
            11,     8,  1090,  2345,    15,     7,   915,    16,     8,   296,
           976,  1315,    12, 25770,  5061,     5,   368,   895, 10270,     7,
            33,   373,   359,   250,    79,   356,     8,  5739,    16,     8,
          2078,    11,    92, 11924,     8,   416,  2783,    15,     6, 19602,
          5523, 25770, 25224,  1079,   301,     5, 10618,   243,     5,   328,
            33,  1664,     3,  4822,    12,    38,     8, 22277,     7,    13,
             8,  6502,  2345,     5,    37,   126,   895, 10270,     7,   369,
            45,  1440,   224,    38, 22138,     6,   368,  5725,    11, 27274,
             5,    96,  3713,    19,     3,     9,  2783,    15,   113,   182,
           231,  2746,    12,  1535,    91,    12,   151,    30,     8,  6346,
             7,     6,    11,    25,  3133,   217,    24,    16,    48,   356,
           976, 10618,   243,     5,    96,  3774,    31,    60,  2508,    81,
           895, 10270,     7,    45,  3115, 20633,  1747,     6,   114,  9702,
           781,   221,     6,     8,  5824,  3368,    13,   304,  1725,     9,
             6, 21099,     6, 10508,     6, 30758,   535,   299,    21,     8,
           511,    97,   437, 11065,    31,  4356,     6,   150,  5452,   263,
             8,   570,     5,    96,   371,    52, 11389,     7,    31,  3275,
            19,   182,   964,    10,   216,  2746,    12,   281,    12,     8,
         20187,   158,  5082,    88,  2593,  1066,   145,  1747,    24,    33,
           641,   420,    18,    88, 19649,    28,   895, 10270,     7,   976,
         10618,   243,     5, 14702,  5377,   155,   235,     6,     3,     9,
          5812,    13,  2078,   892,    44,  2566,   152,   636,    16,   368,
          5092,     6,  4466,    24, 11065,  2162,   112,   126, 21079,    13,
           895, 10270,     7,    30,     8,  6502,   377, 11535,    13,     8,
         12741,  8237,    63,     6,    84, 18681,    15,     7,     8,   719,
            13,     8, 22673,    12,  1850,    31,  3879,  4687,    16, 15659,
           109,  6015,     5,    96,  7638, 18886,    13,   386,  7624,  1076,
            45,   623,   550,     6,     8, 17384,    31,     7,  3703,    21,
           895, 10270,   497,    24,   334,   415,  2078, 15314,     3,     9,
           286,    44,     8,   600,   953,   535,    86,   119,  1234,     6,
         11065,  2746,     3,     9,    72,    20, 21411,  2078,    11,  2746,
            12,  1616,  5139,   912,    45,   422,  2597,    24,  2561,   623,
            45,  6502,   159,    51,    31,     7,   579,  6881,     6,  5377,
           155,   235,   243,     5,   466,   744,    31,    17,  1243, 11065,
            19,     8,   166, 19068,  5982,    12,     3,     9,   102,  2700,
           895, 10270,     7,    45,     8,  2421,   296,     6,   713,     5,
         22738,    16,     8, 13978,     7,     6,    46,  3094,   381,    13,
          6271,   797,  2078,   904,   130,  2650,   895, 10270,     7,     6,
            11,    16,     8,  8754,     7,     6,   472,     5,  1079,     3,
             4,     4, 13671,     6,  4068, 11065,    54,   106,  1601,   336,
           215,     6,  7817,     8,   166,   895, 10270,     7,    45,  3411,
             6,     8, 12729,    11,  2648,     5,    86,   811,    12,     8,
           627,   126,   895, 10270,     7, 11065,  2650,    30,  1771,     6,
           874, 10611, 11508, 11514, 10776,     7,    11, 25214,     7,    56,
            92,    36, 13242,    38,   895, 10270,     7,     5,  2506,   215,
             6, 17384, 11065,  7817,   957,   126,   895, 10270,     7,     6,
           379, 25214,     7,    45, 22179,    11,  4152,  2917,     9,  1699,
             7,    32,     5, 19602,    31,     7,  4173, 27575,    11,  2144,
         10333,   109,   377,  8038,    76,  9859,    12,    48,   934,     5,
             1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}

summary_ids =  tensor([[    0,   126,   895, 10270,     7,   369,    45,  1179,  1440,    45,
           334, 10829,     3,     5,    79,    33,  1664,     3,  4822,    12]])

['new cardinals come from 13 countries from every continent . they are sometimes referred to']
['new cardinals come from 13 countries from every continent . they are sometimes referred to']

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

方式02、from transformers import T5Tokenizer, T5ForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import T5Tokenizer, T5ForConditionalGeneration

tokenizer = T5Tokenizer.from_pretrained(r'D:\Pretrained_Model\t5-base')
model = T5ForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\t5-base')

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')

print('inputs = ', inputs)

summary_ids = model.generate(inputs['input_ids'])

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

打印结果:

inputs =  {'input_ids': tensor([[   41,   254, 17235,    61,  3809,     8,   511,    97,   383,   112,
             3, 16281,  4710,     6, 17384, 11065,    65,  2162,     3,     9,
           126,   563,    13, 25214,     7,    11, 11508, 11514, 10776,     7,
           356,    12,   582,   895, 10270,     7,  1636,    11,    79,   369,
            45,    66,   147,     8,   296,     5, 17384, 11065,   243,  1771,
            24,     3,    88,   133,  1520,     3,     9,  1338,    13,   895,
         10270,     7,    30,  2083,   968,    96,    26,  7920,    84,    27,
            56,   564,   627,   126, 21967,     7,   113,     6,  1107,    45,
          1179,  1440,    45,   334, 10829,     6,  6571,     8,     3,  8482,
             7, 26175,  2416,   344,     8,  2345,    13,  7332,    11,     8,
          1090,  2345,    15,     7,   915,    16,     8,   296,   976,  1315,
            12, 25770,  5061,     5,   368,   895, 10270,     7,    33,   373,
           359,   250,    79,   356,     8,  5739,    16,     8,  2078,    11,
            92, 11924,     8,   416,  2783,    15,     6, 19602,  5523, 25770,
         25224,  1079,   301,     5, 10618,   243,     5,   328,    33,  1664,
             3,  4822,    12,    38,     8, 22277,     7,    13,     8,  6502,
          2345,     5,    37,   126,   895, 10270,     7,   369,    45,  1440,
           224,    38, 22138,     6,   368,  5725,    11, 27274,     5,    96,
          3713,    19,     3,     9,  2783,    15,   113,   182,   231,  2746,
            12,  1535,    91,    12,   151,    30,     8,  6346,     7,     6,
            11,    25,  3133,   217,    24,    16,    48,   356,   976, 10618,
           243,     5,    96,  3774,    31,    60,  2508,    81,   895, 10270,
             7,    45,  3115, 20633,  1747,     6,   114,  9702,   781,   221,
             6,     8,  5824,  3368,    13,   304,  1725,     9,     6, 21099,
             6, 10508,     6, 30758,   535,   299,    21,     8,   511,    97,
           437, 11065,    31,  4356,     6,   150,  5452,   263,     8,   570,
             5,    96,   371,    52, 11389,     7,    31,  3275,    19,   182,
           964,    10,   216,  2746,    12,   281,    12,     8, 20187,   158,
          5082,    88,  2593,  1066,   145,  1747,    24,    33,   641,   420,
            18,    88, 19649,    28,   895, 10270,     7,   976, 10618,   243,
             5, 14702,  5377,   155,   235,     6,     3,     9,  5812,    13,
          2078,   892,    44,  2566,   152,   636,    16,   368,  5092,     6,
          4466,    24, 11065,  2162,   112,   126, 21079,    13,   895, 10270,
             7,    30,     8,  6502,   377, 11535,    13,     8, 12741,  8237,
            63,     6,    84, 18681,    15,     7,     8,   719,    13,     8,
         22673,    12,  1850,    31,  3879,  4687,    16, 15659,   109,  6015,
             5,    96,  7638, 18886,    13,   386,  7624,  1076,    45,   623,
           550,     6,     8, 17384,    31,     7,  3703,    21,   895, 10270,
           497,    24,   334,   415,  2078, 15314,     3,     9,   286,    44,
             8,   600,   953,   535,    86,   119,  1234,     6, 11065,  2746,
             3,     9,    72,    20, 21411,  2078,    11,  2746,    12,  1616,
          5139,   912,    45,   422,  2597,    24,  2561,   623,    45,  6502,
           159,    51,    31,     7,   579,  6881,     6,  5377,   155,   235,
           243,     5,   466,   744,    31,    17,  1243, 11065,    19,     8,
           166, 19068,  5982,    12,     3,     9,   102,  2700,   895, 10270,
             7,    45,     8,  2421,   296,     6,   713,     5, 22738,    16,
             8, 13978,     7,     6,    46,  3094,   381,    13,  6271,   797,
          2078,   904,   130,  2650,   895, 10270,     7,     6,    11,    16,
             8,  8754,     7,     6,   472,     5,  1079,     3,     4,     4,
         13671,     6,  4068, 11065,    54,   106,  1601,   336,   215,     6,
          7817,     8,   166,   895, 10270,     7,    45,  3411,     6,     8,
         12729,    11,  2648,     5,    86,   811,    12,     8,   627,   126,
           895, 10270,     7, 11065,  2650,    30,  1771,     6,   874, 10611,
         11508, 11514, 10776,     7,    11, 25214,     7,    56,    92,    36,
         13242,    38,   895, 10270,     7,     5,  2506,   215,     6, 17384,
         11065,  7817,   957,   126,   895, 10270,     7,     6,   379, 25214,
             7,    45, 22179,    11,  4152,  2917,     9,  1699,     7,    32,
             5, 19602,    31,     7,  4173, 27575,    11,  2144, 10333,   109,
           377,  8038,    76,  9859,    12,    48,   934,     5,     1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}

summary_ids =  tensor([[    0,   126,   895, 10270,     7,   369,    45,  1179,  1440,    45,
           334, 10829,     3,     5,    79,    33,   557,     3,  4822,    12]])

['new cardinals come from 13 countries from every continent . they are often referred to']
['new cardinals come from 13 countries from every continent . they are often referred to']

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

三、BART

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\bart-base')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\bart-base')

# 用T5做文本摘要任务,前面加 "summarize:"识别符
text = """
        summarize: (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')

print('inputs = ', inputs)

summary_ids = model.generate(inputs['input_ids'])

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

打印结果:

Ignored unknown kwarg option direction
inputs =  {'input_ids': tensor([[    0, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437, 40402,
            35,    36, 16256,    43,  2709,     5,   200,    86,   148,    39,
         13102,  5073,     6,  8509,  5075,    34,   585,    10,    92,   333,
             9, 19929,     8,  9599,   428, 32084,   278,     7,   555,  1886,
         17552,   480,     8,    51,   283,    31,    70,    81,     5,   232,
             4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  8509,
          5075,    26,   395,    14,    37,    74,   946,    10,   529,     9,
          1886, 17552,    15,   902,   501,    22, 37460,    61,    38,    40,
           766,   379,    92,  6293,    54,     6,   567,    31,   508,   749,
            31,   358,  9183,     6, 19318,     5,  9473,  3006,  1168, 26633,
          5678,   227,     5,  2197,     9,  8947,     8,     5,  1989, 37391,
          1455,    11,     5,   232,    60,   309,     7, 11484,  4611,     4,
         50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,   188,  1886,
         17552,    32,   460,   505,   142,    51,   278,     5,  6328,    11,
             5,  2352,     8,    67, 10371,     5,   220, 16627,     6,  3480,
          3596, 11484,  9821,   610,   226,     4,  3823,    26,     4,   252,
            32,  2128,  4997,     7,    25,     5, 39978,     9,     5,  4019,
          2197,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,
            20,    92,  1886, 17552,   283,    31,   749,   215,    25, 13934,
             6,   188,  3324,     8,  7095,     4, 50118,  1437,  1437,  1437,
          1437,  1437,  1437,  1437,    22,   713,    16,    10, 16627,    54,
           182,   203,  1072,     7,  1338,    66,     7,    82,    15,     5,
          5510,     6,     8,    47,  2563,   192,    14,    11,    42,   278,
            60,  3823,    26,     4,    22,  1185,   214,  1686,    59,  1886,
         17552,    31,  3700, 16042,  2127,     6,   101,  6268,  3060,  2794,
             6,     5,  3073,  2946,     9, 17922,   102,     6, 12276,     6,
          6547,     6, 17609,    72, 50118,  1437,  1437,  1437,  1437,  1437,
          1437,  1437,   125,    13,     5,   200,    86,   187,  5075,   108,
           729,     6,   117,  1791,   156,     5,   889,     4, 50118,  1437,
          1437,  1437,  1437,  1437,  1437,  1437,    22, 38461,   354,   108,
          6184,    16,   182,   699,    35,    91,  1072,     7,   213,     7,
             5, 20456, 41464,   918,  1195,    87,  2127,    14,    32,   416,
           299,    12, 18888,    19,  1886, 17552,    60,  3823,    26,     4,
         50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  5469,  3043,
         29765,     6,    10,  3097,     9,  2352,   750,    23,  3350,   260,
           589,    11,   188,  3123,     6,  1581,    14,  5075,   585,    39,
            92, 15777,     9,  1886, 17552,    15,     5,  4019, 37072,     9,
             5, 14230, 39880,     6,    61, 16293,  1626,     5,   825,     9,
             5,  3771,   118,     7,  5772,   108, 32357,    11, 26557,     4,
         50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,    22,  4148,
         23220,     9,   130, 11036,   604,    31,   444,   409,     6,     5,
          8509,    18,  5717,    13, 32533,   224,    14,   358,   400,  2352,
          8613,    10,   317,    23,     5,   380,  2103,    72, 50118,  1437,
          1437,  1437,  1437,  1437,  1437,  1437,    96,    97,  1617,     6,
          5075,  1072,    10,    55, 34930,  2352,     8,  1072,     7,  1798,
          3114,  2956,    31,   650,  1822,    14,  2662,   444,    31, 42580,
            18,   476,  5228,     6,  3043, 29765,    26,     4, 50118,  1437,
          1437,  1437,  1437,  1437,  1437,  1437,   280,   630,    75,  1266,
          5075,    16,     5,    78, 29476,  4822,     7,  9653,  1886, 17552,
            31,     5,  2623,   232,     6,   600,     4, 22856,    11,     5,
         18283,    29,     6,    41,  2284,   346,     9,  5862,   470,  2352,
          2262,    58,  1440,  1886, 17552,     6,     8,    11,     5,  7571,
            29,     6,   312,     4,   610, 26166, 24457,     6,  2661,  5075,
         32839,  1538,    94,    76,     6,  3873,     5,    78,  1886, 17552,
            31,  1429,     6,     5,  5639,     8,  1327,     4, 50118,  1437,
          1437,  1437,  1437,  1437,  1437,  1437,    96,  1285,     7,     5,
           379,    92,  1886, 17552,  5075,  1440,    15,   395,     6,   292,
          3562,  9599,   428, 32084,     8, 19929,    40,    67,    28,  7809,
            25,  1886, 17552,     4, 50118,  1437,  1437,  1437,  1437,  1437,
          1437,  1437,  1426,    76,     6,  8509,  5075,  3873,   753,    92,
          1886, 17552,     6,   217, 19929,    31, 17009,     8, 18294,  1243,
         21433,   139,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,
          1437,  3480,    18,  3028, 12032,     8,  4845, 14286,   459,   274,
          5223,   257,  3162,     7,    42,   266,     4, 50118,     2]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}

summary_ids =  tensor([[    2,     0,  1640, 16256,    43,  1437,  1437,  1437,  2537,  1437,
          1437,    36, 16256,    43,  2709,     5,   200,    86,   148,     2]])

['(CNN)   Â   (CNN)For the second time during']
['(CNN)   Â   (CNN)For the second time during']

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99

方式02、from transformers import BartTokenizer, BartForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/t5/modeling_t5.py
from transformers import BartTokenizer, BartForConditionalGeneration

tokenizer = BartTokenizer.from_pretrained(r'D:\Pretrained_Model\bart-base')
model = BartForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\bart-base')

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')

print('inputs = ', inputs)

summary_ids = model.generate(inputs['input_ids'])

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

打印结果:

inputs =  {'input_ids': tensor([[    0, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  1437,
            36, 16256,    43,  2709,     5,   200,    86,   148,    39, 13102,
          5073,     6,  8509,  5075,    34,   585,    10,    92,   333,     9,
         19929,     8,  9599,   428, 32084,   278,     7,   555,  1886, 17552,
           480,     8,    51,   283,    31,    70,    81,     5,   232,     4,
         50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,  8509,  5075,
            26,   395,    14,    37,    74,   946,    10,   529,     9,  1886,
         17552,    15,   902,   501,    22, 37460,    61,    38,    40,   766,
           379,    92,  6293,    54,     6,   567,    31,   508,   749,    31,
           358,  9183,     6, 19318,     5,  9473,  3006,  1168, 26633,  5678,
           227,     5,  2197,     9,  8947,     8,     5,  1989, 37391,  1455,
            11,     5,   232,    60,   309,     7, 11484,  4611,     4, 50118,
          1437,  1437,  1437,  1437,  1437,  1437,  1437,   188,  1886, 17552,
            32,   460,   505,   142,    51,   278,     5,  6328,    11,     5,
          2352,     8,    67, 10371,     5,   220, 16627,     6,  3480,  3596,
         11484,  9821,   610,   226,     4,  3823,    26,     4,   252,    32,
          2128,  4997,     7,    25,     5, 39978,     9,     5,  4019,  2197,
             4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,    20,
            92,  1886, 17552,   283,    31,   749,   215,    25, 13934,     6,
           188,  3324,     8,  7095,     4, 50118,  1437,  1437,  1437,  1437,
          1437,  1437,  1437,    22,   713,    16,    10, 16627,    54,   182,
           203,  1072,     7,  1338,    66,     7,    82,    15,     5,  5510,
             6,     8,    47,  2563,   192,    14,    11,    42,   278,    60,
          3823,    26,     4,    22,  1185,   214,  1686,    59,  1886, 17552,
            31,  3700, 16042,  2127,     6,   101,  6268,  3060,  2794,     6,
             5,  3073,  2946,     9, 17922,   102,     6, 12276,     6,  6547,
             6, 17609,    72, 50118,  1437,  1437,  1437,  1437,  1437,  1437,
          1437,   125,    13,     5,   200,    86,   187,  5075,   108,   729,
             6,   117,  1791,   156,     5,   889,     4, 50118,  1437,  1437,
          1437,  1437,  1437,  1437,  1437,    22, 38461,   354,   108,  6184,
            16,   182,   699,    35,    91,  1072,     7,   213,     7,     5,
         20456, 41464,   918,  1195,    87,  2127,    14,    32,   416,   299,
            12, 18888,    19,  1886, 17552,    60,  3823,    26,     4, 50118,
          1437,  1437,  1437,  1437,  1437,  1437,  1437,  5469,  3043, 29765,
             6,    10,  3097,     9,  2352,   750,    23,  3350,   260,   589,
            11,   188,  3123,     6,  1581,    14,  5075,   585,    39,    92,
         15777,     9,  1886, 17552,    15,     5,  4019, 37072,     9,     5,
         14230, 39880,     6,    61, 16293,  1626,     5,   825,     9,     5,
          3771,   118,     7,  5772,   108, 32357,    11, 26557,     4, 50118,
          1437,  1437,  1437,  1437,  1437,  1437,  1437,    22,  4148, 23220,
             9,   130, 11036,   604,    31,   444,   409,     6,     5,  8509,
            18,  5717,    13, 32533,   224,    14,   358,   400,  2352,  8613,
            10,   317,    23,     5,   380,  2103,    72, 50118,  1437,  1437,
          1437,  1437,  1437,  1437,  1437,    96,    97,  1617,     6,  5075,
          1072,    10,    55, 34930,  2352,     8,  1072,     7,  1798,  3114,
          2956,    31,   650,  1822,    14,  2662,   444,    31, 42580,    18,
           476,  5228,     6,  3043, 29765,    26,     4, 50118,  1437,  1437,
          1437,  1437,  1437,  1437,  1437,   280,   630,    75,  1266,  5075,
            16,     5,    78, 29476,  4822,     7,  9653,  1886, 17552,    31,
             5,  2623,   232,     6,   600,     4, 22856,    11,     5, 18283,
            29,     6,    41,  2284,   346,     9,  5862,   470,  2352,  2262,
            58,  1440,  1886, 17552,     6,     8,    11,     5,  7571,    29,
             6,   312,     4,   610, 26166, 24457,     6,  2661,  5075, 32839,
          1538,    94,    76,     6,  3873,     5,    78,  1886, 17552,    31,
          1429,     6,     5,  5639,     8,  1327,     4, 50118,  1437,  1437,
          1437,  1437,  1437,  1437,  1437,    96,  1285,     7,     5,   379,
            92,  1886, 17552,  5075,  1440,    15,   395,     6,   292,  3562,
          9599,   428, 32084,     8, 19929,    40,    67,    28,  7809,    25,
          1886, 17552,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,
          1437,  1426,    76,     6,  8509,  5075,  3873,   753,    92,  1886,
         17552,     6,   217, 19929,    31, 17009,     8, 18294,  1243, 21433,
           139,     4, 50118,  1437,  1437,  1437,  1437,  1437,  1437,  1437,
          3480,    18,  3028, 12032,     8,  4845, 14286,   459,   274,  5223,
           257,  3162,     7,    42,   266,     4, 50118,     2]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}

summary_ids =  tensor([[    2,     0,  1640, 16256,    43,   480,  1437,  1437,  2537,  1437,
          1437,  1437,    36, 16256,    43,  2709,     5,   200,    86,     2]])

['(CNN) --  Â    (CNN)For the second time']
['(CNN) --  Â    (CNN)For the second time']

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

四、Pegasus

方式01、from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_model\pegasus-cnn_dailymail')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_model\pegasus-cnn_dailymail')

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer.encode(text)
inputs = torch.tensor([inputs])

print('inputs = ', inputs)

summary_ids = model.generate(inputs)

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

打印结果:

inputs =  tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,
           108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,
           111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,
           157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,
           243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,
           116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,
           738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,
           290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,
           109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,
           109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,
           116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,
           109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,
          4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,
           127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,
          1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,
           130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,
           117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,
           112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,
           120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,
           131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,
           108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,
           108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,
           453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,
           109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,
           151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,
          3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,
           122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,
           497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,
           115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,
         11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,
         60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,
          1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,
           339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,
           116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,
           114,   295,   134,   109,   461,   826,   496,   222,   176,   989,
           108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,
          1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,
         52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,
           107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,
           110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,
           278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,
           142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,
         30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,
           107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,
           232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,
           109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,
           177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,
         66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,
           116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,
         30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,
           107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,
         28929,  5674,   112,   136,   731,   107,     1]])

summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,
         16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,
          3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,
           131,  2974,   110,   107,     1]])

["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

方式02、from transformers import PegasusTokenizer, PegasusForConditionalGeneration

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
from transformers import PegasusTokenizer, PegasusForConditionalGeneration

tokenizer = PegasusTokenizer.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')
model = PegasusForConditionalGeneration.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer(text, max_length=1024, truncation=True, return_tensors='pt')

print('inputs = ', inputs)

summary_ids = model.generate(inputs['input_ids'])

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

打印结果:

inputs =  {'input_ids': tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,
           108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,
           111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,
           157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,
           243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,
           116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,
           738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,
           290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,
           109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,
           109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,
           116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,
           109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,
          4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,
           127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,
          1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,
           130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,
           117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,
           112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,
           120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,
           131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,
           108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,
           108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,
           453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,
           109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,
           151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,
          3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,
           122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,
           497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,
           115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,
         11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,
         60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,
          1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,
           339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,
           116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,
           114,   295,   134,   109,   461,   826,   496,   222,   176,   989,
           108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,
          1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,
         52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,
           107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,
           110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,
           278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,
           142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,
         30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,
           107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,
           232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,
           109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,
           177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,
         66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,
           116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,
         30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,
           107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,
         28929,  5674,   112,   136,   731,   107,     1]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])}

summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,
         16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,
          3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,
           131,  2974,   110,   107,     1]])

["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83



参考资料:
Transformers预训练模型使用:文本摘要 Summarization

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/477223?site
推荐阅读
相关标签
  

闽ICP备14008679号