Decode (i.e., translate) an encoded sentence. Args: input_seq: A `numpy.ndarray` of shape `(1, max_encoder_seq_length, num_encoder_tokens)`. encoder_model: A `keras.Model` instance for the encoder. decoder_model: A `keras.Model` instance for the decoder. num_decoder_tokens: Number of un
(
inputSeq: tf.Tensor,
encoderModel: tf.LayersModel,
decoderModel: tf.LayersModel,
numDecoderTokens: number,
targetBeginIndex: number,
reverseTargetCharIndex: {[indice: number]: string},
maxDecoderSeqLength: number,
)
| 292 | """ |
| 293 | */ |
| 294 | async function decodeSequence ( |
| 295 | inputSeq: tf.Tensor, |
| 296 | encoderModel: tf.LayersModel, |
| 297 | decoderModel: tf.LayersModel, |
| 298 | numDecoderTokens: number, |
| 299 | targetBeginIndex: number, |
| 300 | reverseTargetCharIndex: {[indice: number]: string}, |
| 301 | maxDecoderSeqLength: number, |
| 302 | ) { |
| 303 | // Encode the input as state vectors. |
| 304 | let statesValue = encoderModel.predict(inputSeq) as tf.Tensor[]; |
| 305 | |
| 306 | // Generate empty target sequence of length 1. |
| 307 | let targetSeq = tf.buffer<tf.Rank.R3>([ |
| 308 | 1, |
| 309 | 1, |
| 310 | numDecoderTokens, |
| 311 | ]); |
| 312 | |
| 313 | // Populate the first character of target sequence with the start character. |
| 314 | targetSeq.set(1, 0, 0, targetBeginIndex); |
| 315 | |
| 316 | // Sampling loop for a batch of sequences |
| 317 | // (to simplify, here we assume a batch of size 1). |
| 318 | let stopCondition = false; |
| 319 | let decodedSentence = ''; |
| 320 | while (!stopCondition) { |
| 321 | const [outputTokens, h, c] = decoderModel.predict( |
| 322 | [targetSeq.toTensor(), ...statesValue] |
| 323 | ) as [ |
| 324 | tf.Tensor<tf.Rank.R3>, |
| 325 | tf.Tensor<tf.Rank.R2>, |
| 326 | tf.Tensor<tf.Rank.R2>, |
| 327 | ]; |
| 328 | |
| 329 | // Sample a token |
| 330 | const sampledTokenIndex = |
| 331 | await outputTokens.squeeze().argMax(-1).array() as number; |
| 332 | |
| 333 | const sampledChar = reverseTargetCharIndex[sampledTokenIndex]; |
| 334 | decodedSentence += sampledChar; |
| 335 | |
| 336 | // Exit condition: either hit max length |
| 337 | // or find stop character. |
| 338 | if (sampledChar === '\n' || |
| 339 | decodedSentence.length > maxDecoderSeqLength) { |
| 340 | stopCondition = true; |
| 341 | } |
| 342 | |
| 343 | // Update the target sequence (of length 1). |
| 344 | targetSeq = tf.buffer<tf.Rank.R3>([1, 1, numDecoderTokens], 'float32'); |
| 345 | targetSeq.set(1, 0, 0, sampledTokenIndex); |
| 346 | |
| 347 | // Update states |
| 348 | statesValue = [h, c]; |
| 349 | } |
| 350 | return decodedSentence; |
| 351 | } |