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

时间:2016-09-09 09:55:35

标签: python tensorflow

我正在构建一个基于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]))

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

  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)

知道如何优雅地让这两个人使用相同的嵌入矩阵吗?

1 个答案:

答案 0 :(得分:0)

当您调用创建第二个嵌入的函数时,可以使用重用变量作用域。如果使用具有相同名称的作用域并设置reuse = True,则将重用嵌入。 sharing variables上的文档是相关的。