繁体   English   中英

在seq2seq中对编码器和解码器使用相同的嵌入

[英]Using the same Embedding for Encoder and Decoder in seq2seq

我正在基于seq2seq-class构建翻译机。 该类为编码器和解码器部分假定不同的词汇表。 因此,它也期望两者的嵌入不同。

但是,我试图在一种语言中使用它。 因此,我希望两个嵌入是一个。 (背景是将外行用语翻译成专家用相同语言表达的用语)

当前相关代码为:

编码器端:在EmbeddingWrapper()中的python / ops / rnn_cell.py中:

with vs.variable_scope(scope or "EmbeddingWrapper"):
        additional_info_size   with vs.variable_scope(scope or type(self).__name__):
      with ops.device("/cpu:0"):
        embedding = vs.get_variable("embedding", [self._embedding_classes, self._embedding_size], initializer=initializer)
        embedded = embedding_ops.embedding_lookup(embedding, array_ops.reshape(inputs, [-1]))

解码器端:在python / ops / seq2seq.py中的embedding_rnn_decoder()中:

  with variable_scope.variable_scope(scope or "embedding_rnn_decoder"):
    with ops.device("/cpu:0"):
      embedding = variable_scope.get_variable("embedding", [num_symbols, embedding_size])
    loop_function = _extract_sksk_argmax_and_embed(
        embedding, output_projection,
        update_embedding_for_previous) if feed_previous else None
    emb_inp = (embedding_ops.embedding_lookup(embedding, i) for i in decoder_inputs)

任何想法如何优雅地让这两个使用相同的嵌入矩阵?

调用创建第二个嵌入的函数时,可以使用重用变量作用域。 如果使用具有相同名称的合并范围,并且设置了reuse = True,则嵌入将被重用。 有关共享变量的文档是相关的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM