I am building a recurrent neural network (RNN). The feature set contains many categorical variables. Some of them are like users and items. In this case, if I use one-hot encoding and concatenate these vectors into a big one, the resulting vector will be super sparse. Is it fine to do this? I am not sure if this is normal for RNN.

Is there any other way to handle this case?

  • $\begingroup$ crossposted on quora: quora.com/unanswered/… $\endgroup$ Aug 8, 2016 at 21:51
  • $\begingroup$ Should I remove either? $\endgroup$
    – Munichong
    Aug 8, 2016 at 21:57
  • $\begingroup$ That's fine, just for people to check out the link first before they answer $\endgroup$ Aug 8, 2016 at 21:58
  • 1
    $\begingroup$ Entity embedding is a method that seems promising here. Cheng Guo and Felix Berkhahn "Entity Embeddings of Categorical Variables" arxiv.org/pdf/1604.06737.pdf $\endgroup$
    – Sycorax
    May 2, 2018 at 15:07
  • $\begingroup$ Are these categorical features "metadata" or is it something like text, engagement indicators, etc.? Also is this a classification or regression problem? $\endgroup$ Sep 6, 2018 at 18:36

2 Answers 2


The "default" way of dealing with categorical variables for neural networks is to use embeddings. The most popular usage is word embeddings, where words are represented by vector representation (learned or pre-trained). The advantages of such approach is that it has smaller dimensionality then if you used one-hot encodings and they usually form meaningful representations of words, i.e. similar words have similar representations in the embedding space. Same idea can be applied to any other categorical variables, Sycorax gave one reference of paper by Guo and Berkhahn, but you can check also other references and this Medium post. Embeddings were used for categorical variables in Kaggle competitions, but you can also find many examples of recommender systems using such representations, for example this recent post on Google Cloud blog.


This is not my area of expertise, but my understanding is that it's a lot harder to learn on a sparse representation. You won't have many training examples for each input, so most of the neurons will be training on just a few examples.

The canonical example for RNNs would be NLP, and I think it's fairly standard to transform the input text from something sparse (e.g., a one-hot encoding of word IDs), to a dense vector embedding (e.g., word2vec). This page is pretty good:


In your case it's hard to say what exactly would be best without knowing more about your data. But maybe you could find some sort of vector embedding that would work for your data. Then you could capture some of its essential properties and train your RNN effectively.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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