CS224N Assignment 1

新坑,本人水平有限,如有錯誤,歡迎指出
Q1.1
主要步驟是對列表的列表進行扁平化,後用set去重

    corpus_words=list(set([word for sublist in corpus for word in sublist]))
    corpus_words.sort()
    num_corpus_words=len(corpus_words)

Q1.2
注意在進行單詞計數時只需讓W[worda,wordb]加1即可,不需要同時讓W[wordb,worda]加一

    M=np.zeros((num_words,num_words))
    index=0
    for word in words:
        word2Ind[word]=index
        index+=1
    for sen in corpus:
        sen_len=len(sen)
        for cur in range(sen_len):
            min=cur-window_size
            max=cur+window_size
            if(min<0):
                min=0
            if(max>=sen_len):
                max=sen_len-1
            for slide_index in range(min,max+1):
                if (slide_index==cur):
                    continue
                M[word2Ind[sen[slide_index]],word2Ind[sen[cur]]]+=1
        

Q1.3

    svd=TruncatedSVD(k)
    M_reduced=svd.fit_transform(M)

Q1.4

    for word in words:
        plt.scatter(M_reduced[word2Ind[word]][0],M_reduced[word2Ind[word]][1])
    return

之後的題目都是開放題,我就不放代碼了,大致講一下Q2.4的思路
這題是想找出兩個單詞的類比關係,比如例子中是男人之於國王類似於女人之於女王,所以在positive中爲女人和國王,爲的是讓結果既要和女人相關,也要和男人的類比結果–國王相關,而在negative裏爲男人,是想讓結果和男人儘可能無關

拓展閱讀:
SKIP-GRAM 論文:https://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf
hierarchical softmax:https://blog.csdn.net/itplus/article/details/37969817

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章