embedding 层的详细解释
生活随笔
收集整理的這篇文章主要介紹了
embedding 层的详细解释
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原文鏈接
在這篇文章中,我們將介紹keras的嵌入層。為此,我創建了一個僅包含3個文檔的樣本語料庫,這足以解釋keras嵌入層的工作。
詞嵌入在各種機器學習應用程序中很有用在開始之前,讓我們瀏覽一下詞嵌入的一些應用:
-
第一個吸引我的應用程序是在基于協同過濾的推薦系統中,我們必須通過分解包含用戶項等級的效用矩陣來創建用戶嵌入和電影嵌入。
要查看有關在Keras中使用詞嵌入的基于CF推薦系統的完整教程,可以遵循我的這篇文章。 -
第二種用途是在自然語言處理及其相關應用程序中,我們必須為語料庫文檔中存在的所有單詞創建單詞嵌入。這是我將在此內核中使用的術語。
-
因此,當我們想要創建將高維數據嵌入到低維向量空間中的嵌入時,可以使用Keras中的嵌入層。
此后,所有唯一詞都將由一個整數表示。 為此,我們使用Keras中的one_hot函數。 請注意,vocab_size被指定為足夠大,以確保每個單詞的唯一整數編碼。
注意一件重要的事情,即單詞的整數編碼在不同文檔中保持不變。 例如,“butter”在每個文檔中都用31表示。
指定詞向量的長度
vocab_size=50 encod_corp=[] for i,doc in enumerate(corp):encod_corp.append(one_hot(doc,50))# print(one_hot(doc,50))print("The encoding for document",i+1," is : ",one_hot(doc,50)) # length of maximum document. will be nedded whenever create embeddings for the words maxlen=-1 for doc in corp:tokens=nltk.word_tokenize(doc)if(maxlen<len(tokens)):maxlen=len(tokens) print("The maximum number of words in any document is : ",maxlen)Keras嵌入層要求所有單個文檔的長度都相同。 因此,我們現在將較短的文檔填充0。 因此,現在在Keras嵌入層中,“ input_length”將等于具有最大長度或最大單詞數的文檔的長度(即單詞數)。
為了填充較短的文檔,我使用Keras庫中的pad_sequences函數。
# now to create embeddings all of our docs need to be of same length. hence we can pad the docs with zeros. pad_corp=pad_sequences(encod_corp,maxlen=maxlen,padding='post',value=0.0) print("No of padded documents: ",len(pad_corp))現在所有文檔的長度相同(填充后)。 因此,現在我們可以創建和使用嵌入了。我將這些詞嵌入8維向量中。
# specifying the input shape # input=Input(shape=(no_docs,maxlen),dtype='float64')""" 嵌入層的參數--- 'input_dim'=我們將選擇的單詞集合大小。 換句話說,這是詞匯中唯一詞的數量。 “ output_dim” =我們希望嵌入的尺寸數。 每個單詞都將由一個如此大小的向量表示。 'input_length'=最大文檔的長度。 在我們的例子中,它存儲在maxlen變量中。 """''' shape of input. each document has 12 element or words which is the value of our maxlen variable.''' word_input=Input(shape=(maxlen,),dtype='float64') # creating the embedding word_embedding=Embedding(input_dim=vocab_size,output_dim=8,input_length=maxlen)(word_input)word_vec=Flatten()(word_embedding) # flatten embed_model =Model([word_input],word_embedding) # combining all into a Keras model embed_model.summary()embed_model.compile(optimizer=keras.optimizers.Adam(lr=1e-3),loss='binary_crossentropy',metrics=['acc']) # compiling the model. parameters can be tuned as always.print(type(word_embedding)) print(word_embedding)embeddings=embed_model.predict(pad_corp) # finally getting the embeddings.""" 結果形狀為(3,12,8)。 3 --->文件的數量 12->每個文件由12個字組成,這是我們所有文件的最大長度。 &8 --->每個單詞都是8維的。 """ print("Shape of embeddings : ",embeddings.shape) print(embeddings)embeddings=embeddings.reshape(-1,maxlen,8) print("Shape of embeddings : ",embeddings.shape) print(embeddings)現在,這使我們可以更容易地看到我們有3個文檔,每個文檔包含12個(最大長度)單詞,每個單詞映射到8維向量。
如何處理一段真實的文本
就像上面一樣,我們現在可以使用任何其他文檔。 我們可以將文件send_tokenize變成句子。
每個句子都有一個單詞列表,我們將使用“ one_hot”函數對這些單詞進行整數編碼,如下所示。
現在,每個句子將具有不同數量的單詞。 因此,我們需要將序列填充到最大單詞數的句子中。
此時,我們已經準備好將輸入提供給Keras嵌入層,如上所示。
‘input_dim’=我們將選擇的詞匯表大小
‘output_dim’=我們希望嵌入的尺寸數
‘input_length’=最大文檔長度
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的embedding 层的详细解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python filter
- 下一篇: python super 理解(四)