
# 这里我们随机设置三个输入, 每个输入的维度是一个4维向量 import torch x = [ [1, 0, 1, 0], # Input 1 [0, 2, 0, 2], # Input 2 [1, 1, 1, 1] # Input 3 ] x = torch.tensor(x, dtype=torch.float32)
`# 每一个输入都有三个表示,分别为key(橙黄色)query(红色)value(紫色)。比如说,每一个表示我们希望是一个3维的向量。由于输入是4维,所以我们的参数矩阵为 4*3 维。
w_key = [ [0, 0, 1], [1, 1, 0], [0, 1, 0], [1, 1, 0] ] w_query = [ [1, 0, 1], [1, 0, 0], [0, 0, 1], [0, 1, 1] ] w_value = [ [0, 2, 0], [0, 3, 0], [1, 0, 3], [1, 1, 0] ] w_key = torch.tensor(w_key, dtype=torch.float32) w_query = torch.tensor(w_query, dtype=torch.float32) w_value = torch.tensor(w_value, dtype=torch.float32)
print("w_key: \n", w_key) print("w_query: \n", w_query) print("w_value: \n", w_value)`
w_key: tensor([[0., 0., 1.], [1., 1., 0.], [0., 1., 0.], [1., 1., 0.]]) w_query: tensor([[1., 0., 1.], [1., 0., 0.], [0., 0., 1.], [0., 1., 1.]]) w_value: tensor([[0., 2., 0.], [0., 3., 0.], [1., 0., 3.], [1., 1., 0.]])
使用向量化获取keys的值
`[0, 0, 1]
[1, 0, 1, 0] [1, 1, 0] [0, 1, 1] [0, 2, 0, 2] x [0, 1, 0] = [4, 4, 0] [1, 1, 1, 1] [1, 1, 0] [2, 3, 1]`

使用向量化获取values的值
`[0, 2, 0]
[1, 0, 1, 0] [0, 3, 0] [1, 2, 3] [0, 2, 0, 2] x [1, 0, 3] = [2, 8, 0] [1, 1, 1, 1] [1, 1, 0] [2, 6, 3]`