How to save fasttext model in binary and text formats?
Asked Answered
F

2

6

The documentation is a bit unclear how to save the fasttext model to disk - how do you specify a path in the argument, I tried doing so and it failed with an error

Example in documentation

>>> from gensim.test.utils import get_tmpfile
>>>
>>> fname = get_tmpfile("fasttext.model")
>>>
>>> model.save(fname)
>>> model = FastText.load(fname)

Furthermore, how can I save the model in text format like can be done with word2vec models?

'word2vecmodel.wv.save_word2vec_format("D:\w2vmodel.txt")'

EDIT

After trying the suggestion to make a file first I keep kgetting the same error as before when I run this code

savepath = os.path.abspath('D:\fasttextmodel.v3.bin');
from gensim.test.utils import get_tmpfile
fname = get_tmpfile(savepath)
fasttext_model.save(fname)

TypeError: file must have a 'write' attribute

Fertilize answered 30/8, 2019 at 16:57 Comment(0)
Q
0

Documentation in FastText save()/load() example is misleading, they suggest you use get_tmpfile. I am able to save the model if I pass the data file name as a string and do not wrap it in get_tmpfile:

model.save("fasttext.model")

Then you can load the same way, passing the string directly:

model = FastText.load("fasttext.model")

Note that this will save multiple files for models that are large. However, when you load the model, you only need to specify the main fasttext.model file, and the function will automatically load additional files, if there are any.

Quita answered 11/6, 2021 at 20:15 Comment(0)
N
-2

Did you try creating a file in your local directory called "fasttext.model" before trying to save it?

Also, I'm assuming you trained the model before this correct?

Nick answered 30/8, 2019 at 18:42 Comment(2)
yes I trained the model, but I didn't know you had to create a blank file first, please see my edit - after trying to create a file I get the same error as beforeFertilize
You don't have to create a blank file first - but trying that could reveal if you are even allowed to create files there, or if there's some other permissions problem (outside of your specific gensim/FastText choices).Homy

© 2022 - 2024 — McMap. All rights reserved.