TransformerEncoder and TransformerDecoder based text encoder decoder

Bishwa_Karki: <p>I wanted to build text encoder-decoder based on nn.TransformerEncoder and nn.TransformerDecoder.<br>
Given sentence to encoder, I want to get a vector representation of size 270 and in the decoder I want to extract the given text using the vector representation.</p>
<pre><code class="lang-auto">class TransformerEncoder(nn.Module):
  def __init__(self, input_size, num_head, hidden_size, num_layers):

    super(TransformerEncoder, self).__init__()

    self.embd = nn.Embedding(word_count,input_size)
    encoder_layer = nn.TransformerEncoderLayer(input_size, num_head, hidden_size)
    self.transformer_enc = nn.TransformerEncoder(encoder_layer, num_layers)
    self.linear1 = nn.Linear(input_size,270)

def forward(self, x):
    x = x.long()
    emb = self.embd(x)
    mem = self.transformer_enc(emb)
    out = self.linear1(mem)
    return out, mem
</code></pre>
<pre><code class="lang-auto">class TransformerDecoder(nn.Module):
  def __init__(self, input_size, num_head, output_size, hidden_size, num_layers):
    super(TransformerDecoder, self).__init__()

    decoder_layer = nn.TransformerDecoderLayer(input_size, num_head, hidden_size)
    self.transformer_dec = nn.TransformerDecoder(decoder_layer, num_layers)
    self.linear1 = nn.Linear(input_size, output_size)

  def forward(self, x, mem):
    out = self.transformer_dec(x, mem)
    out = self.linear1(out)
    return out
</code></pre>
<p>I am wondering if my implementation is correct because I have not used <strong>positional encoding</strong> and <strong>masking</strong>.  Is it mandatory to use <strong>positional encoding</strong> and <strong>masking</strong> when using <strong>Transformer</strong>? Is my implementation correct?</p>

JuanFMontesinos: <p>Masking is task-dependant. It’s usually used in autoregressive transformers to speed up training.  If you just want to make an encoder-decoder, you don’t need it. However I doubt you are gonna obtain something useful. The trick of autoencoders is the bottleneck is a compressed representation of the input. In your case, your latent space is going to have as many elements as your input sentence, thus, it’s very unlikely it needs to compress the information.</p>
<p>wrt the positional encoder, yes.</p>
<p>Without positional encoding (from transformer’s point of view) it’s the same passing<br>
“I live in the south of Vietnam” as "Vietnam in live of I south the " or any other permutation. Basically the positional encoding is what allows the network to know the order of the sequence.</p>

Bishwa_Karki: <p>Hi <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a>  thank you for the answer.</p>
<p>“your latent space is going to have as many elements as your input sentence, thus, it’s very unlikely it needs to compress the information.” How can you infer this?</p>
<p>Is this because of this in the encoder?:</p>
<pre><code>mem = self.transformer_enc(emb)
out = self.linear1(mem)
</code></pre>
<p>If so how can I get the useful representations?</p>
<p>Also, I am passing final memory of encoder to the decoder which I think shouldn’t be the case in Auto-encoder, is there any alternative to this?</p>

JuanFMontesinos: <p>Transformers are sequence to sequence models.<br>
If your sequence is composed by 100 elements (vectors), your output will be composed of 100 elements as well.</p>
<p>I’m not super familiarized with NLP. Some options are checking the word2vec (embedding generators). They generate meaninful vectors laying on a manifold from words. Maybe you can extend that to sentences and get profit of the pretrained weights.</p>
<p>Other options are applying a temporal pooling to “mem” or some sort of postprocessing that reduces the dimensionality until you have a bottleneck. Then train an autoregressive decoder to infer the sentence from there.</p>