I have a non-differentiable loss function. Something that takes a few tensors that require gradients, copies them, computes some stuff, and then returns the cost as a tensor.
Is there a way to force the autograd framework to compute the gradients numerically?
Or must I explicitly compute the numerical gradients?
Using autograd I have started to write this:
class torch_loss(torch.autograd.Function):

    @staticmethod
    def forward(ctx, g_T, g_pred, tsr_img, obj):
        ctx.save_for_backward(g_T, g_pred, tsr_img)
        ctx.obj = obj

        return loss_fcn(g_T, g_pred, tsr_img, obj)

    @staticmethod
    def backward(ctx, grad_output):
        g_T, g_pred, tsr_img = ctx.saved_tensors
        obj = ctx.obj

        grad_T = grad_pred = None
        # do something with grad_T, grad_pred, and grad_output

        return grad_T, grad_pred, None, None

Where loss_fcn() is the non-differentiable part. And g_T and g_pred have requires_grad=True. Also obj is not a tensor, thus it cannot be saved by save_for_backwards().
Is this doable?
What are the shapes of the output tensors for grad_T and grad_pred if they have, for example, shapes of (3, 4) and (1, 10, 20), respectively?