I haven’t tried ignore_index, that method will probably work. More general way is to use reduction=‘none’, multiply padded loss cells by zero and reduce loss tensor using weighted mean (i.e. divide loss sum by number of good cells).