2017-08-12 85 views
4

我不明白Keras的嵌入層。雖然有很多文章解釋它,但我仍然感到困惑。例如,從IMDB情感分析用例子說明:如何在keras中嵌入圖層

top_words = 5000 
max_review_length = 500 
embedding_vecor_length = 32  

model = Sequential() 
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length)) 
model.add(LSTM(100)) 
model.add(Dense(1, activation='sigmoid')) 
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 
print(model.summary()) 
model.fit(X_train, y_train, nb_epoch=3, batch_size=64) 

下面的代碼在這段代碼,完全嵌入層做什麼,什麼是埋層的輸出,這將是很好,如果有人可以用一些例子來解釋它可能是!!

+1

可能重複[什麼是在Keras中嵌入?](https://stackoverflow.com/questions/38189713/what-is-an-embedding-in-keras) – DJK

+0

它與theano解釋但它會更容易通過keras中的示例來了解 – user1670773

+0

層的數學遵循相同的原則。 – DJK

回答

2

嵌入層從輸入單詞中創建嵌入向量(我自己仍然不理解數學),就像word2vec或預先計算的手套一樣。

在開始您的代碼之前,我們來舉個簡單的例子。

texts = ['This is a text','This is not a text'] 

首先,我們把這些句子成整數的向量,其中每個字是分配給在所述載體的字典和順序字的數創建的字序列。

from keras.preprocessing.text import Tokenizer 
from keras.preprocessing.sequence import pad_sequences 
from keras.utils import to_categorical 

max_review_length = 6 #maximum length of the sentence 
embedding_vecor_length = 3 
top_words = 10 

#num_words is tne number of unique words in the sequence, if there's more top count words are taken 
tokenizer = Tokenizer(top_words) 
tokenizer.fit_on_texts(texts) 
sequences = tokenizer.texts_to_sequences(texts) 
word_index = tokenizer.word_index 
input_dim = len(word_index) + 1 
print('Found %s unique tokens.' % len(word_index)) 

#max_review_length is the maximum length of the input text so that we can create vector [... 0,0,1,3,50] where 1,3,50 are individual words 
data = pad_sequences(sequences, max_review_length) 

print('Shape of data tensor:', data.shape) 
print(data) 

[Out:] 
'This is a text' --> [0 0 1 2 3 4] 
'This is not a text' --> [0 1 2 5 3 4] 

現在可以輸入到這些埋入層

from keras.models import Sequential 
from keras.layers import Embedding 

model = Sequential() 
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length,mask_zero=True)) 
model.compile(optimizer='adam', loss='categorical_crossentropy') 
output_array = model.predict(data) 

output_array包含尺寸的陣列(2,6,3):在我的情況2個輸入評論或句子,圖6是最大數在每個評論(max_review_length)和3是embedding_vecor_length。 例如

array([[[-0.01494285, -0.007915 , 0.01764857], 
    [-0.01494285, -0.007915 , 0.01764857], 
    [-0.03019481, -0.02910612, 0.03518577], 
    [-0.0046863 , 0.04763055, -0.02629668], 
    [ 0.02297204, 0.02146662, 0.03114786], 
    [ 0.01634104, 0.02296363, -0.02348827]], 

    [[-0.01494285, -0.007915 , 0.01764857], 
    [-0.03019481, -0.02910612, 0.03518577], 
    [-0.0046863 , 0.04763055, -0.02629668], 
    [-0.01736645, -0.03719328, 0.02757809], 
    [ 0.02297204, 0.02146662, 0.03114786], 
    [ 0.01634104, 0.02296363, -0.02348827]]], dtype=float32) 

你的情況,你有5000個單詞的列表,它可以創造的最大500個字的評論(更會被剪掉),並把每一種500個字成大小的矢量32

你可以通過運行得到了這個詞索引和嵌入矢量之間的映射:

model.layers[0].get_weights() 

在下面top_words的情況下爲10,所以我們有10個字的映射,你可以看到該映射0,1,2,3, 4和5等於上面的output_array。

[array([[-0.01494285, -0.007915 , 0.01764857], 
    [-0.03019481, -0.02910612, 0.03518577], 
    [-0.0046863 , 0.04763055, -0.02629668], 
    [ 0.02297204, 0.02146662, 0.03114786], 
    [ 0.01634104, 0.02296363, -0.02348827], 
    [-0.01736645, -0.03719328, 0.02757809], 
    [ 0.0100757 , -0.03956784, 0.03794377], 
    [-0.02672029, -0.00879055, -0.039394 ], 
    [-0.00949502, -0.02805768, -0.04179233], 
    [ 0.0180716 , 0.03622523, 0.02232374]], dtype=float32)] 

https://stats.stackexchange.com/questions/270546/how-does-keras-embedding-layer-work提到的這些向量被髮起隨機和優化由netword優化就像網絡的任何其它參數。