From 59d829cad0654743514a462b781080161baf99ea Mon Sep 17 00:00:00 2001 From: k-gorbenko <164647543+k-gorbenko@users.noreply.github.com> Date: Wed, 4 Dec 2024 22:31:04 +0300 Subject: [PATCH] Update template_p09.py --- Deep Learning/template_p01.py | 40 ++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/Deep Learning/template_p01.py b/Deep Learning/template_p01.py index 190fb4e..055d0f3 100644 --- a/Deep Learning/template_p01.py +++ b/Deep Learning/template_p01.py @@ -18,13 +18,23 @@ def multiplicative_attention(decoder_hidden_state, encoder_hidden_states, W_mult decoder_hidden_state: np.array of shape (n_features_dec, 1) encoder_hidden_states: np.array of shape (n_features_enc, n_states) W_mult: np.array of shape (n_features_dec, n_features_enc) - + return: np.array of shape (n_features_enc, 1) Final attention vector ''' - # your code here - - return attention_vector + # Умножаем матрицу весов на вектор скрытого состояния декодера + attention_scores = W_mult @ decoder_hidden_state # (n_features_enc, 1) + + # Умножаем полученные оценки внимания на скрытые состояния кодера + attention_weights = np.dot(encoder_hidden_states.T, attention_scores) # (n_states, 1) + + # Применяем softmax к вектору внимания + attention_weights = np.exp(attention_weights) / np.sum(np.exp(attention_weights), axis=0) + + # Вычисляем взвешенную сумму скрытых состояний кодера + attention_vector = np.dot(encoder_hidden_states, attention_weights) # (n_features_enc, 1) + + return attention_vector # Возвращаем вектор в виде (n_features_enc, 1) def additive_attention(decoder_hidden_state, encoder_hidden_states, v_add, W_add_enc, W_add_dec): ''' @@ -33,10 +43,24 @@ def additive_attention(decoder_hidden_state, encoder_hidden_states, v_add, W_add v_add: np.array of shape (n_features_int, 1) W_add_enc: np.array of shape (n_features_int, n_features_enc) W_add_dec: np.array of shape (n_features_int, n_features_dec) - + return: np.array of shape (n_features_enc, 1) Final attention vector ''' - # your code here - - return attention_vector + # Применяем линейные преобразования к скрытым состояниям кодера и декодера + encoder_transformed = W_add_enc @ encoder_hidden_states # (n_features_int, n_states) + decoder_transformed = W_add_dec @ decoder_hidden_state # (n_features_int, 1) + + # Суммируем результаты + scores = v_add @ (encoder_transformed + decoder_transformed.T) # (1, n_states) + + # Применяем активацию tanh + scores = np.tanh(scores) # (1, n_states) + + # Применяем softmax к оценкам внимания + attention_weights = np.exp(scores) / np.sum(np.exp(scores), axis=1, keepdims=True) # (1, n_states) + + # Вычисляем взвешенную сумму скрытых состояний кодера + attention_vector = encoder_hidden_states @ attention_weights.T # (n_features_enc, 1) + + return attention_vector # Возвращаем вектор в виде (n_features_enc, 1)