title: Scalable, Efficient Hierarchical Softmax in Tensorflow?
body: <p>I'm interested in implementing a hierarchical softmax model that can handle large vocabularies, say on the order of 10M classes. What is the best way to do this to both be scalable to large class counts and efficient? For instance, at least <a href="https://arxiv.org/abs/1512.04906" rel="noreferrer">one paper</a> has shown that HS can achieve a ~25x speedup for large vocabs when using a 2-level tree where each node <code>sqrt(N)</code> classes. I'm interested also in a more general version for an arbitrary depth tree with an arbitrary branching factor.</p>
  
  <p>There are a few options that I see here:</p>
  
  <p>1) Run <code>tf.gather</code> for every batch, where we gather the indices and splits. This creates problems with large batch sizes and fat trees where now the coefficients are being duplicated a lot, leading to OOM errors.</p>
  
  <p>2) Similar to #1, we could use <code>tf.embedding_lookup</code> which would keep help with OOM errors but now keeps everything on the CPU and slows things down quite a bit.</p>
  
  <p>3) Use <code>tf.map_fn</code> with <code>parallel_iterations=1</code> to process each sample separately and go back to using gather. This is much more scalable but does not really get close to the 25x speedup due to the serialization.</p>
  
  <p>Is there a better way to implement HS? Are there different ways for deep and narrow vs. short and wide trees?</p>