# Named Entity Recognition: fine-tune or create new model?

**URL:** <https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476>\
**Category:** Beginners\
**Created:** [February 10, 2023, 10:45pm UTC](https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476 "2023-02-10T22:45:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![antcodes](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@antcodes](https://discuss.huggingface.co/u/antcodes)\
**Post date:** [February 10, 2023, 10:45pm UTC](https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476/1 "2023-02-10T22:45:39Z")

</div>

Hello,

I have the bert-base-NER model running on my system.

But I want to recognize other kinds of entities, for example prices and product names. I have my own data for this that I can label to train the model.

Is this something that can be achieved by fine-tuning the bert-base-NER model?

Or should I create a model from scratch instead?

Thanks for any advice.

---

<div class="post-metadata">

**Author:** ![nbroad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nbroad/32/26465_2.png) [@nbroad](https://discuss.huggingface.co/u/nbroad)\
**Post date:** [February 11, 2023, 2:48am UTC](https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476/2 "2023-02-11T02:48:09Z")

</div>

You can’t just add a few labels to a pre-existing model. I would recommend starting from a fine-tuned model, but you’ll need labels for all entities that you are trying to detect, including the old ones.

---

<div class="post-metadata">

**Author:** ![antcodes](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@antcodes](https://discuss.huggingface.co/u/antcodes)\
**Post date:** [February 11, 2023, 3:57am UTC](https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476/3 "2023-02-11T03:57:05Z")

</div>

Thanks for your reply. So just to clarify: you’re saying that I _can_ do this by fine-tuning something like bert-base-NER, as long as in my training dataset, I label everything that I want to be able to detect?

---

<div class="post-metadata">

**Author:** ![nbroad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nbroad/32/26465_2.png) [@nbroad](https://discuss.huggingface.co/u/nbroad)\
**Post date:** [February 11, 2023, 4:23am UTC](https://discuss.huggingface.co/t/named-entity-recognition-fine-tune-or-create-new-model/31476/4 "2023-02-11T04:23:25Z")

</div>

Actually, you could transfer the weights from the old classifier to the new one.

In pytorch, if your classifier is this:

```python

classifier1 = nn.Linear(hidden_size, num_labels)

```

it will have shape `(num_labels, hidden_size)`

So if you add more `k` more labels, the new classifier will be

```python

classifier2 = nn.Linear(hidden_size, num_labels+k)

```

and it will have shape `(num_labels+k, hidden_size)`

You would then do

```python

classifier2.weight.data[:num_labels, :] = classifier1.weight.data

```

You’ll still need to train on samples that have all the labels (otherwise it will forget the originals), and you need to make sure that the ids for each label stay the same. i.e. if your label2id is originally `{"person": 0, "org": 1, "misc": 2}`, the new label2id should be `{"person": 0, "org": 1, "misc": 2, "price": 3, "product_names": 4}`

oh and you should init the classifer2 weights before moving the weights. It’s common to do something like this:

```python
module.weight.data.normal_(mean=0.0, std=std)
if module.bias is not None:
    module.bias.data.zero_()

```

where std is the initializer range in the config. usually 0.02
