自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三種文本相似計(jì)算方法:規(guī)則、向量與大模型裁判

發(fā)布于 2025-2-11 12:48
瀏覽
0收藏

文本相似計(jì)算

介紹

有一些工作需要評(píng)估出兩個(gè)字符串之間的相似程度。比如,要評(píng)估大模型生成的結(jié)果,與預(yù)設(shè)定的答案之間的相似程度。本文介紹三類方法用于評(píng)估兩個(gè)字符串的相似程度:規(guī)則、向量、大模型裁判。

  • 規(guī)則:基于字符 n-gram 的相似計(jì)算,常用算法,ROUGE、BLEU;
  • 向量:使用熱門的嵌入模型(Jina),把字符串編碼為向量,計(jì)算兩個(gè)向量之間的相似度;
  • 大模型裁判:使用大模型評(píng)估兩個(gè)字符串之間的相關(guān)性;

摘要

介紹了三種方法,評(píng)估兩個(gè)字符串之間的相似度:基于字符 n-gram 的規(guī)則算法,通過(guò)嵌入模型將文本編碼為向量并計(jì)算余弦相似度,以及使用大模型直接評(píng)判文本相關(guān)性。文章詳細(xì)探討了這些方法的實(shí)現(xiàn)細(xì)節(jié)及適用場(chǎng)景,并提供了 Python 示例代碼,幫助讀者理解和應(yīng)用不同的方法來(lái)滿足具體需求。

規(guī)則

Find a metric on the Hub

本篇文章主要關(guān)注 Metric 方面的評(píng)估

Metric: measures the performance of a model on a given dataset, usually by comparing the model's predictions to some ground truth labels -- these are covered in this space.

裝包,主要依賴 nltk 這個(gè)包:

pip install transformers evaluate

眾多的自然語(yǔ)言處理評(píng)估方法會(huì)發(fā)布在 evaluate 這個(gè)包上。

google_bleu 網(wǎng)頁(yè),若想瀏覽更多的例子請(qǐng)點(diǎn)擊查看,??https://huggingface.co/spaces/evaluate-metric/google_bleu??

三種文本相似計(jì)算方法:規(guī)則、向量與大模型裁判-AI.x社區(qū)

從 ??evaluate?? 加載工具的時(shí)候,需要科 學(xué) 上 網(wǎng),解決方案如下:

  • 梯 子 開(kāi)啟全局代理;
  • 嘗試把下述代理,加入到python代碼,7890是clash的端口

import os
  os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
  os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'


import evaluate
google_bleu = evaluate.load("google_bleu")

sentence1 = "the cat sat on the mat"
sentence2 = "the cat ate the mat"
result1 = google_bleu.compute(predictinotallow=[sentence1], references=[[sentence2]])
print(result1)
# result1 {'google_bleu': 0.3333333333333333}


result2 = google_bleu.compute(predictinotallow=[sentence1], references=[[sentence1]])
print(result2)
# result2 {'google_bleu': 1.0}

【注意】:references 是一個(gè)嵌套的二維列表。

references 設(shè)計(jì)為二維列表的原因是,針對(duì)同一個(gè)問(wèn)題,可能有多個(gè)回答,最終的結(jié)果是返回與多個(gè)結(jié)果計(jì)算google_bleu的最大值。

predictions = ["The cat is on the mat."]
references = [["The cat is on the mat.", "There is a cat on the mat."]]
print(google_bleu.compute(predictinotallow=predictions, references=references))
>>> {'google_bleu': 1.0}

下述是中文的例子:

google_bleu.compute(
    predictinotallow=["我愛(ài)你"], 
    references=[["我愛(ài)我的祖國(guó)"]]
)
# >>> {'google_bleu': 0.0}

上述 ??我愛(ài)你??? 和 ??我愛(ài)我的祖國(guó)?? 如上述所示,google_bleu 不會(huì)原生支持漢字,原因在于英文可直接按照空格拆分開(kāi),但是漢語(yǔ)之間沒(méi)有空格。比如, ["我愛(ài)我的祖國(guó)"] 可拆分為:

  • ["我 愛(ài) 我 的 祖 國(guó)"] ,
  • ["我 愛(ài) 我 的 祖國(guó)"] , 祖國(guó)中間沒(méi)有空格分開(kāi)

顯然 ??祖國(guó)??? 作為一個(gè)詞更好,若拆分為 ??祖和國(guó)?? 兩個(gè)字則會(huì)丟失原來(lái)的語(yǔ)義信息。

google_bleu.compute(
    predictinotallow=["我 愛(ài) 你"], 
    references=[["我 愛(ài) 我 的 祖 國(guó)"]]
)
# >>> {'google_bleu': 0.16666666666666666}

