如何使用tf2为seq2seq构建自定义双向编码器?

问题描述 投票:1回答:1
class Encoder(tf.keras.Model):
  def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
    super(Encoder, self).__init__()
    self.batch_sz = batch_sz
    self.enc_units = enc_units

    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(self.enc_units,
                                   return_sequences=True,
                                   return_state=True,
                                   recurrent_initializer='glorot_uniform')

    self.bigru=tf.keras.layers.Bidirectional(tf.keras.layers.GRU(self.enc_units,
                                                                 return_sequences=True,
                                                                 return_state=True, recurrent_initializer='glorot_uniform'))

  def call(self, x):
    x = self.embedding(x)
    # output, state = self.gru(x)
    output, state = self.bigru(x)

    return output, state

对于以上代码,当我使用gru层时,它起作用了。但是,当我使用bigru层时,出现以下错误:

ValueError:转换后的代码:

<ipython-input-51-3ba1fe0beb05>:8 train_step_seq2seq  *
    enc_output, enc_hidden = encoder(inp)
/tensorflow-2.0.0/python3.6/tensorflow_core/python/keras/engine/base_layer.py:847 __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
<ipython-input-53-4f1b00e47a9a>:22 call  *
    output, state = self.bidir(x)

ValueError: too many values to unpack (expected 2)

所以我现在想知道这里发生了什么?

recurrent-neural-network tensorflow2.0 encoder seq2seq
1个回答
0
投票

不是well-documented,但是双向层(与单向RNN层不同)返回一个三元组:

© www.soinside.com 2019 - 2024. All rights reserved.