patrick-kidger / torchcubicspline

Interpolating natural cubic splines. Includes batching, GPU support, support for missing values, evaluating derivatives of the spline, and backpropagation.
Apache License 2.0
198 stars 18 forks source link

1 Dimension Only? #4

Open jeongjoonpark opened 3 years ago

jeongjoonpark commented 3 years ago

Hi I wonder if this code works for 2 or 3 dimensional data!

Thanks.

patrick-kidger commented 3 years ago

Depends what you mean!

The first example in the README considers three different dimensions (=channels) at the same time:

import torch
from torchcubicspline import(natural_cubic_spline_coeffs, 
                             NaturalCubicSpline)

length, channels = 7, 3
t = torch.linspace(0, 1, length)
x = torch.rand(length, channels)
coeffs = natural_cubic_spline_coeffs(t, x)
spline = NaturalCubicSpline(coeffs)
point = torch.tensor(0.4)
out = spline.evaluate(point)

If you mean that you want to have multiple "lengths", then I'm afraid not. (Good ways of doing splines isn't even a solved problem in that context.)

Does that answer your question?

yanconglin commented 3 years ago

Hi, Patrick Kidger,

Thanks for sharing! I have a similar question: interpolating on non-regular 2D/3D data. Seems like your approach only interpolates over the time dimesion (1-D data). Now I have a 2D non-regular grid, and want to upsample over this grid. Since the Pytorch interploation only works with regular grids (e.g. images), I am looking for other implementations.

x = np.linspace(-3, 3, 100) # non regular grids y = np.linspace(-5, 5, 100) z = np.linspace(-9, 9, 100)

X,Y, Z = np.meshgrid(x,y,z,indexing='ij') values = np.randn((100,))

interp = custom_Interp3D_algorithm((x,y,z), values)

// sample points
new_x = np.linspace(-3, 3, 400) new_y = np.linspace(-5, 5, 400) new_z = np.linspace(-9, 9, 400) // output new values: [400, ] new_values = interp((new_x, new_y, new_z))

Once can also wrap this into BXCXHXW tensor, where HX W is the 2d grid. Was wondering if you have such an implemtation. Thanks a lot!

Yancong

patrick-kidger commented 3 years ago

Hi @yanconglin - I'm afraid not, is the short answer.

Producing higher-dimensional interpolants, on irregular data, is much harder than the 1D case. Methods for doing so are much less standard than in the 1D case.