ViT 是 Vision Transformer(视觉 Transformer),把图像切成固定大小的图像块,即互不重叠的小方块,再把每块映射成一项数字表示,加入位置信息,送入 Transformer 编码器。2020 年的 ViT 论文表明,在足够规模的预训练下,这条路线可以取得很强的图像分类效果。
import math
q = 1# 位置 1“追”的查询
keys = [0, 1, 2] # 候选位置 0、1、2 的键
values = [10, 20, 30] # 对应位置的值
scores = [q * key for key in keys]
exp_scores = [math.exp(score) for score in scores]
total = sum(exp_scores)
weights = [score / total for score in exp_scores]
output = sum(weight * value for weight, value inzip(weights, values))
print([round(weight, 2) for weight in weights])
print(round(output, 2))
for position inrange(3):
print(position, list(range(position + 1)))