-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added attention layer to use all hidden states
- Loading branch information
Showing
4 changed files
with
31 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class AttentionLayer(nn.Module): | ||
def __init__(self, input_size): | ||
super(AttentionLayer, self).__init__() | ||
self.W = nn.Linear(input_size, input_size) | ||
self.v = nn.Linear(input_size, 1, bias=False) | ||
|
||
def forward(self, embeddings): | ||
# Apply linear transformation to the embeddings | ||
transformed = torch.tanh(self.W(embeddings)) | ||
|
||
# Calculate attention weights | ||
attention_weights = torch.softmax(self.v(transformed), dim=1) | ||
|
||
# Apply attention weights to the embeddings | ||
attended_embeddings = torch.sum(attention_weights * embeddings, dim=1) | ||
|
||
return attended_embeddings |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters