1
$\begingroup$

I am currently in the process of learning about seq2seq autoencoders for a task involving sentence embedding (samples are sentences, with words represented as integers in a vocab of size $n$). In the different tutorials and examples I have viewed I have noticed some authors use an embedding layer after the input layer while others do not. Now I understand what embedding layers are and roughly how they work, but I can not seem to figure out when one should use an embedding layer - specifically in the context of my task.

My best guess is that embedding layers simply make the representation of the data easier for the network to work with, transforming a large vocab of $n$ words as integers into fixed sized float vectors. However I do not really have any evidence to back this up other than intuition.

So are there any rules of thumb as to when embedding layer after a network's input layer? If so, are there any good rules of thumb for what embedding size to choose in proportion to the vocabulary size?

If it matters, I am referring to the Embedding layer in Tensorflow 2 / Keras.

$\endgroup$
2
  • $\begingroup$ When an author omits an embedding layer, is the input to the network something like a 1-hot vector? Or is there a different kind of input? I can imagine several different ways of doing this kind of NLP task, and the comparison you're making may or may not reflect alternative ways to represent the same model... $\endgroup$
    – Sycorax
    Commented Sep 14, 2020 at 16:42
  • $\begingroup$ Hm, I do believe on some of the smaller vocabularies I saw, some of the inputs were one hot encoded. However most were just integers. $\endgroup$
    – J. Auon
    Commented Sep 14, 2020 at 17:09

1 Answer 1

1
$\begingroup$

Embedding layers and multiplication by appropriate 1-hot vectors can be shown to be the same in the sense that they produce the same result. See: How does torch.nn.Embedding or tf.keras.layers.Embedding compare to a dense layer?

Computing the dot product of a $1$-hot vector with a large matrix is expensive, and not strictly necessary. Computing a bunch of these (because you have several words in a sentence) is wasteful because multiplying by zero a bunch of times is silly. Instead we can just select the hot entries from the matrix directly, which is what an embedding layer does.

If you're not convinced, write down a matrix and a 1-hot vector and carry out the multiplication.


are there any rules of thumb as to when embedding layer after a network's input layer?

Since the two methods yield the same result, we could distinguish them on the basis of which is faster. Which method wins the race is a specific to the software, which is not on-topic here.


are there any good rules of thumb for what embedding size to choose in proportion to the vocabulary size?

This Google Developers Blog post "Introducing TensorFlow Feature Columns" suggests using the fourth root of the number of categories (vocabulary size).

$$ \text{embedding dimensions} = \text{number of categories}^{\frac{1}{4}} $$

I'm not aware of any results establish that this rule of thumb is optimal. Different tasks or applications might require different embeddings. Of course, engineering constraints, such as storage or computational requirements, might limit the size of the embedding dimension.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.