1

我想实现一个 GRU 能够将向量序列编码为一个向量(多对一),然后另一个 GRU 能够将向量解码为向量序列(一对多)。向量的大小不会改变。我想对我实施的内容发表意见。

这是代码:

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

我不确定这是否是进行一对多 GRU 的好方法。我可以对此发表一些意见吗?

谢谢阅读!

4

0 回答 0