# Save trained model and add to a pretrained model

**URL:** <https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187>\
**Category:** Uncategorized\
**Tags:** usage, textcat, spacy, solved\
**Created:** [February 5, 2019, 12:48am UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187 "2019-02-05T00:48:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timothyjlaurent](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/timothyjlaurent/32/590_2.png) [@timothyjlaurent](https://support.prodi.gy/u/timothyjlaurent)\
**Post date:** [February 5, 2019, 12:48am UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187/1 "2019-02-05T00:48:44Z")

</div>

Hi All,

I'm trying to figure out how to best save on space. We want to do periodic batch-training and persist the models to s3. I notice that when I run textcat.batch\_train, it creates a new textcat spacy model and adds it to the text cat directory in the model next to the vocabulary directory that comes with the en\_vectors\_web\_lg model. Since the vocabulary isn't being changed, it would be nice to be able to omit the vocabulary from the persisted model and then add it back in when doing textcat.teach.

Is there a good way to mix and match the model sub-components like this?

Thanks,

---

<div class="post-metadata">

**Author:** ![honnibal](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/honnibal/32/35_2.png) [@honnibal](https://support.prodi.gy/u/honnibal)\
**Post date:** [February 6, 2019, 8:10am UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187/2 "2019-02-06T08:10:44Z")

</div>

Hey,

Yeah, it should be fairly easy to do what you want. You should be able to do either:

`nlp.get_pipe('textcat').to_bytes(vocab=False)` or `nlp.get_pipe('textcat').to_disk(vocab=False)`, which should save out the textcat model without the vocab. This should let you checkpoint the model and then load it back later. The vectors data — which is the part that takes a lot of space — is immutable, so you don’t need to save it out every checkpoint.

---

<div class="post-metadata">

**Author:** ![timothyjlaurent](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/timothyjlaurent/32/590_2.png) [@timothyjlaurent](https://support.prodi.gy/u/timothyjlaurent)\
**Post date:** [September 18, 2019, 1:07am UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187/3 "2019-09-18T01:07:51Z")

</div>

Thanks @honnibal,

This works well. The part I'm having trouble with is loading the pipe back into the model.

I tried:

```python
nlp.from_disk(<path to model>)

```

But I'm seeing an error:

```python
{ValueError}Can't read file: /var/folders/7s/chb7w54n6g79dr448sp625j40000gp/T/tmp0ax88qjj/meta.json

```

Any help would be greatly appreciated.

PS we'd love to be part of the prodigy-scale beta so I don't have to muck around with this stuff.

---

<div class="post-metadata">

**Author:** ![timothyjlaurent](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/timothyjlaurent/32/590_2.png) [@timothyjlaurent](https://support.prodi.gy/u/timothyjlaurent)\
**Post date:** [September 18, 2019, 10:11pm UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187/4 "2019-09-18T22:11:00Z")

</div>

So I think I have a working solution:

```python
# so save the model to disk

nlp.get_pipe('textcat').to_disk(model_path, exclude=['vocab'])

# now create a new model with vocab

 nlp = spacy.load(input_model)

# add the sentencizer that TextClassifier adds 
sentencizer = nlp.create_pipe('sentencizer')
nlp.add_pipe(sentencizer)

# create a new textcat pipe with vocab
textcat = nlp.create_pipe('textcat')
# add the model and cfg from the dumped model        
textcat.from_disk(model_path, exclude=['vocab'])
# add pipe to language model
nlp.add_pipe(textcat)

```

Does this approach look correct?

---

<div class="post-metadata">

**Author:** ![ines](https://sea2.discourse-cdn.com/flex020/user_avatar/support.prodi.gy/ines/32/3_2.png) [@ines](https://support.prodi.gy/u/ines)\
**Post date:** [September 19, 2019, 9:57am UTC](https://support.prodi.gy/t/save-trained-model-and-add-to-a-pretrained-model/1187/5 "2019-09-19T09:57:53Z")

</div>

Yes, that looks good 👍
