Hi,
I want to be able to use the univariate spline class from scipy with pytorch but I am not able to do this while maintaining autograd. I have been looking at Creating Extensions Using numpy and scipy — PyTorch Tutorials 1.8.0 documentation 1 to try and get the scipy code working in pytorch but I am unsure that the correct gradient is being returned. This is the code I have so far:
from torch.autograd import Function

class SPLFunction(Function):
   
    @staticmethod
    def forward(ctx, bin, x):
        ctx.save_for_backward(bin, x)

        x = x.detach().numpy()
        bin = bin.detach().numpy()
        
        bin_steps = np.arange(2, 22+20/63, 20/63)[:-1]
        spline_curve = UnivariateSpline(bin_steps, bin, ext=3)
        spline_curve.set_smoothing_factor(0.5)
        
        result = spline_curve(x)
        return torch.as_tensor(result)

    @staticmethod
    def backward(ctx, grad_output):
        bin, x = ctx.saved_tensors

        bin_steps = np.arange(2, 22+20/63, 20/63)[:-1]
        spline_curve = UnivariateSpline(bin_steps, bin, ext=3)
        spline_curve.set_smoothing_factor(0.5)
        
        df = spline_curve.derivative(x)
        df = torch.from_numpy(df*grad_output)

        return df

def apply_spline(bin, x):
    return SPLFunction.apply(bin, x)

Thank you for your help