keras understanding Word Embedding Layer

谁说胖子不能爱 提交于 2019-12-04 14:49:48

1 - Yes, word unicity is not guaranteed, see the docs:

  • From one_hot: This is a wrapper to the hashing_trick function...
  • From hashing_trick: "Two or more words may be assigned to the same index, due to possible collisions by the hashing function. The probability of a collision is in relation to the dimension of the hashing space and the number of distinct objects."

It would be better to use a Tokenizer for this. (See question 4)

It's very important to remember that you should involve all words at once when creating indices. You cannot use a function to create a dictionary with 2 words, then again with 2 words, then again.... This will create very wrong dictionaries.


2 - Embeddings have the size 50 x 8, because that was defined in the embedding layer:

Embedding(vocab_size, 8, input_length=max_length)
  • vocab_size = 50 - this means there are 50 words in the dictionary
  • embedding_size= 8 - this is the true size of the embedding: each word is represented by a vector of 8 numbers.

3 - You don't know. They use the same embedding.

The system will use the same embedding (the one for index = 2). This is not healthy for your model at all. You should use another method for creating indices in question 1.


4 - You can create a word dictionary manually, or use the Tokenizer class.

Manually:

Make sure you remove punctuation, make all words lower case.

Just create a dictionary for each word you have:

dictionary = dict()
current_key = 1

for doc in docs:
    for word in doc.split(' '):
        #make sure you remove punctuation (this might be boring)
        word = word.lower()

        if not (word in dictionary):
            dictionary[word] = current_key
            current_key += 1

Tokenizer:

from keras.preprocessing.text import Tokenizer

tokenizer = Tokenizer()

#this creates the dictionary
#IMPORTANT: MUST HAVE ALL DATA - including Test data
#IMPORTANT2: This method should be called only once!!!
tokenizer.fit_on_texts(docs)

#this transforms the texts in to sequences of indices
encoded_docs2 = tokenizer.texts_to_sequences(docs)

See the output of encoded_docs2:

[[6, 2], [3, 1], [7, 4], [8, 1], [9], [10], [5, 4], [11, 3], [5, 1], [12, 13, 2, 14]]

See the maximum index:

padded_docs2 = pad_sequences(encoded_docs2, maxlen=max_length, padding='post')
max_index = array(padded_docs2).reshape((-1,)).max()

So, your vocab_size should be 15 (otherwise you'd have lots of useless - and harmless - embedding rows). Notice that 0 was not used as an index. It will appear in padding!!!

Do not "fit" the tokenizer again! Only use texts_to_sequences() or other methods here that are not related to "fitting".

Hint: it might be useful to include end_of_sentence words in your text sometimes.

Hint2: it is a good idea to save your Tokenizer to be used later (since it has a specific dictoinary for your data, created with fit_on_texts).

#save:
text_to_save = tokenizer.to_json()

#load:
from keras.preprocessing.text import tokenizer_from_json
tokenizer = tokenizer_from_json(loaded_text)

5 - Params for embedding are correct.

Dense:

Params for Dense are always based on the preceding layer (the Flatten in this case).

The formula is: previous_output * units + units

This results in 32 (from the Flatten) * 1 (Dense units) + 1 (Dense bias=units) = 33

Flatten:

It gets all the previous dimensions multiplied = 8 * 4.
The Embedding outputs lenght = 4 and embedding_size = 8.


6 - The Embedding layer is not dependent of your data and how you preprocess it.

The Embedding layer has simply the size 50 x 8 because you told so. (See question 2)

There are, of course, better ways of preprocessing the data - See question 4.

This will lead you to select better the vocab_size (which is dictionary size).

Seeing the embedding of a word:

Get the embeddings matrix:

embeddings = model.layers[0].get_weights()[0]

Choose any word index:

embeding_for_word_7 = embeddings[7]

That's all.

If you're using a tokenizer, get the word index with:

index = tokenizer.texts_to_sequences([['word']])[0][0]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!