Learning about Word Vector Representations and Deep Learning through Implementing Word2vec (2021)

Abstract: Word vector representations are an essential part of an NLP curriculum. Here, we describe a homework that has students implement a popular method for learning word vectors, word2vec. Students implement the core parts of the method, including text preprocessing, negative sampling, and gradient descent. Starter code provides guidance and handles basic operations, which allows students to focus on the conceptually challenging aspects. After generating their vectors, students evaluate them using qualitative and quantitative tests.

## introduction nlp curricula typically include content on word semantics, how semantics can be learned computationally through word vectors, and what are the vectors' uses. this document describes an assignment for having students implement word2vec @xcite , a popular method that relies on a single-layer neural network. this homework is designed to introduce students to word vectors and simple neural networks by having them implement the network from scratch, without the use of deeplearning libraries. the assignment is appropriate for upper-division undergraduates or graduate students who are familiar with python programming, have some experience with the numpy library @xcite , and have been exposed to concepts around gradients and neural networks. through implementing major portions of the word2vec software and using the learned vectors, students will gain a deeper understanding of how networks are trained, how to learn word vectors, and their uses in downstream tasks. ## design and learning goals this homework is designed to take place just before the middle stretch of the class, after lexical semantics and machine learning concepts have been introduced. the content is designed at the level of an nlp student who (1) has some technical background and at least one advanced course in statistics and (2) will implement or adapt new nlp methods. this level is deeper than what is needed for a purely applied nlp setting but too shallow for a more machine learning focused nlp class, which would likely benefit from additional derivations and proofs around the gradient descent to solidify understanding. the homework has typically been assigned over a three to four week period; many students complete the homework in the course of a week, but the longer time frame enables students with less background or programming experience to work through the steps. the material prepares students for advanced nlp concepts around deep learning and pre-trained language models, as well as provides intuition for what steps modern deep learning libraries perform. the homework has three broad learning goals. first, the training portion of the homework helps deepen students' understanding of machine learning concepts, gradient descent, and develop complex nlp software. central to this design is having students turn the equations in the homework and formal descriptions of word2vec into software operations. this step helps students understand how to ground equations found in some papers into the more-familiar language of programming, while also building a more intuition for how gradient descent and backpropagation work in practice. second, the process of software development aids students in developing larger nlp software methods that involve end-to-end development. this goal includes seeing how different algorithmic software designs work and are implemented. the speed of training requires that students be moderately efficient in how they implement their software. for example, the use of for loops instead of vectorized numpy operations will lead to a significant slow down in performance. in class instruction and tutorials detail how to write the relevant efficient numerical operations, which help guide students to identify where and how to selectively optimize. however, slow code will still finish correctly allowing students to debug for their initial implementations for correctness. this need for performant code creates opportunities for students to practice their performance optimizing skills. third, the lexical semantics portion of the homework exposes students to the uses and limitations of word vectors. through training the vectors, students understand how statistical regularities in cooccurrence can be used to learn meaning. qualitative and quantitative evaluations show students what their model has learned (e.g., using vector analogies) and introduce them to concepts of polysemy, fostering a larger discussion on what can be captured in a vector representation. ## homework description the homework has students implement two core aspects of the word2vec algorithm using numpy for the numeric portions, and then evaluate with two downstream tasks. the first aspect has students perform the commonly-used text preprocessing steps that turn a raw text corpus into self-supervised training examples. this step includes removing lowfrequency tokens and subsampling tokens based on their frequency. the second aspect focuses on the core training procedure, including (i) negative sampling for generating negative examples of context words, (ii) performing gradient descent to update the two word vector matrices, and (iii) computing the negative log-likelihood. these tasks are broken into eight discrete steps that guide students in how to do each aspect. the assignment document includes links to more in-depth descriptions of the method including the extensive description of @xcite and the recent chapter of jurafsky and martin (2021, ch. 6) to help students understand the math behind the training procedure. in the second part of the homework, students evaluate the learned vectors in two downstream tasks. the first task has students load these vectors using the gensim package @xcite and perform vector arithmetic operations to find word-pair analogies and examine the nearest-neighbors of words; this qualitative evaluation exposes students to what is or is not learned by the model. the second task is quantitative evaluation that has students generate word-pair similarity scores for the subset of the simlex-999 @xcite present in their training corpus, which is uploaded to kaggle inclass foot_0 to see how their vectors compare with others; this leaderboard helps students identify a bug in their code (via a low-scoring submission) and occasionally prompts students to think about how to improve/extend their code to attain a higher score. potential extensions the word2vec method has been extended in numerous ways in nlp to improve its vectors (e.g., @xcite @xcite . this assignment includes descriptions of other possible extensions that students can explore, such as implementing dropout, adding learning rate decay, or making use of external knowledge during training. typically, a single extension to word2vec is included as a part of the homework to help ground the concept in code but without increasing the difficulty of the assignment. students who are interested in deepening their understanding can use these as starting points to see how to develop their own nlp methods as a part of a course project. this assignment also provides multiple possibilities for examining the latent biases learned in word vectors. prior work has established that pretrained vectors often encode gender and racial biases based on the corpora they are trained on (e.g., @xcite . in a future extension, this assignment could be adapted to use wikipedia biographies as a base corpus and have students identify how occupations become more associated with gendered words during training @xcite . once this bias is discovered, students can discuss various methods for mitigating it (e.g., @xcite and how their method might be adapted to avoid other forms of bias. this extension can help students critically think about what is and is not being captured in pretrained vectors and models. ## reflection on student experiences student experiences on this homework have been very positive, with multiple students expressing a strong sense of satisfaction at completing the homework and being able to understand the algorithm and software backing word2vec. several students reported feeling like completing this assignment was a great confidence boost and that they were now more confident in their ability to understand nlp papers and connect algorithms, equations, and code. the majority of student difficulties happen in two sources. first, the vast majority of bugs happen when implementing the gradient descent and calculating the negative log-likelihood (nll). while only a few lines of code in total, this step requires translating the loss function for word2vec (in the negative sampling case) into numpy code. this translation task appeared daunting at first for many students, though they found creating the eventual solution rewarding for being able to ground similar equations in nlp papers. two key components for mitigating early frustration were (1) including built-in periodic reports of the nll, which help students quickly spot whether there are numeric errors that lead to infinity or nan values and (2) adding early-stopping and printing nearest neighbors of instructor-provided words (e.g., "january") which should be thematically coherent after only a few minutes of training. these components help students quickly identify the presence of a bug in the gradient descent. the second student difficulty comes from the text preprocessing steps. the removal of lowfrequency words and frequency-based subsampling steps require students to have a solid distinction of type versus token in practice in order to subsample tokens (versus types). i suspect that because many of these routine preprocessing steps are done for the student by common libraries (e.g., the countvectorizer of scikit learn @xcite ), these steps feel unfamiliar. common errors in this theme were subsampling types or producing a sequence of word types (rather than tokens) to use for training.