Building the model

Now let's build our model. Let's define some of the important hyperparameters that our model needs:

  • The size parameter represents the size of the vector, that is, dimensions of our vector, to represent a word. The size can be chosen according to our data size. If our data is very small, then we can set the size to a small value, but if we have a significantly large dataset, then we can set the size to 300. In our case, we set the size to 100.
  • The window_size parameter represents the distance that should be considered between the target word and its neighboring word. Words exceeding the window size from the target word will not be considered for learning. Typically, a small window size is preferred.
  • The min_count parameter represents the minimum frequency of words. If the particular word's occurrence is less than a min_count, then we can simply ignore that word.
  • The workers parameter specifies the number of worker threads we need to train the model.
  • Setting sg=1 implies that we use the skip-gram model for training, but if it is set to sg=0, then it implies that we use CBOW model for training.

Define all the hyperparameters using following code:

size = 100
window_size = 2
epochs = 100
min_count = 2
workers = 4
sg = 1

Let's train the model using the Word2Vec function from gensim:

model = Word2Vec(corpus, sg=1,window=window_size,size=size, min_count=min_count,workers=workers,iter=epochs)

Once, we trained the model successfully, we save them. Saving and loading the model is very simple; we can simply use the save and load functions, respectively:

model.save('model/word2vec.model')

We can also load the already saved Word2Vec model by using the following code:

model = Word2Vec.load('model/word2vec.model')
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset