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

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

發(fā)布于 2025-2-3 13:24
瀏覽
0收藏

文本相似計算

介紹

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

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

摘要

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

規(guī)則

Find a metric on the Hub

本篇文章主要關注 Metric 方面的評估

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 這個包:

pip install transformers evaluate

眾多的自然語言處理評估方法會發(fā)布在 evaluate 這個包上。

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

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

  • 梯 子 開啟全局代理;
  • 嘗試把下述代理,加入到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 是一個嵌套的二維列表。

references 設計為二維列表的原因是,針對同一個問題,可能有多個回答,最終的結(jié)果是返回與多個結(jié)果計算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=["我愛你"], 
    references=[["我愛我的祖國"]]
)
# >>> {'google_bleu': 0.0}

上述 我愛你? 和 我愛我的祖國 如上述所示,google_bleu 不會原生支持漢字,原因在于英文可直接按照空格拆分開,但是漢語之間沒有空格。比如, ["我愛我的祖國"] 可拆分為:

  • ["我 愛 我 的 祖 國"] ,
  • ["我 愛 我 的 祖國"] , 祖國中間沒有空格分開

顯然 祖國? 作為一個詞更好,若拆分為 祖和國 兩個字則會丟失原來的語義信息。

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

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

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

向量

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

下述是一個簡單的例子:

!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

# 定義余弦相似度計算函數(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 = ['今天天氣怎么樣?', '我今天很高興', '天氣預報說今天會下雨', '你最喜歡的顏色是什么?']

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

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

# 計算相似度并排序
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}")

上面代碼展示了,計算 input_string 與 candidates 候選字符串之間的向量余弦相似度分數(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? - 天氣預報說今天會下雨), Similarity: 0.5470
(How is the weather today? - 我今天很高興), Similarity: 0.4202
(How is the weather today? - 你最喜歡的顏色是什么?), Similarity: 0.1032

大模型裁判

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

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

提示詞參考:

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 作為評判者?????進行自動化和多方面的評估
  2. https://github.com/huggingface/evaluate 

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

收藏
回復
舉報
回復
相關推薦