# Linking Labels

**URL:** <https://support.prodi.gy/t/linking-labels/6386>\
**Category:** Uncategorized\
**Tags:** relations\
**Created:** [February 24, 2023, 1:44pm UTC](https://support.prodi.gy/t/linking-labels/6386 "2023-02-24T13:44:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Mohammad](https://support.prodi.gy/u/Mohammad)\
**Post date:** [February 24, 2023, 1:44pm UTC](https://support.prodi.gy/t/linking-labels/6386/1 "2023-02-24T13:44:18Z")

</div>

Hello,  
After I trained the model to extract "CompaniesWorkedAt" ,"Designation" and "Location" the model predict labels that not linking to each other . How I can add relation between the entities or link entities to together.

---

<div class="post-metadata">

**Author:** ![ryanwesslen](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ryanwesslen/32/2969_2.png) [@ryanwesslen](https://support.prodi.gy/u/ryanwesslen)\
**Post date:** [February 24, 2023, 6:31pm UTC](https://support.prodi.gy/t/linking-labels/6386/2 "2023-02-24T18:31:40Z")

</div>

hi @Mohammad!

Thanks for your question.

> [@Mohammad](#):
>
> After I trained the model to extract "CompaniesWorkedAt" ,"Designation" and "Location" the model predict labels that not linking to each other . How I can add relation between the entities or link entities to together.

Just to confirm, you trained an `ner` component for your entities (`"CompaniesWorkedAt" ,"Designation" and "Location"`), right?

And now you want to train a custom `relations` model to link those entities?

## Step 1: Get relations annotations

You can now use the `rel.manual` recipe to annotate relations. Please read through our [section in our docs](https://prodi.gy/docs/dependencies-relations#ner).

## Step 2: Training relations requires some configuration

As that section highlights:

> Note that in order to train a model to predict relations between spans, you have to bring **your own model implementation** and implement your own training.

What's important to know is that unlike other native built-in spaCy components like `ner`, `spancat`, and `textcat`, there isn't a built-in spaCy component for `relations`. Therefore, `prodigy train` and `data-to-spacy` doesn't work out of the box for training `relations`.

> [@prodigy data-to-spacy for relation extraction](https://support.prodi.gy/t/prodigy-data-to-spacy-for-relation-extraction/5620/2):
>
> At the time of writing, spaCy doesn't natively support relation extraction models. The example that we list on our docs [here](https://spacy.io/usage/layers-architectures#component-rel) is meant to be a tutorial on how to set up a custom component, not a guide on a feature in spaCy. The crux of the issue is that the Doc object in spaCy currently has no support for relationships. That is also why, in turn, the .spacy object does not support them. The config file that you see can be changed via the --config flag ([docs](https://prodi.gy/docs/recipes#data-to-spacy)). If this flag is not set, which is y…

However, we've created a tutorial video that explains how to build a custom trainable component with `Thinc`

[![](https://i.ytimg.com/vi/8HL-Ap5_Axo/hqdefault.jpg "SPACY v3: Custom trainable relation extraction component") ](https://www.youtube.com/watch?v=8HL-Ap5_Axo)

including companion project with code too

> **[projects/tutorials/rel\_component at v3 · explosion/projects](https://github.com/explosion/projects/tree/v3/tutorials/rel_component)**
>
> 🪐 End-to-end NLP workflows from prototype to production - explosion/projects

What's important is that you can use most of Sofie's project for training. However, you will need to customize the code, especially the `parse_data.py` file. There's more details in these posts:

> [@data-to-spacy for rel\_component training](https://support.prodi.gy/t/data-to-spacy-for-rel-component-training/5852/4):
>
> Hi @korneliaB , Ok, let us step back for a bit. I realized that since you already have the labeled documents in Prodigy, you can export them into .jsonl using the [db-out command](https://prodi.gy/docs/recipes#db-out), then [reuse / modify this parse\_data.py script](https://github.com/explosion/projects/blob/v3/tutorials/rel_component/scripts/parse_data.py) to convert the JSONL files into the spaCy format. The reason why it errored out is because it expects some labels before the component is initialized. You can see this being done in the [main function](https://github.com/explosion/projects/blob/v3/tutorials/rel_component/scripts/parse_data.py#L22). So you have to do something like: python scripts.parse\_data path/to/js…

> [@Training a relation extraction component](https://support.prodi.gy/t/training-a-relation-extraction-component/6376/4):
>
> Hi @stella! Yes, this is exactly the setup Sofie does. She explicitly says from the beginning she's going to assume she already has a trained ner component. Yes! Sofie used Thinc for training. You can see the training code [here](https://github.com/explosion/projects/blob/v3/tutorials/rel_component/scripts/rel_model.py) and she carefully explains the code in 8:11 to 18:30 the Thinc model script. She then describes around 22:55 an Overview of the TrainablePipe API and how to implement the custom component. You may not need to know all of the details and can luckily leverage a lot of t…

> [@Mohammad](#):
>
> How I can add relation between the entities or link entities to together.

Since you also mentioned how to "link entities together", I suspect you may be interested in entity linking (aka entity disambiguation). Sofie also has a related video on entity linking as well:

[![](https://i.ytimg.com/vi/8u57WSXVpmw/hqdefault.jpg "Training a custom ENTITY LINKING model with spaCy") ](https://www.youtube.com/watch?v=8u57WSXVpmw)

And similar project too:

> **[projects/tutorials/nel\_emerson at v3 · explosion/projects](https://github.com/explosion/projects/tree/v3/tutorials/nel_emerson)**
>
> 🪐 End-to-end NLP workflows from prototype to production - explosion/projects
