-
Notifications
You must be signed in to change notification settings - Fork 19
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
Problem when dim x not same as dim y #13
Comments
dear @monabf, maybe you should just duplicate the you're right, the doc from the readme and the docstring from the function don't seem to match. As it is implemented now, it looks like |
Hi @aliutkus, you're right, using |
To explore which of x and xnew should be expanded, I write a demo: import torch
from utils.gpu.torch_interp1d import interp1d
import scipy.interpolate as syinterpolate
if __name__ == "__main__":
N = 100
D = 1024
P = 30
x = torch.arange(N).view(-1, N) # -> 1,N
y = torch.randn([D, N])
xnew = torch.linspace(0, N, P)
ynew = interp1d(x, y, xnew)
ynewv2 = interp1d(x.expand(D, -1), y, xnew)
ynewv3 = interp1d(x, y, xnew.expand(D, -1))
ynewv4 = interp1d(x.expand(D, -1), y, xnew.expand(D, -1))
# scipy interp1d
x = x.squeeze().cpu().numpy() # N
y = y.transpose(-1, -2).cpu().numpy() # N,D
xnew = xnew.cpu().numpy() # P
fit_func = syinterpolate.interp1d(x, y, kind="linear", axis=0, fill_value="extrapolate")
ynewv5 = fit_func(xnew)
ynewv5 = torch.from_numpy(ynewv5).float().transpose(-1, -2) # D,P
print(ynew.shape) # 1,P
print(ynewv2.shape) # D,P
print(ynewv3.shape) # D,P
print(ynewv4.shape) # D,P
# print((ynewv2 == ynewv3).all()) # False
# print((ynewv2 == ynewv4).all()) # True
# print((ynewv2 == ynewv5).all()) # may be False, caused by computational accuracy of the cpu
print((ynewv2 - ynewv3).abs().sum()) # >0
print((ynewv2 - ynewv4).abs().sum()) # 0
print((ynewv2 - ynewv5).abs().sum()) # slightly close to 0
print((ynewv3 - ynewv4).abs().sum()) # >0
print((ynewv3 - ynewv5).abs().sum()) # >0
print((ynewv4 - ynewv5).abs().sum()) # slightly close to 0
# conclusion: ynewv2 is equal to ynewv4 and they both are close to ynewv5 So, we should expand |
Hi,
Thanks a lot for the implementation. However, I have a problem with the dimensions. For example, x is a 1D grid of 100 points, y is a 2D grid of 100 points, and I want to interpolate the 2D value of y at one point in x denoted xnew. Then I should obtain a 2D value for ynew, which should be the interpolation of each axis of y at the interpolated coordinate xnew. However, I obtain a scalar ynew. Conversely, when x is a 2D grid and y is a 1D grid, I obtain ynew of dimension 2 for xnew of dimension 2. Am I missing something here?
Below two small working examples, with respectively
Thanks a lot for your help!
Output:
The text was updated successfully, but these errors were encountered: