| import numpy as np | |
| def encoder_layer(Q, K_transpose, V, Wq, Wk, Wv): | |
| Q_proj = np.dot(Q, Wq.T) | |
| K_proj = np.dot(K_transpose.T, Wk.T) | |
| V_proj = np.dot(V, Wv.T) | |
| attention_scores = scaled_dot_product_attention_2D(Q_proj, K_proj, V_proj) | |
| return attention_scores | |
| def calcular_pesos_proyeccion(attention_matrix): | |
| num_letras = attention_matrix.shape[0] | |
| Wq = np.zeros((num_letras, attention_matrix.shape[1])) | |
| Wk = np.zeros((num_letras, attention_matrix.shape[1])) | |
| Wv = np.zeros((num_letras, attention_matrix.shape[1])) | |
| for i in range(num_letras): | |
| Wq[i, i] = 1 | |
| Wk[i, i] = 1 | |
| Wv[i, i] = 1 | |
| return Wq, Wk, Wv | |
| def scaled_dot_product_attention_2D(Q, K_transpose, V): | |
| out = np.zeros((Q.shape[0], V.shape[1])) | |
| for i in range(Q.shape[0]): | |
| scores = np.dot(Q[i, :], K_transpose) | |
| softmax_scores = np.exp(scores - np.max(scores)) / np.sum(np.exp(scores - np.max(scores))) | |
| out[i, :] = np.dot(softmax_scores, V) | |
| return out | |