fast_lisa_subtraction.utils.interpolation module
This scripts implements the Linear 1D interpolation in PyTorch as done in scipy.interpolate.interp1d The code is adapted from https://github.com/aliutkus/torchinterp1d
- class fast_lisa_subtraction.utils.interpolation.Interp1d(*args, **kwargs)[source]
Bases:
Function- static backward(ctx, grad_out)[source]
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjpfunction.)It must accept a context
ctxas the first argument, followed by as many outputs as theforward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,backward()will havectx.needs_input_grad[0] = Trueif the first input toforward()needs gradient computed w.r.t. the output.
- static forward(ctx, x, y, xnew, out=None)[source]
Linear 1D interpolation on the GPU for Pytorch. This function returns interpolated values of a set of 1-D functions at the desired query points xnew. This function is working similarly to Matlab™ or scipy functions with the linear interpolation mode on, except that it parallelises over any number of desired interpolation problems. The code will run on GPU if all the tensors provided are on a cuda device.
- Parameters:
x ((N, ) or (D, N) Pytorch Tensor) – A 1-D or 2-D tensor of real values.
y ((N,) or (D, N) Pytorch Tensor) – A 1-D or 2-D tensor of real values. The length of y along its last dimension must be the same as that of x
xnew ((P,) or (D, P) Pytorch Tensor) – A 1-D or 2-D tensor of real values. xnew can only be 1-D if _both_ x and y are 1-D. Otherwise, its length along the first dimension must be the same as that of whichever x and y is 2-D.
out (Pytorch Tensor, same shape as xnew) – Tensor for the output. If None: allocated automatically.