You could change the criterion to reduce=True or call loss.mean().backward().
In your above code, you are calling loss.backward with a random Tensor, since torch.Tensor(1, h, w) does not initialize the Tensor with any specific values.
You should change it to torch.ones in case you want to pass an argument to the backward function.