当前位置:   article > 正文

BERTScore——一种衡量两段文本相似度的方法

bertscore

论文原文:原文链接

github链接:github链接

BertScore是一种用于衡量文本相似度的指标,它基于BERT模型,通过计算两个句子在BERT模型中的embedding编码之间的cosine相似度,来评估它们的相似度。

BertScore的基本理念是将两个句子分别输入到预先训练好的BERT模型中,获取句子的语义表示,然后比较这些表示的相似程度。与其他基于词向量的相似度指标不同,BertScore考虑了上下文信息和语义信息,因此能够更准确地衡量句子之间的相似度,特别是对于语义相似但表达不同的句子,BertScore更能捕捉到它们之间的相似性。

1.安装:

pip install bert-score

2.使用

通过model_type来指定需要的模型,会自动从huggingface下载,支持的模型有:

  1. model2layers = {
  2. "bert-base-uncased": 9, # 0.6925188074454226
  3. "bert-large-uncased": 18, # 0.7210358126642836
  4. "bert-base-cased-finetuned-mrpc": 9, # 0.6721947475618048
  5. "bert-base-multilingual-cased": 9, # 0.6680687802637132
  6. "bert-base-chinese": 8,
  7. "roberta-base": 10, # 0.706288719158983
  8. "roberta-large": 17, # 0.7385974720781534
  9. "roberta-large-mnli": 19, # 0.7535618640417984
  10. "roberta-base-openai-detector": 7, # 0.7048158349432633
  11. "roberta-large-openai-detector": 15, # 0.7462770207355116
  12. "xlnet-base-cased": 5, # 0.6630103662114238
  13. "xlnet-large-cased": 7, # 0.6598800720297179
  14. "xlm-mlm-en-2048": 6, # 0.651262570131464
  15. "xlm-mlm-100-1280": 10, # 0.6475166424401905
  16. # "scibert-scivocab-uncased": 8, # 0.6590354319927313
  17. # "scibert-scivocab-cased": 9, # 0.6536375053937445
  18. # "scibert-basevocab-uncased": 9, # 0.6748944832703548
  19. # "scibert-basevocab-cased": 9, # 0.6524624150542374
  20. "allenai/scibert_scivocab_uncased": 8, # 0.6590354393124127
  21. "allenai/scibert_scivocab_cased": 9, # 0.6536374902465466
  22. "nfliu/scibert_basevocab_uncased": 9, # 0.6748945076082333
  23. "distilroberta-base": 5, # 0.6797558139322964
  24. "distilbert-base-uncased": 5, # 0.6756659152782033
  25. "distilbert-base-uncased-distilled-squad": 4, # 0.6718318036382493
  26. "distilbert-base-multilingual-cased": 5, # 0.6178131050889238
  27. "albert-base-v1": 10, # 0.654237567249745
  28. "albert-large-v1": 17, # 0.6755890754323239
  29. "albert-xlarge-v1": 16, # 0.7031844211905911
  30. "albert-xxlarge-v1": 8, # 0.7508642218461096
  31. "albert-base-v2": 9, # 0.6682455591837927
  32. "albert-large-v2": 14, # 0.7008537594374035
  33. "albert-xlarge-v2": 13, # 0.7317228357869254
  34. "albert-xxlarge-v2": 8, # 0.7505160257184014
  35. "xlm-roberta-base": 9, # 0.6506799445871697
  36. "xlm-roberta-large": 17, # 0.6941551437476826
  37. "google/electra-small-generator": 9, # 0.6659421842117754
  38. "google/electra-small-discriminator": 11, # 0.6534639151385759
  39. "google/electra-base-generator": 10, # 0.6730033453857188
  40. "google/electra-base-discriminator": 9, # 0.7032089590812965
  41. "google/electra-large-generator": 18, # 0.6813370013104459
  42. "google/electra-large-discriminator": 14, # 0.6896675824733477
  43. "google/bert_uncased_L-2_H-128_A-2": 1, # 0.5887998733228855
  44. "google/bert_uncased_L-2_H-256_A-4": 1, # 0.6114863547661203
  45. "google/bert_uncased_L-2_H-512_A-8": 1, # 0.6177345529192847
  46. "google/bert_uncased_L-2_H-768_A-12": 2, # 0.6191261237956839
  47. "google/bert_uncased_L-4_H-128_A-2": 3, # 0.6076202863798991
  48. "google/bert_uncased_L-4_H-256_A-4": 3, # 0.6205239036810148
  49. "google/bert_uncased_L-4_H-512_A-8": 3, # 0.6375351621856903
  50. "google/bert_uncased_L-4_H-768_A-12": 3, # 0.6561849979644787
  51. "google/bert_uncased_L-6_H-128_A-2": 5, # 0.6200458425360283
  52. "google/bert_uncased_L-6_H-256_A-4": 5, # 0.6277501629539081
  53. "google/bert_uncased_L-6_H-512_A-8": 5, # 0.641952305130849
  54. "google/bert_uncased_L-6_H-768_A-12": 5, # 0.6762186226247106
  55. "google/bert_uncased_L-8_H-128_A-2": 7, # 0.6186876506711779
  56. "google/bert_uncased_L-8_H-256_A-4": 7, # 0.6447993208267708
  57. "google/bert_uncased_L-8_H-512_A-8": 6, # 0.6489729408169956
  58. "google/bert_uncased_L-8_H-768_A-12": 7, # 0.6705203359541737
  59. "google/bert_uncased_L-10_H-128_A-2": 8, # 0.6126762064125278
  60. "google/bert_uncased_L-10_H-256_A-4": 8, # 0.6376350032576573
  61. "google/bert_uncased_L-10_H-512_A-8": 9, # 0.6579006292799915
  62. "google/bert_uncased_L-10_H-768_A-12": 8, # 0.6861146692220176
  63. "google/bert_uncased_L-12_H-128_A-2": 10, # 0.6184105693383591
  64. "google/bert_uncased_L-12_H-256_A-4": 11, # 0.6374004994430261
  65. "google/bert_uncased_L-12_H-512_A-8": 10, # 0.65880012149526
  66. "google/bert_uncased_L-12_H-768_A-12": 9, # 0.675911357700092
  67. "amazon/bort": 0, # 0.41927911053036643
  68. "facebook/bart-base": 6, # 0.7122259132414092
  69. "facebook/bart-large": 10, # 0.7448671872459683
  70. "facebook/bart-large-cnn": 10, # 0.7393148105835096
  71. "facebook/bart-large-mnli": 11, # 0.7531665445691358
  72. "facebook/bart-large-xsum": 9, # 0.7496408866539556
  73. "t5-small": 6, # 0.6813843919496912
  74. "t5-base": 11, # 0.7096044814981418
  75. "t5-large": 23, # 0.7244153820191929
  76. "vinai/bertweet-base": 9, # 0.6529471006118857
  77. "microsoft/deberta-base": 9, # 0.7088459455930344
  78. "microsoft/deberta-base-mnli": 9, # 0.7395257063907247
  79. "microsoft/deberta-large": 16, # 0.7511806792052013
  80. "microsoft/deberta-large-mnli": 18, # 0.7736263649679905
  81. "microsoft/deberta-xlarge": 18, # 0.7568670944373346
  82. "microsoft/deberta-xlarge-mnli": 40, # 0.7780600929333213
  83. "YituTech/conv-bert-base": 10, # 0.7058253551080789
  84. "YituTech/conv-bert-small": 10, # 0.6544473011107349
  85. "YituTech/conv-bert-medium-small": 9, # 0.6590097075123257
  86. "microsoft/mpnet-base": 8, # 0.724976539498804
  87. "squeezebert/squeezebert-uncased": 9, # 0.6543868703018726
  88. "squeezebert/squeezebert-mnli": 9, # 0.6654799051284791
  89. "squeezebert/squeezebert-mnli-headless": 9, # 0.6654799051284791
  90. "tuner007/pegasus_paraphrase": 15, # 0.7188349436772694
  91. "google/pegasus-large": 8, # 0.63960462272448
  92. "google/pegasus-xsum": 11, # 0.6836878575233349
  93. "sshleifer/tiny-mbart": 2, # 0.028246072231946733
  94. "facebook/mbart-large-cc25": 12, # 0.6582922975802958
  95. "facebook/mbart-large-50": 12, # 0.6464972230103133
  96. "facebook/mbart-large-en-ro": 12, # 0.6791285137459857
  97. "facebook/mbart-large-50-many-to-many-mmt": 12, # 0.6904136529270892
  98. "facebook/mbart-large-50-one-to-many-mmt": 12, # 0.6847906439540236
  99. "allenai/led-base-16384": 6, # 0.7122259170564179
  100. "facebook/blenderbot_small-90M": 7, # 0.6489176335400088
  101. "facebook/blenderbot-400M-distill": 2, # 0.5874774070540008
  102. "microsoft/prophetnet-large-uncased": 4, # 0.586496184234925
  103. "microsoft/prophetnet-large-uncased-cnndm": 7, # 0.6478379437729287
  104. "SpanBERT/spanbert-base-cased": 8, # 0.6824006863686848
  105. "SpanBERT/spanbert-large-cased": 17, # 0.705352690855603
  106. "microsoft/xprophetnet-large-wiki100-cased": 7, # 0.5852499775879524
  107. "ProsusAI/finbert": 10, # 0.6923213940752796
  108. "Vamsi/T5_Paraphrase_Paws": 12, # 0.6941611753807352
  109. "ramsrigouthamg/t5_paraphraser": 11, # 0.7200917597031539
  110. "microsoft/deberta-v2-xlarge": 10, # 0.7393675784473045
  111. "microsoft/deberta-v2-xlarge-mnli": 17, # 0.7620620803716714
  112. "microsoft/deberta-v2-xxlarge": 21, # 0.7520547670281869
  113. "microsoft/deberta-v2-xxlarge-mnli": 22, # 0.7742603457742682
  114. "allenai/longformer-base-4096": 7, # 0.7089559593129316
  115. "allenai/longformer-large-4096": 14, # 0.732408493548181
  116. "allenai/longformer-large-4096-finetuned-triviaqa": 14, # 0.7365882744744722
  117. "zhiheng-huang/bert-base-uncased-embedding-relative-key": 4, # 0.5995636595368777
  118. "zhiheng-huang/bert-base-uncased-embedding-relative-key-query": 7, # 0.6303599452145718
  119. "zhiheng-huang/bert-large-uncased-whole-word-masking-embedding-relative-key-query": 19, # 0.6896878492850327
  120. "google/mt5-small": 8, # 0.6401166527273479
  121. "google/mt5-base": 11, # 0.5663956536597241
  122. "google/mt5-large": 19, # 0.6430931371732798
  123. "google/mt5-xl": 24, # 0.6707200963021145
  124. "google/bigbird-roberta-base": 10, # 0.6695606423502717
  125. "google/bigbird-roberta-large": 14, # 0.6755874042374509
  126. "google/bigbird-base-trivia-itc": 8, # 0.6930725491629892
  127. "princeton-nlp/unsup-simcse-bert-base-uncased": 10, # 0.6703066531921142
  128. "princeton-nlp/unsup-simcse-bert-large-uncased": 18, # 0.6958302800755326
  129. "princeton-nlp/unsup-simcse-roberta-base": 8, # 0.6436615893535319
  130. "princeton-nlp/unsup-simcse-roberta-large": 13, # 0.6812864385585965
  131. "princeton-nlp/sup-simcse-bert-base-uncased": 10, # 0.7068074935240984
  132. "princeton-nlp/sup-simcse-bert-large-uncased": 18, # 0.7111049471332378
  133. "princeton-nlp/sup-simcse-roberta-base": 10, # 0.7253123806661946
  134. "princeton-nlp/sup-simcse-roberta-large": 16, # 0.7497820277237173
  135. "dbmdz/bert-base-turkish-cased": 10, # WMT18 seg en-tr 0.5522827687776142
  136. "dbmdz/distilbert-base-turkish-cased": 4, # WMT18 seg en-tr 0.4742268041237113
  137. "google/byt5-small": 1, # 0.5100025975052146
  138. "google/byt5-base": 17, # 0.5810347173565313
  139. "google/byt5-large": 30, # 0.6151895697554877
  140. "microsoft/deberta-v3-xsmall": 10, # 0.6941803815412021
  141. "microsoft/deberta-v3-small": 4, # 0.6651551203179679
  142. "microsoft/deberta-v3-base": 9, # 0.7261586651018335
  143. "microsoft/mdeberta-v3-base": 10, # 0.6778713684091584
  144. "microsoft/deberta-v3-large": 12, # 0.6927693082293821
  145. "khalidalt/DeBERTa-v3-large-mnli": 18, # 0.7428756686018376
  146. }

允许一个句子和一个句子比较,或者一对多,多对多,举例来说:

  1. # 一对一
  2. cand=["I have an apple."]
  3. ref=["I have a pen."]
  4. P, R, F1 = bert_score.score(cand,ref, lang="en", verbose=True,model_type='bert-large-uncased') #tensor([0.8176]) tensor([0.8176]) tensor([0.8176])
  5. # 一对多
  6. # 那个数量多的要再套一层列表
  7. # 注意 这种写法会返回三个里面分数最高的,而不是把三个都返回
  8. cand=["I have an apple."]
  9. ref=[["I have a pen.","I have a doll"]]
  10. P, R, F1 = bert_score.score(cand,ref, lang="en", verbose=True,model_type='bert-large-uncased') #tensor([0.8176]) tensor([0.8176]) tensor([0.8176])
  11. # 多对多
  12. cand=["I have an apple.","I am Lucky."]
  13. ref=["I have a pen.","I am Lucy."]
  14. P, R, F1 = bert_score.score(cand,ref, lang="en", verbose=True,model_type='bert-large-uncased') #tensor([0.8176, 0.6489]) tensor([0.8176, 0.6489]) tensor([0.8176, 0.6489])

点关注!!

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

闽ICP备14008679号