张量 2.0 NMT 中编码器-解码器示例中隐藏张量的大小?

The size of the hidden tensor in encoder-decoder example in tensor 2.0 NMT?

谁能指出为什么 tensorflow tutorial 中的 "hidden = [tf.zeros((1, units))]" 行是正确的?我觉得应该是"hidden = tf.zeros((1, units))".

hidden = [tf.zeros((1, units))]行被encoder使用。具体来说,hidden 作为第二个输入参数传递给 encoder.call()encoder.call()的定义是:

class Encoder(tf.keras.Model):

  def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
    # ... Omitted some code...
    self.gru = tf.keras.layers.GRU(self.enc_units,
                                   return_sequences=True,
                                   return_state=True,
                                   recurrent_initializer='glorot_uniform')

  def call(self, x, hidden):
    x = self.embedding(x)
    output, state = self.gru(x, initial_state = hidden)
    return output, state

如您所见,hidden 作为其 initial_state 传递给 self.gru.call()。根据 the documentation of tf.keras.layers.GRU,初始状态必须是一个列表,并且列表的长度必须与 RNN 的内部状态数相匹配。在这种情况下,RNN 是一个 GRU,并且有一个内部状态。因此,您会看到长度为 1 的列表。