# MetaCAT - Issue with training when there's more than 2 output classes

**URL:** <https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351>\
**Category:** MedCAT\
**Created:** [April 25, 2025, 1:41pm UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351 "2025-04-25T13:41:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshi](https://avatars.discourse-cdn.com/v4/letter/y/aeb1de/32.png) [@Yoshi](https://discourse.cogstack.org/u/Yoshi)\
**Post date:** [April 25, 2025, 1:41pm UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/1 "2025-04-25T13:41:24Z")

</div>

![Screenshot 2025-04-25 at 14.39.19](https://europe1.discourse-cdn.com/flex017/uploads/cogstack/original/1X/77fc9fb91c811244642753cebd9171917573150c.png)

As above, I keep getting the error ‘Target 2 is out of bounds’ when trying to train a MetaCAT model with 3 possible outputs. From this thread, [IndexError: Target 2 is out of bounds - #12 by Vijaya\_kumar - vision - PyTorch Forums](https://discuss.pytorch.org/t/indexerror-target-2-is-out-of-bounds/69614/12), I believe it is to do with the input tensor being the wrong size but I’m struggling to debug it looking at the git hub code. This happens when using the model.train\_raw and model.train\_from\_json functions. Any thoughts?

Thanks

---

<div class="post-metadata">

**Author:** ![mart.ratas](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@mart.ratas](https://discourse.cogstack.org/u/mart.ratas)\
**Post date:** [April 28, 2025, 8:41am UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/2 "2025-04-28T08:41:04Z")

</div>

I think the issue is that the `meta_cat_config.model.nclasses` value is `2` while the number of of keys in `meta_cat_config.general.category_value2id` is 3.  
So if you set `meta_cat_config.model.nclasses = 3` this should be resolved.

---

<div class="post-metadata">

**Author:** ![Yoshi](https://avatars.discourse-cdn.com/v4/letter/y/aeb1de/32.png) [@Yoshi](https://discourse.cogstack.org/u/Yoshi)\
**Post date:** [April 30, 2025, 4:20pm UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/3 "2025-04-30T16:20:21Z")

</div>

It seems to struggle when you try to condense several classes into one. In my case this was:

- config\_metacat.general[‘category\_value2id’] = {‘patient’: 0, ‘family’: 1, ‘other’: 1}
- config\_metacat.model[‘nclasses’] = 2

I’ve had to edit the source code for metacat to fix this

---

<div class="post-metadata">

**Author:** ![mart.ratas](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@mart.ratas](https://discourse.cogstack.org/u/mart.ratas)\
**Post date:** [April 30, 2025, 4:52pm UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/4 "2025-04-30T16:52:38Z")

</div>

I do believe this is not something it is designed to do. The expectation is to have a one-to-one mapping for category values to the corresponding ID.

With that said, since release 1.16 (in a pre-release state right now - so not available on PyPI yet) we will be supporting `alternative_category_names`. The change was introduced here: [Updates for MetaCAT by shubham-s-agarwal · Pull Request #515 · CogStack/MedCAT · GitHub](https://github.com/CogStack/MedCAT/pull/515)  
You can look at the docs [here](https://medcat.readthedocs.io/en/v1.16.0/autoapi/medcat/config_meta_cat/index.html#medcat.config_meta_cat.General.alternative_category_names).

---

<div class="post-metadata">

**Author:** ![Yoshi](https://avatars.discourse-cdn.com/v4/letter/y/aeb1de/32.png) [@Yoshi](https://discourse.cogstack.org/u/Yoshi)\
**Post date:** [April 30, 2025, 6:48pm UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/5 "2025-04-30T18:48:46Z")

</div>

> [@mart.ratas](#):
>
> Updates for MetaCAT by shubham-s-agarwal · Pull Request #515 · CogStack/MedCAT · GitHub

Thanks, I’ll have a look. FYI the condensing of classes works with the BERT MetaCAT models, just not the LSTM ones. I’m trying to debug this at the moment as they share the same pipeline so it seems to be a model specific issue.

---

<div class="post-metadata">

**Author:** ![mart.ratas](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@mart.ratas](https://discourse.cogstack.org/u/mart.ratas)\
**Post date:** [May 1, 2025, 7:59am UTC](https://discourse.cogstack.org/t/metacat-issue-with-training-when-theres-more-than-2-output-classes/351/6 "2025-05-01T07:59:43Z")

</div>

If they work with something, then that’s just a happy accident. They’re not _designed_ to work that way.
