Path: blob/main/a2/a2_latex_template/a2_latex_template.tex
1003 views
\documentclass{article}12\newif\ifanswers3\answerstrue % comment out to hide answers45\usepackage[compact]{titlesec}6\usepackage{fancyhdr} % Required for custom headers7\usepackage{lastpage} % Required to determine the last page for the footer8\usepackage{extramarks} % Required for headers and footers9\usepackage[usenames,dvipsnames]{color} % Required for custom colors10\usepackage{graphicx} % Required to insert images11\usepackage{listings} % Required for insertion of code12\usepackage{courier} % Required for the courier font13\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template14\usepackage{enumerate}15\usepackage{enumitem}16\usepackage{subfigure}17\usepackage{booktabs}18\usepackage{amsmath, amsthm, amssymb}19\usepackage{caption}20\usepackage{hyperref}21\captionsetup[table]{skip=4pt}22\usepackage{framed}23\usepackage{bm}24\usepackage{minted}25\usepackage{soul}2627\usepackage{tikz}28\usetikzlibrary{positioning,patterns,fit}2930% Margins31\topmargin=-0.45in32\evensidemargin=0in33\oddsidemargin=0in34\textwidth=6.5in35\textheight=9.0in36\headsep=0.25in3738\linespread{1.1} % Line spacing3940% Set up the header and footer41\pagestyle{fancy}42\rhead{\hmwkAuthorName} % Top left header43\lhead{\hmwkClass: \hmwkTitle} % Top center head44\lfoot{\lastxmark} % Bottom left footer45\cfoot{} % Bottom center footer46\rfoot{Page\ \thepage\ of\ \protect\pageref{LastPage}} % Bottom right footer47\renewcommand\headrulewidth{0.4pt} % Size of the header rule48\renewcommand\footrulewidth{0.4pt} % Size of the footer rule4950\setlength\parindent{0pt} % Removes all indentation from paragraphs5152\newenvironment{answer}{53% Uncomment this if using the template to write out your solutions.54{\bf Answer:} \sf \begingroup\color{red}55}{\endgroup}%56%----------------------------------------------------------------------------------------57% CODE INCLUSION CONFIGURATION58%----------------------------------------------------------------------------------------5960\definecolor{MyDarkGreen}{rgb}{0.0,0.4,0.0} % This is the color used for comments61\definecolor{shadecolor}{gray}{0.9}6263\lstloadlanguages{Python} % Load Perl syntax for listings, for a list of other languages supported see: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf64\lstset{language=Python, % Use Perl in this example65frame=single, % Single frame around code66basicstyle=\footnotesize\ttfamily, % Use small true type font67keywordstyle=[1]\color{Blue}\bf, % Perl functions bold and blue68keywordstyle=[2]\color{Purple}, % Perl function arguments purple69keywordstyle=[3]\color{Blue}\underbar, % Custom functions underlined and blue70identifierstyle=, % Nothing special about identifiers71commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MyDarkGreen}\small, % Comments small dark green courier font72stringstyle=\color{Purple}, % Strings are purple73showstringspaces=false, % Don't put marks in string spaces74tabsize=5, % 5 spaces per tab75%76% Put standard Perl functions not included in the default language here77morekeywords={rand},78%79% Put Perl function parameters here80morekeywords=[2]{on, off, interp},81%82% Put user defined functions here83morekeywords=[3]{test},84%85morecomment=[l][\color{Blue}]{...}, % Line continuation (...) like blue comment86numbers=left, % Line numbers on left87firstnumber=1, % Line numbers start with line 188numberstyle=\tiny\color{Blue}, % Line numbers are blue and small89stepnumber=5 % Line numbers go in steps of 590}9192% Creates a new command to include a perl script, the first parameter is the filename of the script (without .pl), the second parameter is the caption93\newcommand{\perlscript}[2]{94\begin{itemize}95\item[]\lstinputlisting[caption=#2,label=#1]{#1.pl}96\end{itemize}97}9899%----------------------------------------------------------------------------------------100% NAME AND CLASS SECTION101%----------------------------------------------------------------------------------------102103\newcommand{\hmwkTitle}{word2vec (49 Points)} % Assignment title104\newcommand{\hmwkClass}{CS\ 224n Assignment \#2} % Course/class105%\newcommand{\hmwkAuthorName}{Abigail See, Sahil Chopra} % Your name106107\newcommand{\ifans}[1]{\ifanswers \color{red} \vspace{5mm} \textbf{Solution: } #1 \color{black} \vspace{5mm} \fi}108109% Chris' notes110\definecolor{CMpurple}{rgb}{0.6,0.18,0.64}111\newcommand\cm[1]{\textcolor{CMpurple}{\small\textsf{\bfseries CM\@: #1}}}112\newcommand\cmm[1]{\marginpar{\small\raggedright\textcolor{CMpurple}{\textsf{\bfseries CM\@: #1}}}}113114%----------------------------------------------------------------------------------------115% TITLE PAGE116%----------------------------------------------------------------------------------------117\title{118\vspace{-1in}119\textmd{\textbf{\hmwkClass:\ \hmwkTitle} \\ \hmwkAuthorName}\\120}121\author{}122%\date{\textit{\small Updated \today\ at \currenttime}} % Insert date here if you want it to appear below your name123\date{}124125\setcounter{section}{0} % one-indexing126\begin{document}127128\maketitle129\vspace{-.7in}130131\begin{center}132\large{\textbf{Due on} Tuesday Jan. 24, 2023 by \textbf{4:30pm (before class)}}133\end{center}134135\section{Written: Understanding word2vec (31 points)}136Recall that the key insight behind {\tt word2vec} is that \textit{`a word is known by the company it keeps'}. Concretely, consider a `center' word $c$ surrounded before and after by a context of a certain length. We term words in this contextual window `outside words' ($O$). For example, in Figure~\ref{fig:word2vec}, the context window length is 2, the center word $c$ is `banking', and the outside words are `turning', `into', `crises', and `as':137138\begin{figure}[h]139\centering140\includegraphics[width=0.6\textwidth]{word2vec.png}141\caption{The word2vec skip-gram prediction model with window size 2}142\label{fig:word2vec}143\end{figure}144145Skip-gram {\tt word2vec} aims to learn the probability distribution $P(O|C)$.146Specifically, given a specific word $o$ and a specific word $c$, we want to predict $P(O=o|C=c)$: the probability that word $o$ is an `outside' word for $c$ (i.e., that it falls within the contextual window of $c$).147We model this probability by taking the softmax function over a series of vector dot-products: % I added the word "softmax" here because I bet a lot of students will have forgotten what softmax is and why the loss fn is called naive softmax. but if this is too wordy we can just take it out148149\begin{equation}150P(O=o \mid C=c) = \frac{\exp(\bm u_{o}^\top \bm v_c)}{\sum_{w \in \text{Vocab}} \exp(\bm u_{w}^\top \bm v_c)}151\label{word2vec_condprob}152\end{equation}153154For each word, we learn vectors $u$ and $v$, where $\bm u_o$ is the `outside' vector representing outside word $o$, and $\bm v_c$ is the `center' vector representing center word $c$.155We store these parameters in two matrices, $\bm U$ and $\bm V$.156The columns of $\bm U$ are all the `outside' vectors $\bm u_{w}$;157the columns of $\bm V$ are all of the `center' vectors $\bm v_{w}$.158Both $\bm U$ and $\bm V$ contain a vector for every $w \in \text{Vocabulary}$.\footnote{Assume that every word in our vocabulary is matched to an integer number $k$. Bolded lowercase letters represent vectors. $\bm u_{k}$ is both the $k^{th}$ column of $\bm U$ and the `outside' word vector for the word indexed by $k$. $\bm v_k$ is both the $k^{th}$ column of $\bm V$ and the `center' word vector for the word indexed by $k$. \textbf{In order to simplify notation we shall interchangeably use $k$ to refer to word $k$ and the index of word $k$.}}\newline159160%We can think of the probability distribution $P(O|C)$ as a prediction function that we can approximate via supervised learning. For any training example, we will have a single $o$ and $c$. We will then compute a value $P(O=o|C=c)$ and report the loss.161Recall from lectures that, for a single pair of words $c$ and $o$, the loss is given by:162163\begin{equation}164\bm J_{\text{naive-softmax}}(\bm v_c, o, \bm U) = -\log P(O=o| C=c).165\label{naive-softmax}166\end{equation}167168We can view this loss as the cross-entropy\footnote{The \textbf{cross-entropy loss} between the true (discrete) probability distribution $p$ and another distribution $q$ is $-\sum_i p_i \log(q_i)$.} between the true distribution $\bm y$ and the predicted distribution $\hat{\bm y}$, for a particular center word c and a particular outside word o.169Here, both $\bm y$ and $\hat{\bm y}$ are vectors with length equal to the number of words in the vocabulary.170Furthermore, the $k^{th}$ entry in these vectors indicates the conditional probability of the $k^{th}$ word being an `outside word' for the given $c$.171The true empirical distribution $\bm y$ is a one-hot vector with a 1 for the true outside word $o$, and 0 everywhere else, for this particular example of center word c and outside word o.\footnote{Note that the true conditional probability distribution of context words for the entire training dataset would not be one-hot.}172The predicted distribution $\hat{\bm y}$ is the probability distribution $P(O|C=c)$ given by our model in equation (\ref{word2vec_condprob}). \newline173174\textbf{Note:} Throughout this homework, when computing derivatives, please use the method reviewed during the lecture (i.e. no Taylor Series Approximations).175176\clearpage177\begin{enumerate}[label=(\alph*)]178% Question 1-A179\item (2 points)180Prove that the naive-softmax loss (Equation \ref{naive-softmax}) is the same as the cross-entropy loss between $\bm y$ and $\hat{\bm y}$, i.e. (note that $\bm y, \hat{\bm y}$ are vectors and $\hat{\bm y}_o$ is a scalar):181182\begin{equation}183-\sum_{w \in \text{Vocab}} \bm y_w \log(\hat{\bm y}_w) = - \log (\hat{\bm y}_o).184\end{equation}185186Your answer should be one line. You may describe your answer in words.187\begin{shaded}188\begin{answer}189190\end{answer}191\end{shaded}192193% Question 1-B194\item (7 points)195\begin{enumerate}[label=(\roman*)]196\item197Compute the partial derivative of $\bm J_{\text{naive-softmax}}(\bm v_c, o, \bm U)$ with respect to $\bm v_c$. \ul{Please write your answer in terms of $\bm y$, $\hat{\bm y}$, $\bm U$, and show your work to receive full credit}.198\begin{itemize}199\item \textbf{Note}: Your final answers for the partial derivative should follow the shape convention: the partial derivative of any function $f(x)$ with respect to $x$ should have the \textbf{same shape} as $x$.\footnote{This allows us to efficiently minimize a function using gradient descent without worrying about reshaping or dimension mismatching. While following the shape convention, we're guaranteed that $\theta:= \theta - \alpha\frac{\partial J(\theta)}{\partial \theta}$ is a well-defined update rule.}200\item Please provide your answers for the partial derivative in vectorized form. For example, when we ask you to write your answers in terms of $\bm y$, $\hat{\bm y}$, and $\bm U$, you may not refer to specific elements of these terms in your final answer (such as $\bm y_1$, $\bm y_2$, $\dots$).201\end{itemize}202\item203When is the gradient you computed equal to zero? \\204\textbf{Hint:} You may wish to review and use some introductory linear algebra concepts.205\item206The gradient you found is the difference between two terms. Provide an interpretation of how each of these terms improves the word vector when this gradient is subtracted from the word vector $v_c$.207\item208In many downstream applications using word embeddings, L2 normalized vectors (e.g. $\mathbf{u}/||\mathbf{u}||_2$ where $||\mathbf{u}||_2 = \sqrt{\sum_i u_i^2}$) are used instead of their raw forms (e.g. $\mathbf{u}$). Now, suppose you would like to classify phrases as being positive or negative. When would L2 normalization take away useful information for the downstream task? When would it not?209Hint: Consider the case where $\mathbf{u}_x = \alpha\mathbf{u}_y$ for some words $x \neq y$ and some scalar $\alpha$.210211\end{enumerate}212213\begin{shaded}214\begin{answer}215216\end{answer}217\end{shaded}218219% Question 1-C220\item (5 points) Compute the partial derivatives of $\bm J_{\text{naive-softmax}}(\bm v_c, o, \bm U)$ with respect to each of the `outside' word vectors, $\bm u_w$'s. There will be two cases: when $w=o$, the true `outside' word vector, and $w \neq o$, for all other words. Please write your answer in terms of $\bm y$, $\hat{\bm y}$, and $\bm v_c$. In this subpart, you may use specific elements within these terms as well (such as $\bm y_1$, $\bm y_2$, $\dots$). Note that $\bm u_w$ is a vector while $\bm y_1, \bm y_2, \dots$ are scalars. Show your work to receive full credit.221222\begin{shaded}223\begin{answer}224225\end{answer}226\end{shaded}227228% Question 1-D229\item (1 point) Write down the partial derivative of $\bm J_{\text{naive-softmax}}(\bm v_c, o, \bm U)$ with respect to $\bm U$. Please break down your answer in terms of the column vectors $\frac{\partial \bm J(\bm v_c, o, \bm U)}{\partial \bm u_1}$, $\frac{\partial \bm J(\bm v_c, o, \bm U)}{\partial \bm u_2}$, $\cdots$, $\frac{\partial \bm J(\bm v_c, o, \bm U)}{\partial \bm u_{|\text{Vocab}|}}$. No derivations are necessary, just an answer in the form of a matrix.230231\begin{shaded}232\begin{answer}233234\end{answer}235\end{shaded}236237% Question 1-E238\item (2 points) The Leaky ReLU (Leaky Rectified Linear Unit) activation function is given by Equation \ref{LeakyReLU} and Figure~\ref{fig:leaky_relu}:239\begin{equation}240\label{LeakyReLU}241f(x) = \max(\alpha x, x)242\end{equation}243244\begin{figure}[h]245\centering246\includegraphics[width=0.3\textwidth]{leaky_relu_graph.png}247\caption{Leaky ReLU}248\label{fig:leaky_relu}249\end{figure}250251Where $x$ is a scalar and $0<\alpha <1$, please compute the derivative of $f(x)$ with respect to $x$. You may ignore the case where the derivative is not defined at 0.\footnote{If you're interested in how to handle the derivative at this point, you can read more about the notion of \hyperref[https://en.wikipedia.org/wiki/Subderivative]{subderivatives}.}252253\begin{shaded}254\begin{answer}255256\end{answer}257\end{shaded}258259% Question 1-F260\item (3 points) The sigmoid function is given by Equation \ref{Sigmoid Function}:261262\begin{equation}263\label{Sigmoid Function}264\sigma (x) = \frac{1}{1 + e^{-x}} = \frac{e^{x}}{e^{x} + 1}265\end{equation}266267Please compute the derivative of $\sigma(x)$ with respect to $x$, where $x$ is a scalar. Please write your answer in terms of $\sigma(x)$. Show your work to receive full credit.268269\begin{shaded}270\begin{answer}271272\end{answer}273\end{shaded}274275% Question 1-G276\item (6 points) Now we shall consider the Negative Sampling loss, which is an alternative to the Naive Softmax loss. Assume that $K$ negative samples (words) are drawn from the vocabulary. For simplicity of notation we shall refer to them as $w_1, w_2, \dots, w_K$, and their outside vectors as $\bm u_{w_1}, \bm u_{w_2}, \dots, \bm u_{w_K}$.\footnote{Note: In the notation for parts (g) and (h), we are using words, not word indices, as subscripts for the outside word vectors.} For this question, assume that the $K$ negative samples are distinct. In other words, $i\neq j$ implies $w_i\neq w_j$ for $i,j\in\{1,\dots,K\}$.277Note that $o\notin\{w_1, \dots, w_K\}$.278For a center word $c$ and an outside word $o$, the negative sampling loss function is given by:279280\begin{equation}281\bm J_{\text{neg-sample}}(\bm v_c, o, \bm U) = -\log(\sigma(\bm u_o^\top \bm v_c)) - \sum_{s=1}^K \log(\sigma(-\bm u_{w_s}^\top \bm v_c))282\end{equation}283for a sample $w_1, \ldots w_K$, where $\sigma(\cdot)$ is the sigmoid function.\footnote{Note: The loss function here is the negative of what Mikolov et al.\ had in their original paper, because we are doing a minimization instead of maximization in our assignment code. Ultimately, this is the same objective function.}284285\begin{enumerate}[label=(\roman*)]286\item Please repeat parts (b) and (c), computing the partial derivatives of $\bm J_{\text{neg-sample}}$ with respect to $\bm v_c$, with respect to $\bm u_o$, and with respect to the $s^{th}$ negative sample $\bm u_{w_s}$. Please write your answers in terms of the vectors $\bm v_c$, $\bm u_o$, and $\bm u_{w_s}$, where $s \in [1, K]$. Show your work to receive full credit. \textbf{Note:} you should be able to use your solution to part (f) to help compute the necessary gradients here.287288\item In lecture, we learned that an efficient implementation of backpropagation leverages the re-use of previously-computed partial derivatives. Which quantity could you reuse amongst the three partial derivatives calculated above to minimize duplicate computation? Write your answer in terms of \\ $\bm{U}_{o, \{w_1, \dots, w_K\}} = \begin{bmatrix} \bm{u}_o, -\bm{u}_{w_1}, \dots, -\bm{u}_{w_K} \end{bmatrix}$, a matrix with the outside vectors stacked as columns, and $\bm{1}$, a $(K + 1) \times 1$ vector of 1's.\footnote{Note: NumPy will automatically broadcast 1 to a vector of 1's if the computation requires it, so you generally don't have to construct $\bm{1}$ on your own during implementation.}289Additional terms and functions (other than $\bm{U}_{o, \{w_1, \dots, w_K\}}$ and $\bm{1}$) can be used in your solution.290\item Describe with one sentence why this loss function is much more efficient to compute than the naive-softmax loss.291\end{enumerate}292293Caveat: So far we have looked at re-using quantities and approximating softmax with sampling for faster gradient descent. Do note that some of these optimizations might not be necessary on modern GPUs and are, to some extent, artifacts of the limited compute resources available at the time when these algorithms were developed.294295\begin{shaded}296\begin{answer}297298\end{answer}299\end{shaded}300301% Question 1-H302\item (2 points) Now we will repeat the previous exercise, but without the assumption that the $K$ sampled words are distinct. Assume that $K$ negative samples (words) are drawn from the vocabulary. For simplicity of notation we shall refer to them as $w_1, w_2, \dots, w_K$ and their outside vectors as $\bm u_{w_1}, \dots, \bm u_{w_K}$. In this question, you may not assume that the words are distinct. In other words, $w_i=w_j$ may be true when $i\neq j$ is true.303Note that $o\notin\{w_1, \dots, w_K\}$.304For a center word $c$ and an outside word $o$, the negative sampling loss function is given by:305306\begin{equation}307\bm J_{\text{neg-sample}}(\bm v_c, o, \bm U) = -\log(\sigma(\bm u_o^\top \bm v_c)) - \sum_{s=1}^K \log(\sigma(-\bm u_{w_s}^\top \bm v_c))308\end{equation}309for a sample $w_1, \ldots w_K$, where $\sigma(\cdot)$ is the sigmoid function.310311Compute the partial derivative of $\bm J_{\text{neg-sample}}$ with respect to a negative sample $\bm u_{w_s}$. Please write your answers in terms of the vectors $\bm v_c$ and $\bm u_{w_s}$, where $s \in [1, K]$. Show your work to receive full credit. Hint: break up the sum in the loss function into two sums: a sum over all sampled words equal to $w_s$ and a sum over all sampled words not equal to $w_s$. Notation-wise, you may write `equal' and `not equal' conditions below the summation symbols, such as in Equation \ref{skip-gram}.312313\begin{shaded}314\begin{answer}315316\end{answer}317\end{shaded}318319% Question 1-I320\item (3 points) Suppose the center word is $c = w_t$ and the context window is $[w_{t-m}$, $\ldots$, $w_{t-1}$, $w_{t}$, $w_{t+1}$, $\ldots$, $w_{t+m}]$, where $m$ is the context window size. Recall that for the skip-gram version of {\tt word2vec}, the total loss for the context window is:321\begin{equation}322\label{skip-gram}323\bm J_{\textrm{skip-gram}}(\bm v_c, w_{t-m},\ldots w_{t+m}, \bm U) = \sum_{\substack{-m\le j \le m \\ j\ne 0}} \bm J(\bm v_c, w_{t+j}, \bm U)324\end{equation}325326Here, $\bm J(\bm v_c, w_{t+j}, \bm U)$ represents an arbitrary loss term for the center word $c=w_t$ and outside word $w_{t+j}$. $\bm J(\bm v_c, w_{t+j}, \bm U)$ could be $\bm J_{\text{naive-softmax}}(\bm v_c, w_{t+j}, \bm U)$ or $\bm J_{\text{neg-sample}}(\bm v_c, w_{t+j}, \bm U)$, depending on your implementation.327328Write down three partial derivatives:329\begin{enumerate}[label=(\roman*)]330\item ${\frac{\partial \bm J_{\textrm{skip-gram}}(\bm v_c, w_{t-m},\ldots w_{t+m}, \bm U)} {\partial \bm U}}$331\item ${\frac{\partial \bm J_{\textrm{skip-gram}}(\bm v_c, w_{t-m},\ldots w_{t+m}, \bm U)} {\partial \bm v_c}}$332\item ${\frac{\partial \bm J_{\textrm{skip-gram}}(\bm v_c, w_{t-m},\ldots w_{t+m}, \bm U)} {\partial \bm v_w}}$ when $w \ne c$333\end{enumerate}334Write your answers in terms of ${\frac{\partial \bm J(\bm v_c, w_{t+j}, \bm U)}{\partial \bm U}}$ and ${\frac{\partial \bm J(\bm v_c, w_{t+j}, \bm U)}{\partial \bm v_c}}$. This is very simple -- each solution should be one line.335336\begin{shaded}337\begin{answer}338339\end{answer}340\end{shaded}341342\textit{\textbf{Once you're done:} Given that you computed the derivatives of $\bm J(\bm v_c, w_{t+j}, \bm U)$ with respect to all the model parameters $\bm U$ and $\bm V$ in parts (a) to (c), you have now computed the derivatives of the full loss function $\bm J_{\text{skip-gram}}$ with respect to all parameters. You're ready to implement \texttt{word2vec}!} % we could remove this line but I added it to make sure they understand why we did all this.343344\end{enumerate}345346\section{Coding: Implementing word2vec (18 points)}347In this part you will implement the word2vec model and train your own word vectors with stochastic gradient descent (SGD). Before you begin, first run the following commands within the assignment directory in order to create the appropriate conda virtual environment. This guarantees that you have all the necessary packages to complete the assignment. \textbf{Windows users} may wish to install the Linux Windows Subsystem\footnote{https://techcommunity.microsoft.com/t5/windows-11/how-to-install-the-linux-windows-subsystem-in-windows-11/m-p/2701207}. Also note that you probably want to finish the previous math section before writing the code since you will be asked to implement the math functions in Python. You’ll probably want to implement and test each part of this section in order, since the questions are cumulative.348349\begin{minted}{bash}350conda env create -f env.yml351conda activate a2352\end{minted}353354Once you are done with the assignment you can deactivate this environment by running:355\begin{minted}{bash}356conda deactivate357\end{minted}358359For each of the methods you need to implement, we included approximately how many lines of code our solution has in the code comments. These numbers are included to guide you. You don't have to stick to them, you can write shorter or longer code as you wish. If you think your implementation is significantly longer than ours, it is a signal that there are some \texttt{numpy} methods you could utilize to make your code both shorter and faster. \texttt{for} loops in Python take a long time to complete when used over large arrays, so we expect you to utilize \texttt{numpy} methods. We will be checking the efficiency of your code. You will be able to see the results of the autograder when you submit your code to \texttt{Gradescope}, we recommend submitting early and often.360361Note: If you are using Windows and have trouble running the .sh scripts used in this part, we recommend trying \href{https://github.com/bmatzelle/gow}{Gow} or manually running commands in the scripts.362363\begin{enumerate}[label=(\alph*)]364% Question 2-A365\item (12 points) We will start by implementing methods in \texttt{word2vec.py}. You can test a particular method by running \texttt{python word2vec.py m} where \texttt{m} is the method you would like to test. For example, you can test the sigmoid method by running \texttt{python word2vec.py sigmoid}.366367\begin{enumerate}[label=(\roman*)]368\item Implement the \texttt{sigmoid} method, which takes in a vector and applies the sigmoid function to it.369\item Implement the softmax loss and gradient in the \texttt{naiveSoftmaxLossAndGradient} method.370\item Implement the negative sampling loss and gradient in the \texttt{negSamplingLossAndGradient} method.371\item Implement the skip-gram model in the \texttt{skipgram} method.372\end{enumerate}373374When you are done, test your entire implementation by running \texttt{python word2vec.py}.375376% Question 2-B377\item (4 points) Complete the implementation for your SGD optimizer in the \texttt{sgd} method of \texttt{sgd.py}. Test your implementation by running \texttt{python sgd.py}.378379% Question 2-C380\item (2 points) Show time! Now we are going to load some real data and train word vectors with everything you just implemented! We are going to use the Stanford Sentiment Treebank (SST) dataset to train word vectors, and later apply them to a simple sentiment analysis task. You will need to fetch the datasets first. To do this, run \texttt{sh get\_datasets.sh}. There is no additional code to write for this part; just run \texttt{python run.py}.381382\emph{Note: The training process may take a long time depending on the efficiency of your implementation and the compute power of your machine \textbf{(an efficient implementation takes one to two hours)}. Plan accordingly!}383384After 40,000 iterations, the script will finish and a visualization for your word vectors will appear. It will also be saved as \texttt{word\_vectors.png} in your project directory. \textbf{Include the plot in your homework write up.} In at most three sentences, briefly explain what you see in the plot. This may include, but is not limited to, observations on clusters and words that you expect to cluster but do not.385386\begin{shaded}387\begin{answer}388389\end{answer}390\end{shaded}391392\section{Submission Instructions}393You shall submit this assignment on Gradescope as two submissions -- one for ``Assignment 2 [coding]" and another for `Assignment 2 [written]":394\begin{enumerate}395\item Run the \texttt{collect\_submission.sh} script to produce your \texttt{assignment2.zip} file.396\item Upload your \texttt{assignment2.zip} file to Gradescope to ``Assignment 2 [coding]".397\item Upload your written solutions to Gradescope to ``Assignment 2 [written]".398\end{enumerate}399400\end{enumerate}401\end{document}402403404