Skip to content

Instantly share code, notes, and snippets.

@nahidalam
Created August 20, 2020 03:51
Show Gist options
  • Save nahidalam/26c51fad94f38000a4ab15a3615823a7 to your computer and use it in GitHub Desktop.
Save nahidalam/26c51fad94f38000a4ab15a3615823a7 to your computer and use it in GitHub Desktop.
class Decoder(tf.keras.Model):
def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
super(Decoder, self).__init__()
self.batch_sz = batch_sz
self.dec_units = dec_units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.dec_units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
self.fc = tf.keras.layers.Dense(vocab_size)
def call(self, x, hidden, enc_output):
# as we have specified return_sequences=True in encoder gru
# enc_output shape == (batch_size, max_length, hidden_size)
# x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)
output, state = self.gru(x, initial_state = hidden)
# output shape == (batch_size * 1, hidden_size)
output = tf.reshape(output, (-1, output.shape[2]))
# output shape == (batch_size, vocab)
x = self.fc(output)
return x, state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment