# Can keras.utils.Sequence return a triple?

**URL:** <https://discuss.ai.google.dev/t/can-keras-utils-sequence-return-a-triple/29401>\
**Category:** General Discussion\
**Tags:** keras, api, custom-loss\
**Created:** [March 4, 2023, 4:15am UTC](https://discuss.ai.google.dev/t/can-keras-utils-sequence-return-a-triple/29401 "2023-03-04T04:15:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bob\_Zigon](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/bob_zigon/32/15472_2.png) [@Bob\_Zigon](https://discuss.ai.google.dev/u/Bob_Zigon)\
**Post date:** [March 4, 2023, 4:15am UTC](https://discuss.ai.google.dev/t/can-keras-utils-sequence-return-a-triple/29401/1 "2023-03-04T04:15:31Z")

</div>

I have a model where the inputs are X,Y,M. All of them are matrices. My custom loss function needs access to the Mask matrix so that I can do something like tf.square(y\_true - y\_pred\*mask). All of my batches are generated by a class that inherits from keras.utils.Sequence.

All of the examples I have seen show the **getitem** method for the Sequence class returning x,y.  
I need to return x,y,m. Is this legal? I’m trying to debug a problem and I need to understand if this basic assumption about the Sequence was valid?

---

<div class="post-metadata">

**Author:** ![aniruthraj](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/aniruthraj/32/660_2.png) [@aniruthraj](https://discuss.ai.google.dev/u/aniruthraj)\
**Post date:** [November 21, 2024, 5:48am UTC](https://discuss.ai.google.dev/t/can-keras-utils-sequence-return-a-triple/29401/3 "2024-11-21T05:48:14Z")

</div>

Hi @Bob_Zigon,

Sorry for the delay in response.  
Yes, returning multiple outputs from ` __getitem__ ` in a Keras `Sequence` class is valid but standard keras training expects two.We have to make sure that ` __getitem__ ` method returns three items: x\_batch, y\_batch, and mask\_batch(in your case) and need to modify the loss function to handle the triple input. Here’s a sample implementation [gist](https://colab.research.google.com/gist/aniruthraj/03872fd53c8deb0f7ef9b681b0b4df56/sequence.ipynb) of above for your reference.

Hope this helps.Thank You.
