Hi albanD,
Thank a lot for reply.
Because I want to do more complex work for this custom loss function, I have to use the conversion to numpy.
I’m not sure how grad_output works, do you have any example?
The following code modified by myself. Is it correct?
class MSE_SCORE(Function):
    @staticmethod
    def forward(ctx, input, label):
        numpy_input = input.detach().numpy()
        numpy_label = label.detach().numpy()
        offset_height = int(height / 4)
        offset_width = int(width / 4)
        target_height = int(height / 2)
        target_width = int(width / 2)
        crop_input = numpy_input[:, :, offset_height:offset_height + target_height, offset_width:offset_width + target_width]
        crop_label = numpy_label[:, :, offset_height:offset_height + target_height, offset_width:offset_width + target_width]
        diff = (crop_input - crop_label)**2
        mean = np.mean(diff, axis=(1,2,3))
        ctx.size = len(mean)
        mse = np.sum(mean)
        ctx.save_for_backward(input, label) --- modified
        return torch.tensor(mse)

    @staticmethod
    def backward(ctx, grad_output):
        grad_output = grad_output.detach()
        input, label = ctx.saved_tensors --- modified
        return grad_output * input, None --- modified