Lecture 03活动 A:一张小矩阵,两种查找方向 · 实现代码
活动 A:一张小矩阵,两种查找方向 · 实现代码
主线阅读与优先实验
caption历史、VQA、CLIP候选、关系控制、GLIP/LLaVA与生成桥梁
展开完整实现代码
# 独立 CPU 的双向配对目标;余弦来自归一化教学向量。
import math
objects = ['redcup', 'bluecup', 'book']
image_vectors = [[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]]
text_weights = [[4.0,1.0,1.0],[1.0,4.0,1.0],[1.0,1.0,4.0]]
def normalize(vector):
norm=math.sqrt(sum(x*x for x in vector))
return [x/norm for x in vector]
text_vectors=[normalize(vector) for vector in text_weights]
S=[[sum(a*b for a,b in zip(u,v)) for v in text_vectors] for u in image_vectors]
temperature=1.0
logits=[[s/temperature for s in row] for row in S]
def softmax(row):
exps=[math.exp(x-max(row)) for x in row]
return [x/sum(exps) for x in exps]
row_prob=[softmax(row) for row in logits]
col_prob=[softmax(list(col)) for col in zip(*logits)]
row_loss=-sum(math.log(row_prob[i][i]) for i in range(3))/3
col_loss=-sum(math.log(col_prob[i][i]) for i in range(3))/3
print('对象顺序:',objects,'温度:',temperature)
print('余弦矩阵:',[[round(s,6) for s in row] for row in S])
print('每张图找正确文字:',[round(row_prob[i][i],4) for i in range(3)])
print('每段文字找正确图:',[round(col_prob[i][i],4) for i in range(3)])
print('双向平均损失:',round((row_loss+col_loss)/2,6))
assert all(abs(sum(p)-1)<1e-12 for p in row_prob+col_prob)
assert all(abs(sum(x*x for x in vector)-1)<1e-12 for vector in image_vectors+text_vectors)
if text_weights==[[4.0,1.0,1.0],[1.0,4.0,1.0],[1.0,1.0,4.0]] and temperature==1.0:
assert all(math.isclose(S[i][j],(4 if i==j else 1)/math.sqrt(18)) for i in range(3) for j in range(3))
assert math.isclose((row_loss+col_loss)/2,0.6861917388550698)