I am completely embarrassed, but I found that I am trying to match input and target with different indices. When I compared input and target with the same index I found that their correspondence is kept. I apologize again for wasting your valuable time.