From 4d2f992c777f3cc53ff5217c09de01f64738300e Mon Sep 17 00:00:00 2001 From: abdelrahman wael Date: Mon, 2 Mar 2020 22:56:12 +0200 Subject: [PATCH] skip gram added --- assignment 2/word2vec.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/assignment 2/word2vec.py b/assignment 2/word2vec.py index 9abe5db..6e5faa3 100644 --- a/assignment 2/word2vec.py +++ b/assignment 2/word2vec.py @@ -162,7 +162,6 @@ def negSamplingLossAndGradient( return loss, gradCenterVec, gradOutsideVecs - def skipgram(currentCenterWord, windowSize, outsideWords, word2Ind, centerWordVectors, outsideVectors, dataset, word2vecLossAndGradient=naiveSoftmaxLossAndGradient): @@ -200,6 +199,24 @@ def skipgram(currentCenterWord, windowSize, outsideWords, word2Ind, ### YOUR CODE HERE + center_word_index=word2Ind[currentCenterWord] + center_word_vec=centerWordVectors[center_word_index] + + for outside_word in outsideWords: + + outside_word_index= word2Ind[outside_word] + loss_from_1_word, grad_center_word, grad_outside= word2vecLossAndGradient(centerWordVec=center_word_vec, + outsideWordIdx=outside_word_index,outsideVectors=outsideVectors,dataset=dataset) + loss += loss_from_1_word + + + gradCenterVecs[center_word_index] += grad_center_word + + gradOutsideVectors += grad_outside + + + + ### END YOUR CODE return loss, gradCenterVecs, gradOutsideVectors