The failing operation tries to reshape the target to target.view(n, h, w, 1), where n, h, w are coming from the input: n, c, h, w = input.size().
The expected shapes are given as:
# input: (n, c, h, w), target: (n, h, w)

which doesn’t seem to be the case for your work flow.
Could you check the model output shape as well as the target shape and make sure that the batch dimension as well as the spatial dimensions are equal?