# Using keras Sequence with ragged inputs, ragged outputs, and Sparse Categorical Cross Entropy

**URL:** <https://discuss.ai.google.dev/t/using-keras-sequence-with-ragged-inputs-ragged-outputs-and-sparse-categorical-cross-entropy/27575>\
**Category:** General Discussion\
**Tags:** keras, models, help\_request\
**Created:** [June 27, 2022, 9:27am UTC](https://discuss.ai.google.dev/t/using-keras-sequence-with-ragged-inputs-ragged-outputs-and-sparse-categorical-cross-entropy/27575 "2022-06-27T09:27:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Bleackley](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Bleackley](https://discuss.ai.google.dev/u/Peter_Bleackley)\
**Post date:** [June 27, 2022, 9:27am UTC](https://discuss.ai.google.dev/t/using-keras-sequence-with-ragged-inputs-ragged-outputs-and-sparse-categorical-cross-entropy/27575/1 "2022-06-27T09:27:30Z")

</div>

I’m trying to create a text generation system. My model has sparse inputs in the form  
`[0]+document`, where document is a list of integer indices corresponding to the words in a document, and I am training it with Sparse Categorical Cross Entropy, so the y values should be in the form `document+[0]`. I want to use an object derived from `keras.utils.Sequence` to train the model, but I’m finding that the model doesn’t understand the shape of the y values.

How should my `Sequence` structure its y values?

---

<div class="post-metadata">

**Author:** ![Peter\_Bleackley](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@Peter\_Bleackley](https://discuss.ai.google.dev/u/Peter_Bleackley)\
**Post date:** [June 28, 2022, 7:04pm UTC](https://discuss.ai.google.dev/t/using-keras-sequence-with-ragged-inputs-ragged-outputs-and-sparse-categorical-cross-entropy/27575/3 "2022-06-28T19:04:16Z")

</div>

Some further information - added here as I’m too new to edit posts.  
When I structure my y values as

```auto
tensorflow.ragged.constant([row+[self.stop]
                            for row in batch])

```

The error I get is

```auto
ValueError: Inconsistent shapes: saw (None,) but expected () 

```

---

<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 8, 2024, 9:50am UTC](https://discuss.ai.google.dev/t/using-keras-sequence-with-ragged-inputs-ragged-outputs-and-sparse-categorical-cross-entropy/27575/5 "2024-07-08T09:50:46Z")

</div>

Hi @Peter_Bleackley ,

Welcome to the TensorFlow Forum .

Thank you for raising this issue. When working with ragged tensors in TensorFlow, if your sequences have consistent lengths across batches (including the stop token), you can convert them to regular tensors using `tf.ragged.to_tensor()`. This approach makes the output compatible with the expected shape for `keras.utils.Sequence`.

I’ve replicated your scenario and implemented this solution in a Colab notebook, which you can find [here](https://colab.research.google.com/gist/AniketD09/c8165e3fa2340489e4bf620fdc108693/replicated.ipynb). The notebook demonstrates how to effectively handle ragged tensors in this context.

Thank you !
