1

I would like to implement a GRU able to encode a sequence of vectors to one vector (many-to-one), and then another GRU able to decode a vector to a sequence of vector (one-to-many). The size of the vectors wouldn't be changed. I would like to have an opinion about what I implemented.

Here is the code:

class AEGRU(nn.Module):
    def __init__(self, opt):
        super(AEGRU, self).__init__()

        self.length = 256

        self.latent_space = 256

        self.num_layers = 1

        self.GRU_enc = nn.GRU(input_size=3, hidden_size=self.latent_space, num_layers=self.num_layers, batch_first=True)
        self.fc_enc = nn.Linear(self.latent_space, self.latent_space)

        self.GRU_dec = nn.GRU(input_size=self.latent_space, hidden_size=3, num_layers=self.num_layers, batch_first=True)
        self.fc_dec = nn.Linear(3, 3)

    def enc(self, x):
        # x has shape: Batch_size x self.length x 3

        h0 = torch.zeros(self.num_layers, x.shape[0], self.latent_space).cuda()

        out, _ = self.GRU_enc(x, h0)

        out = out[:, -1, :]

        out = self.fc_enc(out)

        return out

    def dec(self, x):
        # x has shape: Batch_size x self.latent_space
        x = x[:, None, :]

        h = torch.zeros(self.num_layers, x.shape[0], 3).cuda()


        # method 1 ??
        '''outputs = torch.zeros(x.shape[0], self.length, 3).cuda()

        for i in range(self.length):
            out, h = self.GRU_dec(x, h)
            outputs[:, i, :] = out[:, 0, :]'''
        
        # method 2 ??
        x = x.repeat(1, self.length, 1)

        outputs, _ = self.GRU_dec(x, h)
        


        # linear layer
        outputs = self.fc_dec(outputs)

        return outputs

    def forward(self, x):
        self.indices = []

        latent = self.enc(x)

        output = self.dec(latent)

        return output

I am not sure whether this is the good way to do a one-to-many GRU. Could I have some opinions about this?

Thanks for reading!

papyrus
  • 33
  • 5

0 Answers0