Word2Vec Model Training and Saving with Gensim

  • Share this:

Code introduction


This custom function uses the Word2Vec model from the gensim library to train a word vector model. It takes two arguments: the first argument is the directory path to save the model file, and the second argument is the file path containing the text data. After training, the function saves the model to the specified directory.


Technology Stack : gensim, os, LineSentence, Word2Vec

Code Type : Custom function

Code Difficulty : Intermediate


                
                    
def word2vec_model(arg1, arg2):
    from gensim.models import Word2Vec
    from gensim.models.word2vec import LineSentence
    import os

    # Check if the directory exists, if not, create it
    if not os.path.exists(arg1):
        os.makedirs(arg1)

    # Train a Word2Vec model
    sentences = LineSentence(arg2)
    model = Word2Vec(sentences, vector_size=100, window=5, min_count=1, workers=4)

    # Save the model
    model.save(os.path.join(arg1, 'word2vec.model'))

    # Return the trained model
    return model