did you try to make the reduce=False in torch.nn. MSELoss ( size_average=None ,  reduce=None ,  reduction=‘mean’ )? It will be return a vector. You can make a mask by yourself. For example, your output is [1,2,3,4], you don’t want to calculate the loss for 1 and 4, you can just set mask = [0,1,1,0] and your loss_matrix = output * mask and then loss = avg(loss_matrix)
here is the Doc:
reduce  ( bool  4 ,  optional ) – Deprecated (see  reduction ). By default, the losses are averaged or summed over observations for each minibatch depending on  size_average . When  reduce  is  False , returns a loss per batch element instead and ignores  size_average . Default:  True