google_bleu.compute(
    predictinotallow=["我 愛(ài) 你"], 
    references=[["我 愛(ài) 我 的 祖國(guó)"]]
)
# >>> {'google_bleu': 0.21428571428571427}

使用合適的中文分詞技術(shù),可提高 google_bleu 分?jǐn)?shù)。如上所示,??祖國(guó)??? 變成一個(gè)詞后,google_bleu 從0.16 提高到 0.21。如果想嘗試中文分詞技術(shù),可嘗試使用??pip install jieba??,支持添加新詞到字典中。

向量

使用經(jīng)過(guò)訓(xùn)練的嵌入模型,把文本編碼為向量,再計(jì)算兩個(gè)向量的余弦相似度。瀏覽 jina-embeddings-v2-base-zh 的介紹, https://modelscope.cn/models/jinaai/jina-embeddings-v2-base-zh

下述是一個(gè)簡(jiǎn)單的例子:

!pip install modelscope
from modelscope import AutoModel
from numpy.linalg import norm

cos_sim = lambda a,b: (a @ b.T) / (norm(a)*norm(b))
# trust_remote_code is needed to use the encode method
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True) 
embeddings = model.encode(['How is the weather today?', '今天天氣怎么樣?'])
print(cos_sim(embeddings[0], embeddings[1]))

import numpy as np
from numpy.linalg import norm
from modelscope import AutoModel

# 定義余弦相似度計(jì)算函數(shù)
cos_sim = lambda a, b: (a @ b.T) / (norm(a) * norm(b))


# 加載模型
model = AutoModel.from_pretrained('jinaai/jina-embeddings-v2-base-zh', trust_remote_code=True)

# 輸入字符串和候選字符串
input_string = 'How is the weather today?'
candidates = ['今天天氣怎么樣?', '我今天很高興', '天氣預(yù)報(bào)說(shuō)今天會(huì)下雨', '你最喜歡的顏色是什么?']

# 計(jì)算輸入字符串的嵌入向量
input_embedding = model.encode([input_string])[0]

# 計(jì)算候選字符串的嵌入向量
candidate_embeddings = model.encode(candidates)

# 計(jì)算相似度并排序
similarities = [cos_sim(input_embedding, candidate_embedding) for candidate_embedding in candidate_embeddings]
sorted_candidates = sorted(zip(candidates, similarities), key=lambda x: x[1], reverse=True)

# 輸出排序結(jié)果
for candidate, similarity in sorted_candidates:
    print(f"({input_string} - {candidate}), Similarity: {similarity:.4f}")

上面代碼展示了,計(jì)算 input_string 與 candidates 候選字符串之間的向量余弦相似度分?jǐn)?shù),按照從高到低排序:

Downloading Model to directory: C:\Users\user_name\.cache\modelscope\hub\jinaai/jina-embeddings-v2-base-zh
(How is the weather today? - 今天天氣怎么樣?), Similarity: 0.7861
(How is the weather today? - 天氣預(yù)報(bào)說(shuō)今天會(huì)下雨), Similarity: 0.5470
(How is the weather today? - 我今天很高興), Similarity: 0.4202
(How is the weather today? - 你最喜歡的顏色是什么?), Similarity: 0.1032

大模型裁判

制定一個(gè)基于規(guī)則的程序來(lái)評(píng)估輸出是非常具有挑戰(zhàn)性的。傳統(tǒng)的評(píng)估指標(biāo),基于輸出和參考答案之間的相似性(例如,ROUGE、BLEU;),對(duì)于這些問(wèn)題也無(wú)效。[1] 在復(fù)雜場(chǎng)景下,可嘗試使用大模型進(jìn)行判決。

主要針對(duì)復(fù)雜的場(chǎng)景,在基于規(guī)則與向量相似度均效果不顯著的情況下,可嘗試使用LLM進(jìn)行判決。

提示詞參考:

JUDGE_PROMPT = """
You will be given a user_question and system_answer couple.
Your task is to provide a 'total rating' scoring how well the system_answer answers the user concerns expressed in the user_question.
Give your answer as a float on a scale of 0 to 10, where 0 means that the system_answer is not helpful at all, and 10 means that the answer completely and helpfully addresses the question.

Provide your feedback as follows:

Feedback:::
Total rating: (your rating, as a float between 0 and 10)

Now here are the question and answer.

Question: {question}
Answer: {answer}

Feedback:::
Total rating: """

參考資料

  1. 使用 LLM 作為評(píng)判者?????進(jìn)行自動(dòng)化和多方面的評(píng)估
  2. ??https://github.com/huggingface/evaluate ??

本文轉(zhuǎn)載自??AI悠閑區(qū)??,作者: jieshenai ????

收藏
回復(fù)
舉報(bào)
回復(fù)
相關(guān)推薦