谁能建议一下为什么tensorflow tutorial中的"hidden = tf.zeros((1,units))“这行是正确的?我觉得应该是"hidden = tf.zeros((1,units))“。
发布于 2019-12-27 03:46:57
encoder使用线路hidden = [tf.zeros((1, units))]。具体地说,将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的列表。
https://stackoverflow.com/questions/59486554
复制相似问题