Gradient checking
Is there any simple and common gradient checking method, when extending an autograd function ?
alexis-jacq
jekbradbury
alexis-jacq
It’s been added only recently and we forgot about that. Can you send a PR please?
apaszke
alexis-jacq
Hello, <SCODE>class SolveTrianguler(Function):
    # sloves A * x = b
    def __init__(self, trans=0, lower=True):
        super(SolveTrianguler, self).__init__()
        # trans=1, transpose the matrix A.T * x = b
        self.trans = trans
        # lower=False, use data contained in the upper triangular, the default is lower
        self.lower = lower
        # self.needs_input_grad = (True, False)

    def forward(self, matrix, rhs):
        x = torch.from_numpy(
            solve_triangular(matrix.numpy(), rhs.numpy(),
                             trans=self.trans,  lower=self.lower))

        self.save_for_backward(matrix, x)
        return x

    def backward(self, grad_output):
        # grad_matrix = grad_rhs = None
        matrix, x = self.saved_tensors
        # formula from Giles 2008, 2.3.1
        return -matrix.inverse().t().mm(grad_output).mm(torch.t(x)), \
               matrix.inverse().t().mm(grad_output)
<ECODE>
yinhao