Open monabf opened 2 years ago
dear @monabf, maybe you should just duplicate the xnew
variable ?
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 out
is same shape as xnew
. I may have a look into this
Hi @aliutkus, you're right, using x = x.expand(Dy,-1)
seems to solve the problem and avoids slicing away part of the y. Thanks for looking into it! It would be great to have this corrected or documented indeed
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 x
.
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: