项目作者: patrick-kidger

项目描述 :
Interpolating natural cubic splines. Includes batching, GPU support, support for missing values, evaluating derivatives of the spline, and backpropagation.
高级语言: Python
项目地址: git://github.com/patrick-kidger/torchcubicspline.git
创建时间: 2020-03-09T17:50:11Z
项目社区:https://github.com/patrick-kidger/torchcubicspline

开源协议:Apache License 2.0

下载


torchcubicspline

Interpolating natural cubic splines using PyTorch. Includes support for:

  • Batching
  • GPU support and backpropagation via PyTorch
  • Support for missing values (represent them as NaN)
  • Evaluating the first derivative of the spline

Installation

  1. pip install git+https://github.com/patrick-kidger/torchcubicspline.git

Example

Simple example:

  1. import torch
  2. from torchcubicspline import(natural_cubic_spline_coeffs,
  3. NaturalCubicSpline)
  4. length, channels = 7, 3
  5. t = torch.linspace(0, 1, length)
  6. x = torch.rand(length, channels)
  7. coeffs = natural_cubic_spline_coeffs(t, x)
  8. spline = NaturalCubicSpline(coeffs)
  9. point = torch.tensor(0.4)
  10. out = spline.evaluate(point)

With multiple batch and evaluation dimensions:

  1. import torch
  2. from torchcubicspline import(natural_cubic_spline_coeffs,
  3. NaturalCubicSpline)
  4. t = torch.linspace(0, 1, 7)
  5. # (2, 1) are batch dimensions. 7 is the time dimension
  6. # (of the same length as t). 3 is the channel dimension.
  7. x = torch.rand(2, 1, 7, 3)
  8. coeffs = natural_cubic_spline_coeffs(t, x)
  9. # coeffs is a tuple of tensors
  10. # ...at this point you can save the coeffs, put them
  11. # through PyTorch's Datasets and DataLoaders, etc...
  12. spline = NaturalCubicSpline(coeffs)
  13. point = torch.tensor(0.4)
  14. # will be a tensor of shape (2, 1, 3), corresponding to
  15. # batch, batch, and channel dimensions
  16. out = spline.derivative(point)
  17. point = torch.tensor([[0.4, 0.5]])
  18. # will be a tensor of shape (2, 1, 1, 2, 3), corresponding to
  19. # batch, batch, time, time and channel dimensions
  20. out = spline.derivative(point)

Functionality

Functionality is provided via the natural_cubic_spline_coeffs function and NaturalCubicSpline class.

natural_cubic_spline_coeffs takes an increasing sequence of times represented by a tensor t of shape (length,) and some corresponding observations x of shape (..., length, channels), where ... are batch dimensions, and each (length, channels) slice represents a sequence of length points, each point with channels many values.

Then calling

  1. coeffs = natural_cubic_spline_coeffs(t, x)
  2. spline = NaturalCubicSpline(coeffs)

produces an instance spline such that

  1. spline.evaluate(t[i]) == x[..., i, :]

for all i.

Why is there a function and a class?

The slow bit is done during natural_cubic_spline_coeffs. The fast bit is NaturalCubicSpline. The returned coeffs are a tuple of PyTorch tensors, so you can take this opportunity to save or load them, push them through torch.utils.data.Dataset or torch.utils.data.DataLoader, etc.

Derivatives

The derivative of the spline at a point may be calculated via spline.derivative. (Not be confused with backpropagation, which is also supported through both spline.evaluate and spline.derivative.)

Missing values

Support for missing values is done by setting that element of x to NaN. In particular this allows for batching elements with different observation times: take times to be the observation times of all elements in the batch, and just set each element to have a missing observation NaN at the times of the observations of the other batch elements.

Limitations

If possible, you should cache the coefficients returned by natural_cubic_spline_coeffs. In particular if there are missing values then the computation can be quite slow.

Any issues?

Any issues or questions - open an issue to let me know. :)