NStepLSTM和Seq2Seq mod

2024-05-08 17:03:12 发布

您现在位置:Python中文网/ 问答频道 /正文

亲爱的chainer社区

我对seq2seqofficial example(英法翻译)中NStepLSTM的逻辑用法感到费解。在

  1. 据我所知,def __call__(self, xs, ys): xs = [x[::-1] for x in xs] #Reverse x是英语短语,ys是法语短语。你为什么把英语短语颠倒过来?

  2. 如何训练网络?将xs和{}嵌入连续空间,然后向编码器提供{}以获得英语短语的潜在表示。但是,然后用eys将潜在表示放入解码器中。但是eys是法语短语的连续表示,在测试阶段解码器无法知道生成的法语短语,对吗?你如何运用你的人际网络?在

    hx, cx, _ = self.the encoder(None, None, exs) _, _, os = self.decoder(hx, cx, eys)

  3. ys_in = [F.concat([eos, y], axis=0) for y in ys]为什么我们把end of sequence放在开头?

  4. ys = self.xp.full(batch, EOS, 'i')def translate中,我们把end of sequence的数组放入解码器,为什么?在

如果我不想翻译句子,而是想建立一个自动编码器,将短语映射到潜在空间,我该怎么办?在


Tags: inself网络nonefordef空间编码器
1条回答
网友
1楼 · 发布于 2024-05-08 17:03:12

谢谢你的问题。在

Question 1

请参阅以下seq2seq原稿。 他们建议: Note that the LSTM reads the input sentence in reverse, because doing so introduces many short term dependencies in the data that make the optimization problem much easier.

(摘要) we found that reversing the order of the words in all source sentences (but not target sentences) improved the LSTM’s performance markedly https://papers.nips.cc/paper/5346-sequence-to-sequence-learning-with-neural-networks.pdf

我认为官方的示例代码也与上面的文章中的输入语句相反。在

Question 2

But then you put the latent representation into decoder with eys. But eys is a continuos representation of the French phrase

在是的。这个代码是在训练时间使用的,所以我们知道目标句子(黄金单词)。在

hx, cx, _ = self.the encoder(None, None, exs)
_, _, os = self.decoder(hx, cx, eys)

在测试时,您应该使用def translate(self, xs, max_length=100):。 该方法可用于从源句xs预测句子。在

^{pr2}$

对于每个循环,用源句子向量和前一个单词ys预测一个单词。在

Question 3 Question 4

我认为这一部分应该是: ys_in = [F.concat([bos, y], axis=0) for y in ys](句首) 官方代码对和都使用eos。在

Final Question

What should I do if I don't want to translate sentences but to build an autoencoder for mapping phrases into the latent space?

当你想建立一个自动编码器

  1. 删除此行xs = [x[::-1] for x in xs]
  2. ys_in = [F.concat([eos, y], axis=0) for y in ys]中使用bos,而不是eos

是否使用eos而不是bos,两者都很好。 您只需删除自动编码器的这行xs = [x[::-1] for x in xs]。在

如果要使用bos,则应修改如下:

UNK = 0
EOS = 1
BOS = 2



47:eos = self.xp.array([EOS], 'i')
48: ys_in = [F.concat([eos, y], axis=0) for y in ys]
=>
bos = self.xp.array([BOS], 'i')
ys_in = [F.concat([bos, y], axis=0) for y in ys]
79: ys = self.xp.full(batch, EOS, 'i')
=> 
ys = self.xp.full(batch, BOS, 'i')

def load_vocabulary(path):
    with open(path) as f:
        # +2 for UNK and EOS
        word_ids = {line.strip(): i + 3 for i, line in enumerate(f)}
    word_ids['<UNK>'] = 0
    word_ids['<EOS>'] = 1
    word_ids['<BOS>'] = 2
    return word_ids

如果你还有其他问题,请再问我一次。在

相关问题 更多 >

    热门问题