Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Deep Learning/template_p01.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ def multiplicative_attention(decoder_hidden_state, encoder_hidden_states, W_mult
return: np.array of shape (n_features_enc, 1)
Final attention vector
'''
# your code here

# Считаем промежуточное произведение W_mult * h_i
transformed_encoder = W_mult.dot(encoder_hidden_states) # shape: (n_features_dec, n_states)
# Скаляное произведение с состоянием декодера
attention_scores = decoder_hidden_state.T.dot(transformed_encoder) # shape: (1, n_states)
# Применяем softmax для получения весов
attention_weights = softmax(attention_scores)
# Итоговый attention vector
attention_vector = attention_weights.dot(encoder_hidden_states.T).T # shape: (n_features_enc, 1)
return attention_vector

def additive_attention(decoder_hidden_state, encoder_hidden_states, v_add, W_add_enc, W_add_dec):
Expand All @@ -37,6 +43,20 @@ def additive_attention(decoder_hidden_state, encoder_hidden_states, v_add, W_add
return: np.array of shape (n_features_enc, 1)
Final attention vector
'''
# your code here
n_states = encoder_hidden_states.shape[1]
attention_scores = np.zeros((1, n_states))

# Вычисляем e_i для каждого состояния энкодера
for i in range(n_states):
h_i = encoder_hidden_states[:, i][:, None] # вектор состояния i
e_i = np.dot(v_add.T, np.tanh(np.dot(W_add_enc, h_i) + np.dot(W_add_dec, decoder_hidden_state)))
attention_scores[0, i] = e_i

# Применяем softmax для получения весов
attention_weights = softmax(attention_scores)

# Итоговый attention vector: взвешенная сумма состояний энкодера
attention_vector = attention_weights.dot(encoder_hidden_states.T).T

return attention_vector