Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation of a 2D tensor doesnt behave as expected #24

Open
anegi19 opened this issue Nov 4, 2024 · 1 comment
Open

Interpolation of a 2D tensor doesnt behave as expected #24

anegi19 opened this issue Nov 4, 2024 · 1 comment

Comments

@anegi19
Copy link

anegi19 commented Nov 4, 2024

Hello,

The interp1d function is unable to interpolate a 2-D tensor

For example

import torch

# Define x, y, and xnew with appropriate shapes
D, N, P = 3, 5, 4  # D = number of batches, N = original points, P = new points

# `y` is a 2D tensor with `D` rows, each representing an independent 1D function
y = torch.tensor([
    [1.0, 2.0, 3.0, 4.0, 5.0],
    [2.0, 3.0, 5.0, 7.0, 11.0],
    [1.0, 4.0, 9.0, 16.0, 25.0]
], requires_grad=True)

# `x` is the corresponding x-coordinates, can be either 1D (shared across all `y` rows) or 2D
x = torch.tensor([0.0, 1.0, 2.0, 3.0, 4.0])  # Shape (N, )

# `xnew` is where we want the interpolated values, can be 1D or 2D
xnew = torch.tensor([0.5, 1.5, 2.5, 3.5])  # Shape (P, )

# Perform interpolation
ynew = interp1d(x, y, xnew)
print("Interpolated values (ynew):", ynew)

gives me the output
Interpolated values (ynew): tensor([[1.5000, 2.5000, 3.5000, 4.5000]], grad_fn=<Interp1dBackward>)

Is this behavior expected? Or this is buggy?

@minh-nguyenhoang
Copy link

As per the docs said, you xnew must have the same batch size of which ever tensor is 2-D

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants