| 324 | |
| 325 | |
| 326 | def decode_sequence(input_seq): |
| 327 | # Encode the input as state vectors. |
| 328 | states_value = encoder_model.predict(input_seq) |
| 329 | |
| 330 | # Generate empty target sequence of length 1. |
| 331 | target_seq = np.zeros((1, 1)) |
| 332 | |
| 333 | # Populate the first character of target sequence with the start character. |
| 334 | # NOTE: tokenizer lower-cases all words |
| 335 | target_seq[0, 0] = word2idx_outputs['<sos>'] |
| 336 | |
| 337 | # if we get this we break |
| 338 | eos = word2idx_outputs['<eos>'] |
| 339 | |
| 340 | # Create the translation |
| 341 | output_sentence = [] |
| 342 | for _ in range(max_len_target): |
| 343 | output_tokens, h, c = decoder_model.predict( |
| 344 | [target_seq] + states_value |
| 345 | ) |
| 346 | # output_tokens, h = decoder_model.predict( |
| 347 | # [target_seq] + states_value |
| 348 | # ) # gru |
| 349 | |
| 350 | # Get next word |
| 351 | idx = np.argmax(output_tokens[0, 0, :]) |
| 352 | |
| 353 | # End sentence of EOS |
| 354 | if eos == idx: |
| 355 | break |
| 356 | |
| 357 | word = '' |
| 358 | if idx > 0: |
| 359 | word = idx2word_trans[idx] |
| 360 | output_sentence.append(word) |
| 361 | |
| 362 | # Update the decoder input |
| 363 | # which is just the word just generated |
| 364 | target_seq[0, 0] = idx |
| 365 | |
| 366 | # Update states |
| 367 | states_value = [h, c] |
| 368 | # states_value = [h] # gru |
| 369 | |
| 370 | return ' '.join(output_sentence) |
| 371 | |
| 372 | |
| 373 | |