MCPcopy
hub / github.com/tensorflow/tfjs-examples / seq2seq_model

Function seq2seq_model

translation/python/translation.py:121–168  ·  view source on GitHub ↗

Create a Keras model for the seq2seq translation. Args: num_encoder_tokens: Total number of distinct tokens in the inputs to the encoder. num_decoder_tokens: Total number of distinct tokens in the outputs to/from the decoder latent_dim: Number of latent dimensions in the L

(num_encoder_tokens, num_decoder_tokens, latent_dim)

Source from the content-addressed store, hash-verified

119
120
121def seq2seq_model(num_encoder_tokens, num_decoder_tokens, latent_dim):
122 """Create a Keras model for the seq2seq translation.
123
124 Args:
125 num_encoder_tokens: Total number of distinct tokens in the inputs
126 to the encoder.
127 num_decoder_tokens: Total number of distinct tokens in the outputs
128 to/from the decoder
129 latent_dim: Number of latent dimensions in the LSTMs.
130
131 Returns:
132 encoder_inputs: Instance of `keras.Input`, symbolic tensor as input to
133 the encoder LSTM.
134 encoder_states: Instance of `keras.Input`, symbolic tensor for output
135 states (h and c) from the encoder LSTM.
136 decoder_inputs: Instance of `keras.Input`, symbolic tensor as input to
137 the decoder LSTM.
138 decoder_lstm: `keras.Layer` instance, the decoder LSTM.
139 decoder_dense: `keras.Layer` instance, the Dense layer in the decoder.
140 model: `keras.Model` instance, the entire translation model that can be
141 used in training.
142 """
143 # Define an input sequence and process it.
144 encoder_inputs = Input(shape=(None, num_encoder_tokens))
145 encoder = LSTM(latent_dim,
146 return_state=True)
147 _, state_h, state_c = encoder(encoder_inputs)
148 # We discard `encoder_outputs` and only keep the states.
149 encoder_states = [state_h, state_c]
150
151 # Set up the decoder, using `encoder_states` as initial state.
152 decoder_inputs = Input(shape=(None, num_decoder_tokens))
153 # We set up our decoder to return full output sequences,
154 # and to return internal states as well. We don't use the
155 # return states in the training model, but we will use them in inference.
156 decoder_lstm = LSTM(FLAGS.latent_dim,
157 return_sequences=True,
158 return_state=True)
159 decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
160 initial_state=encoder_states)
161 decoder_dense = Dense(num_decoder_tokens, activation='softmax')
162 decoder_outputs = decoder_dense(decoder_outputs)
163
164 # Define the model that will turn
165 # `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
166 model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
167 return (encoder_inputs, encoder_states, decoder_inputs, decoder_lstm,
168 decoder_dense, model)
169
170
171def decode_sequence(input_seq,

Callers 1

mainFunction · 0.85

Calls 1

encoderFunction · 0.85

Tested by

no test coverage detected