The trick is to build a submatrix with only the entries that correspond to actual words.  You can reshape the array to flatten the words and sentences, and only keep the subset.  Then the nll only applies to those entries and backprop will do the correct thing.