Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

hybrid: enable no bottleneck #556

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 12 additions & 5 deletions pytorch_translate/hybrid_transformer_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def add_args(parser):
)
parser.add_argument(
"--decoder-out-embed-dim",
default=None,
type=int,
metavar="N",
help="decoder output embedding dimension",
Expand Down Expand Up @@ -220,7 +221,7 @@ def _init_dims(self, args, src_dict, dst_dict, embed_tokens):
self.input_dim = self.lstm_units + self.attention_dim

self.num_attention_heads = args.decoder_attention_heads
self.out_embed_dim = args.decoder_out_embed_dim
self.bottleneck_dim = args.decoder_out_embed_dim

def _init_components(self, args, src_dict, dst_dict, embed_tokens):
self.initial_rnn_layer = nn.LSTM(
Expand Down Expand Up @@ -249,9 +250,14 @@ def _init_components(self, args, src_dict, dst_dict, embed_tokens):
nn.LSTM(input_size=self.input_dim, hidden_size=self.lstm_units)
)

self.bottleneck_layer = fairseq_transformer.Linear(
self.input_dim, self.out_embed_dim
)
self.bottleneck_layer = None
if self.bottleneck_dim is not None:
self.out_embed_dim = self.bottleneck_dim
self.bottleneck_layer = fairseq_transformer.Linear(
self.input_dim, self.out_embed_dim
)
else:
self.out_embed_dim = self.input_dim

self.embed_out = nn.Parameter(torch.Tensor(len(dst_dict), self.out_embed_dim))
nn.init.normal_(self.embed_out, mean=0, std=self.out_embed_dim ** -0.5)
Expand Down Expand Up @@ -378,7 +384,8 @@ def forward(

x = torch.cat([x, attention_out], dim=2)
x = self._concat_latent_code(x, encoder_out)
x = self.bottleneck_layer(x)
if self.bottleneck_layer is not None:
x = self.bottleneck_layer(x)

# T x B x C -> B x T x C
x = x.transpose(0, 1)
Expand Down