# Feeding Custom/Pretrained embeddings for ner\_crf

**URL:** <https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406>\
**Category:** Rasa Open Source\
**Created:** [February 21, 2019, 2:18pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406 "2019-02-21T14:18:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gowtham1997](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gowtham1997](https://forum.rasa.com/u/gowtham1997)\
**Post date:** [February 21, 2019, 2:18pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/1 "2019-02-21T14:18:13Z")

</div>

Hello,

I’m trying to use custom embeddings or pretrained embeddings with ner\_crf for entity extraction, but can’t find a proper tutorial for it yet. I have tried using fasttext with spacy but I don’t think the embeddings are being used by ner\_crf(as I’m not using POS tags feature with ner\_crf).

If I had to feed custom embeddings as an additional feature to ner\_crf, how should I do it with/without spacy(Spacy doesn’t have support for Bert embeddings yet)?

---

<div class="post-metadata">

**Author:** ![Juste](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/juste/32/15_2.png) [@Juste](https://forum.rasa.com/u/Juste)\
**Post date:** [March 7, 2019, 12:46pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/2 "2019-03-07T12:46:21Z")

</div>

Hey @gowtham1997. Did you have a chance to look into the blogpost written by our contributor Souvik? [Build a Rasa NLU Chatbot with spaCy and FastText – strai – Medium](https://medium.com/strai/build-a-rasa-nlu-chatbot-with-spacy-with-fasttext-240e192082bd)

---

<div class="post-metadata">

**Author:** ![gowtham1997](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gowtham1997](https://forum.rasa.com/u/gowtham1997)\
**Post date:** [March 7, 2019, 1:33pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/3 "2019-03-07T13:33:22Z")

</div>

Hello,

@Juste Yes, I did and have followed [this GitHub issue](https://github.com/RasaHQ/rasa_nlu/issues/869) to use FastText with rasa. But going through the [code](https://github.com/RasaHQ/rasa_nlu/blob/master/rasa_nlu/extractors/crf_entity_extractor.py) I see that Spacy is only used when pos\_features are used with the ner\_crf(and pos features aren’t included in default params of ner\_crf). I tried using the pos\_features(in the config file) as well but did not find any improvements. So my questions are:

1. assuming the embeddings I want are available in spacy and I create a package following the instructions, does rasa only use pos\_tags as features for ner\_crf? (Going through the code, I couldn’t find where the actual word embeddings are directly used. So maybe I’m missing something.)
2. Let’s say Spacy doesn’t have embeddings I want to use(or I have custom embeddings for every word or subword). How do I pass them as features to ner\_crf?

---

<div class="post-metadata">

**Author:** ![andadiana](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/andadiana/32/2875_2.png) [@andadiana](https://forum.rasa.com/u/andadiana)\
**Post date:** [May 14, 2019, 10:32am UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/4 "2019-05-14T10:32:06Z")

</div>

Hi @gowtham1997, did you manage to figure this out? I’m having the same problem and I also went through the code, without being able to figure out where and how the actual word embeddings are used by ner\_crf.

---

<div class="post-metadata">

**Author:** ![jamesmf](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/jamesmf/32/4476_2.png) [@jamesmf](https://forum.rasa.com/u/jamesmf)\
**Post date:** [August 7, 2019, 5:02am UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/5 "2019-08-07T05:02:33Z")

</div>

I have a work-in-progress PR to discuss how to pass these kinds of features to ner\_crf/CRFEntityExtractor. This would then pair with another new component like SpacyVectorEntityFeaturizer that would pass the features along. That way if any new components for custom NER came along, it would be reusable.

> <https://github.com/RasaHQ/rasa/pull/4187>
>
> This is a work-in-progress implementation that allows custom entity-featurizers …to pass features along to \`CRFEntityExtractor\`. This would enable new featurizers that passed word/token vectors along to \`CRFEntityExtractor\`. Also following this \`ner\_features\` pattern would make it easy to develop other components that could also do custom NER. 
> 
> The edit to the example is just to show that, while the \`DummyNERFeaturizer\` provides nothing but noise to the \`CRFEntityExtractor\`, it seems to be working.
> 
> Just looking for feedback before I add tests or do much else.
> 
> Addresses https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406
> 
> \*\*Proposed changes\*\*:
> \- Make \`CRFEntityExtractor\` look for \`ner\_features\` on the message
> \- Have \`CRFEntityExtractor\` convert the features from an array to \`python-crfsuite\` style dicts
> 
> \*\*Status (please check what you already did)\*\*:
> \- \[X\] made PR ready for code review
> \- \[x\] added some tests for the functionality
> \- \[x\] updated the documentation
> \- \[x\] updated the changelog
> \- \[X\] reformat files using \`black\` (please check \[Readme\](https://github.com/RasaHQ/rasa\_nlu#code-style) for instructions)

---

<div class="post-metadata">

**Author:** ![rasafan](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rasafan](https://forum.rasa.com/u/rasafan)\
**Post date:** [September 13, 2019, 11:30am UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/6 "2019-09-13T11:30:22Z")

</div>

Hi, is your solution now answer to the original question and best way to go if I have the same problem?

---

<div class="post-metadata">

**Author:** ![jamesmf](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/jamesmf/32/4476_2.png) [@jamesmf](https://forum.rasa.com/u/jamesmf)\
**Post date:** [September 13, 2019, 1:11pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/7 "2019-09-13T13:11:56Z")

</div>

It’s not merged into master yet, but yes.

If you have a `SpacyFeaturizer` whose component config specifies `ner_feature_vectors: true`, it should work. It will make `token.vector` available to `CRFEntityExtractor` for every `token` in the `spacy.Doc`

---

<div class="post-metadata">

**Author:** ![rasafan](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rasafan](https://forum.rasa.com/u/rasafan)\
**Post date:** [September 14, 2019, 10:54am UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/8 "2019-09-14T10:54:31Z")

</div>

@jamesmf @souvikg10 - I read your tutorial too

Just to check, this is how we did things:

1. Downloaded the language I desire from here - [Word vectors for 157 languages · fastText](https://fasttext.cc/docs/en/crawl-vectors.html)
2. Started this code and saved the model - [https://github.com/souvikg10/spacy-fasttext/blob/master/load\_fastText.py](https://github.com/souvikg10/spacy-fasttext/blob/master/load_fastText.py)
3. Loaded the model with rasa, made our domain,stories, etc. files for that language and trained on it
4. Started chatting and used config like this:

> ```
> language: "br"
> pipeline: "pretrained_embeddings_spacy"
> policies:
> - epochs: 45
> max_history: 10
> name: KerasPolicy
> - max_history: 10
> name: AugmentedMemoizationPolicy
> - name: "FallbackPolicy"
> nlu_threshold: 0.2
> core_threshold: 0.1
> fallback_action_name: "action_default_fallback"
> 
> ```

This worked and we started to chat with the bot but I know we do not have any entity extraction so it is pretty lame, can you maybe help me with what should I do next, should I wait for your solution to come to master branch or are there things I need to do beforehand?

Thanks.

---

<div class="post-metadata">

**Author:** ![jamesmf](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/jamesmf/32/4476_2.png) [@jamesmf](https://forum.rasa.com/u/jamesmf)\
**Post date:** [September 16, 2019, 12:31pm UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/9 "2019-09-16T12:31:51Z")

</div>

You just need to replace the `pipeline: "pretrained_embeddings_spacy"` with individual components. You can pick and choose, but if you want mostly `spacy` based components, you could do:

```auto
pipeline:
  - name: 'SpacyNLP'
    model: 'your_model_name_here'
  - name: 'SpacyTokenizer'
  - name: 'SpacyFeaturizer'
    ner_feature_vectors: true # this is the part that's new functionality
  - name: 'CRFEntityExtractor'
  - name: 'EmbeddingIntentClassifier'

```

This would use `spacy` to tokenize, would create features for intents using the `.vector` attribute on the `Doc`, and would pass the `.vector` attribute on each token to the `CRFEntityExtractor` as (some of) the features to do custom entity extraction.

---

<div class="post-metadata">

**Author:** ![Shashikant9198](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Shashikant9198](https://forum.rasa.com/u/Shashikant9198)\
**Post date:** [May 22, 2020, 7:22am UTC](https://forum.rasa.com/t/feeding-custom-pretrained-embeddings-for-ner-crf/5406/10 "2020-05-22T07:22:11Z")

</div>

Hi @Juste , Can Rasa just use idea of this paper(" Massively Multilingual Sentence Embeddings for Zero-ShotCross-Lingual Transfer and Beyond ") in place of pretrained embeddings in DIET Classifier archiecture. Instead of using GLoVe and BERT or ConveRT. Early reply will be admired…
