# Predict method with transformer encoder decoder

**URL:** <https://discuss.ai.google.dev/t/predict-method-with-transformer-encoder-decoder/30512>\
**Category:** General Discussion\
**Tags:** education, keras, models, help\_request\
**Created:** [May 31, 2023, 11:46pm UTC](https://discuss.ai.google.dev/t/predict-method-with-transformer-encoder-decoder/30512 "2023-05-31T23:46:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![acraev](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@acraev](https://discuss.ai.google.dev/u/acraev)\
**Post date:** [May 31, 2023, 11:46pm UTC](https://discuss.ai.google.dev/t/predict-method-with-transformer-encoder-decoder/30512/1 "2023-05-31T23:46:49Z")

</div>

I have fitted a model using a fairly standard transformer encoder-decoder architecture using keras\_nlp layers. When instantiating the model, I provide input as ([input, target], offset\_target). So, _target_ is just a sequence that goes into decoder and _offset\_target_ is the target sequence offset by 1 step. This is a standard nlp seq2seq model.

How do I correctly call predict method on the fitted model, as I have no target sequences?

---

<div class="post-metadata">

**Author:** ![Aniket\_Dubey](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/aniket_dubey/32/16664_2.png) [@Aniket\_Dubey](https://discuss.ai.google.dev/u/Aniket_Dubey)\
**Post date:** [July 9, 2024, 6:58am UTC](https://discuss.ai.google.dev/t/predict-method-with-transformer-encoder-decoder/30512/3 "2024-07-09T06:58:24Z")

</div>

Hi @acraev ,

Here’s how you can approach this:

1. Input preparation: For prediction, you only need to provide the encoder input. The decoder will generate the output sequence step by step.

2. Using the `predict` method: You’ll need to modify how you call the `predict` method. Instead of providing both input and target, you’ll only provide the encoder input.

3. Handling the decoder input: Your model should be set up to handle the absence of decoder input during inference. This is typically done by using a special start token and then feeding back the previous output as the next input.

4. Generating sequences: If your model doesn’t automatically handle the sequence generation, you might need to implement a custom prediction loop.

5. Using `keras_nlp` specific methods: If you’re using `keras_nlp.models` like `TransformerEncoder` and `TransformerDecoder`, they might provide built-in methods for generation. Check the [documentation1](https://www.tensorflow.org/api_docs/python/tfm/nlp/models/TransformerEncoder)[documentation2](https://www.tensorflow.org/api_docs/python/tfm/nlp/models/TransformerDecoder) for any `generate` or similar methods that handle the decoding process automatically.

Additionally you can also refer this [Article](https://www.tensorflow.org/api_docs/python/tfm/nlp/models/Seq2SeqTransformer)  
Hope this helps.

Thank you.
