Thanks for the reply! The length of k actually varies and I padded them to the same length. In this situation, is there any good way to do the loss (please see my updated example in reply to @ptrblck) ?