How to pass context vector of Encorder to decorder without using attention

Hi
I am going through several tutorials about seq2seq in pytorch but most of them are with using attention. I want to create simple seq2seq model.

I just need to know how to pass context vector into decorder.
Working on english to French dataset.

Below is my code:

class Encorder(nn.Module):
def init(self,n_vocab,n_embed,hidden_size):

    super(Encorder,self).__init__()
    
    self.hidden_size = hidden_size
    self.embed = nn.Embedding(n_vocab+1,n_embed)
    self.RNN = nn.RNN(n_embed,hidden_size,batch_first = True)

def forward(self,x):
    x = self.embed(x)
    x,hidden = self.RNN(x)
    assert x[:,-1,:].shape == hidden.squeeze(0).shape
    return x[:,-1,:]

def initHidden(self):
    return torch.zeros(1, 1, self.hidden_size, device=device)

For Decorder

class decorder(nn.Module):
def init(self,n_vocab,n_embed,hidden_size,output_size):
super(decorder,self).init()

    self.hidden_size = hidden_size
    self.embed = nn.Embedding(n_vocab+1,n_embed)
    self.RNN = nn.RNN(n_embed,hidden_size,batch_first = True)
    self.fc1 = nn.Linear(hidden_size,output_size)
    
    
def forward(self,x,hidden):
    #x = x.unsqueeze(0)
    x = self.embed(x)
    x,hidden = self.RNN(x,hidden)
    x = self.fc1(x)
    
    return x
def initHidden(self):
    return torch.zeros(1, 1, self.hidden_size, device=device)

Please let me know if some one need more info. I just want to make basic seq2seq (without attention)

Thanks in advance.
Stay safe. !!

Without attention, the context vector is usually just the last hidden state – the most basic encoder-decoder setup. And you’re already return the last hidden state in your forward() method of your encoder. Your current code is working, right?

I just need to find out how to put that last context vector into the initial state of Decorder.

Example::

n_vocab = 3160
n_embed = 16
hidden_size = 16
model = Encorder(n_vocab,n_embed,hidden_size)
model(input_[0:3])

Results are :
tensor([[ 0.2726, -0.5882, -0.9438, -0.8380, 0.2866, 0.7537, 0.8387, -0.9315,
-0.3624, -0.6164, 0.6472, 0.3157, -0.9453, 0.6535, 0.5850, -0.7729],
[-0.1652, -0.9067, -0.7611, 0.2591, 0.0118, -0.1394, 0.8526, -0.1523,
-0.7598, -0.3685, -0.5124, -0.3204, 0.2357, 0.2204, 0.5002, -0.6450],
[-0.1652, -0.9067, -0.7611, 0.2591, 0.0118, -0.1394, 0.8526, -0.1523,
-0.7598, -0.3685, -0.5124, -0.3204, 0.2357, 0.2204, 0.5002, -0.6450]],
grad_fn=)

Now I got 3 hidden state for 3 sentences Now how to send those state into initial_state of decorder part/

Isn’t the basic Seq2Seq tutorial what you’re looking for; before they introduce the Attention Decoder?

1 Like

Yeah thats the same thing I am looking for but all the tutorials I got are just related to attention mechanis