我想要一天分享一點「LLM從底層堆疊的技術」,並且每篇文章長度控制在三分鐘以內,讓大家不會壓力太大,但是又能夠每天成長一點。
有了 AI說書 - 從0開始 - 261 | SHAP 數學計算 的數學,我們來撰寫實作程式:
import itertools
words = {'I': 0.2, 'love': 0.6, 'playing': 0.5, 'chess': 0.4, 'with': 0.1, 'my': 0.3, 'friends': 0.4}
bonus = {('I', 'love'): 0.3, ('love', 'playing'): 0.25, ('playing', 'chess'): 0.45, ('with', 'my', 'friends'): 0.35}
def total_score(coalition):
score = sum(words[word] for word in coalition)
for b in bonus.keys():
if all(word in coalition for word in b):
score += bonus[b]
return score
def shapley_value(word):
N = len(words)
permutations = list(itertools.permutations(words))
marginal_contributions = []
counter = 0
for permutation in permutations:
index = permutation.index(word)
coalition_without_word = permutation[:index]
coalition_with_word = permutation[:index+1]
marginal_contribution = total_score(coalition_with_word) - total_score(coalition_without_word)
marginal_contributions.append(marginal_contribution)
counter += 1
print(f"Processed {counter} permutations")
return sum(marginal_contributions)
for word in words:
print(f"The Shapley value of '{word}' is {shapley_value(word)}")