如何使用 tf2 为 seq2seq 构建自定义双向编码器? [英] how to build a custom bidirectional encoder for seq2seq with tf2?

查看:113
本文介绍了如何使用 tf2 为 seq2seq 构建自定义双向编码器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 层时,出现以下错误:

For the above code, when I used the gru layer, it worked. But when I used the bigru layer, I got the following error:

ValueError:在转换后的代码中:

ValueError: in converted code:

<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)

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

So I'm now wondering what is going on here?

推荐答案

它不是 有据可查,但双向层(与单向 RNN 层不同)返回一个三元组:

It is not well-documented, but the bidirectional layer (unlike the single direction RNN layer) returns a three-tuple:

  • 前向和后向 RNN 的连接状态(形状:batch × 长度 × 2 GRU 维度)
  • 前向 RNN 的最终状态(形状:batch × batch 维度)
  • 反向 RNN 的最终状态(形状:batch × batch 维度)
  • concatenated states of the forward and backward RNN (shape: batch × length × 2 GRU dimentsion)
  • final state of the forward RNN (shape: batch × batch dimension)
  • final state of the backward RNN (shape: batch × batch dimension)

这篇关于如何使用 tf2 为 seq2seq 构建自定义双向编码器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