Spaces:
Sleeping
Sleeping
Download app/decoder.py from Rohit2sali/translationmodel: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/Rohit2sali/translationmodel/resolve/main/app/decoder.py
- Command line
-
hf download hf://spaces/Rohit2sali/translationmodel/app/decoder.py
-
curl -L -o decoder.py https://huggingface.co/spaces/Rohit2sali/translationmodel/resolve/main/app/decoder.py
1.46 kB
| import torch.nn as nn | |
| from Attention import MultiHeadAttention | |
| class Decoder(nn.Module): | |
| def __init__(self, d_model, n_heads, fnn_hidden_dim): | |
| super(Decoder, self).__init__() | |
| self.d_model = d_model | |
| self.n_heads = n_heads | |
| self.maskedattention = MultiHeadAttention(d_model) | |
| self.cross_attention = MultiHeadAttention(d_model) | |
| self.fnn = nn.Sequential( | |
| nn.Linear(d_model, fnn_hidden_dim), | |
| nn.ReLU(), | |
| nn.Dropout(0.1), | |
| nn.Linear(fnn_hidden_dim, d_model) | |
| ) | |
| self.layernorm1 = nn.LayerNorm(d_model) | |
| self.layernorm2 = nn.LayerNorm(d_model) | |
| self.layernorm3 = nn.LayerNorm(d_model) | |
| def forward(self, embedding_output, key, decoder_padding_mask, encoder_padding_mask, lookahead_mask): | |
| x = self.layernorm1(embedding_output) | |
| maskedattention_output = self.maskedattention(x, x, x, decoder_padding_mask, lookahead_mask=lookahead_mask, n_heads=self.n_heads) | |
| attention_output = maskedattention_output + embedding_output | |
| query = self.layernorm2(attention_output) | |
| cross_attention_output = self.cross_attention(query, key, key, encoder_padding_mask, lookahead_mask=None, n_heads=self.n_heads) | |
| attention_output = cross_attention_output + attention_output | |
| output = self.fnn(self.layernorm3(attention_output)) | |
| output = output + attention_output | |
| return output | |