Natural Language Processing · Intermediate level

Intermediate level · Module 3: Word Embeddings and Word2Vec · Lesson 2 of 3

Training Word2Vec with Gensim

Learners train Word2Vec on the project corpus with Gensim, explore nearest neighbours and see how corpus size and parameters change the results.

In this lesson you will

  • Train a Word2Vec model with Gensim
  • Explore and evaluate embeddings

Key concepts

Word2Vec(sentences, vector_size, window, min_count, sg)model.wv.most_similarcorpus sizerandom seedintrinsic evaluation

The lesson

Gensim trains Word2Vec from a list of tokenised sentences: Word2Vec(sentences, vector_size=100, window=5, min_count=2, sg=1, seed=1). model.wv['school'] returns a vector and model.wv.most_similar('school') the nearest words.

Embeddings need a lot of text. On a few thousand sentences, neighbours are noisy and change between runs. This is a direct illustration of the low-resource problem: the same method that works on billions of English words struggles on small local corpora.

Evaluate embeddings intrinsically by checking neighbours for words you understand well, and extrinsically by testing whether they improve a downstream task such as classification.

Step by step

  1. Tokenise the project corpus into sentences of tokens.
  2. Train Word2Vec with the settings above.
  3. Print the nearest neighbours of ten words and judge them.
  4. Retrain with half the data and a different seed and compare.

Practice activity

Train and evaluate Word2Vec on the project corpus and write a short stability note.

Expected result: A trained model, a neighbour table and an honest note on stability with small data.

Check your understanding

  1. What does min_count=2 do?

    Show answer

    It ignores words that appear fewer than two times.

  2. Why are embeddings noisy on small corpora?

    Show answer

    There are too few contexts to learn reliable vectors.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.