Skip to content

Commit 3bf7ec7

Browse files
thakorehgpleiss
andauthored
fix: replace deprecated np.trapz with scipy.integrate.trapezoid (#2721)
* fix: replace deprecated np.trapz with scipy.integrate.trapezoid (GH#2695) np.trapz was removed in numpy 2.4.0. Use scipy.integrate.trapezoid instead. Fixes #2695 * fix: use torch.trapezoid instead of scipy (address review feedback) Use PyTorch's native torch.trapezoid and torch.cumulative_trapezoid instead of scipy.integrate functions for better GPU compatibility. * feat: use torch.fft instead of scipy for FFT operations Replaced scipy.fftpack.fft with torch.fft.fft to stay in PyTorch land and avoid unnecessary tensor conversions. Changes: - spectral_mixture_kernel.py: Use torch.fft.fft instead of scipy - spectral_delta_kernel.py: Use torch.fft.fft instead of scipy - All frequency arrays now created with torch.arange instead of np.arange - Removed redundant tensor conversions (torch.from_numpy calls) --------- Co-authored-by: Geoff Pleiss <gpleiss@gmail.com>
1 parent 4d66a49 commit 3bf7ec7

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

gpytorch/kernels/spectral_delta_kernel.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,30 @@ def initialize_from_data(self, train_x, train_y):
5252
is initialized, but we skip the last step of fitting a GMM to the samples and just use the samples directly.
5353
"""
5454
import numpy as np
55-
from scipy.fftpack import fft
56-
from scipy.integrate import cumulative_trapezoid
5755

5856
N = train_x.size(-2)
59-
emp_spect = np.abs(fft(train_y.cpu().detach().numpy())) ** 2 / N
57+
# Use torch.fft instead of scipy for FFT (stay in PyTorch land)
58+
train_y_np = train_y.cpu().detach()
59+
emp_spect = torch.abs(torch.fft.fft(train_y_np)) ** 2 / N
6060
M = math.floor(N / 2)
6161

62-
freq1 = np.arange(M + 1)
63-
freq2 = np.arange(-M + 1, 0)
64-
freq = np.hstack((freq1, freq2)) / N
62+
freq1 = torch.arange(M + 1, dtype=train_y_np.dtype)
63+
freq2 = torch.arange(-M + 1, 0, dtype=train_y_np.dtype)
64+
freq = torch.hstack((freq1, freq2)) / N
6565
freq = freq[: M + 1]
6666
emp_spect = emp_spect[: M + 1]
6767

68-
total_area = np.trapz(emp_spect, freq)
69-
spec_cdf = np.hstack((np.zeros(1), cumulative_trapezoid(emp_spect, freq)))
68+
# Use torch.trapezoid (already in PyTorch)
69+
total_area = torch.trapezoid(emp_spect, freq).item()
70+
spec_cdf = torch.cat([torch.zeros(1), torch.cumulative_trapezoid(emp_spect, freq)]).numpy()
7071
spec_cdf = spec_cdf / total_area
72+
freq_np = freq.numpy()
7173

7274
a = np.random.rand(self.raw_Z.size(-2), 1)
7375
p, q = np.histogram(a, spec_cdf)
7476
bins = np.digitize(a, q)
75-
slopes = (spec_cdf[bins] - spec_cdf[bins - 1]) / (freq[bins] - freq[bins - 1])
76-
intercepts = spec_cdf[bins - 1] - slopes * freq[bins - 1]
77+
slopes = (spec_cdf[bins] - spec_cdf[bins - 1]) / (freq_np[bins] - freq_np[bins - 1])
78+
intercepts = spec_cdf[bins - 1] - slopes * freq_np[bins - 1]
7779
inv_spec = (a - intercepts) / slopes
7880

7981
self.Z = inv_spec

gpytorch/kernels/spectral_mixture_kernel.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def initialize_from_data_empspect(self, train_x: torch.Tensor, train_y: torch.Te
165165
"""
166166

167167
import numpy as np
168-
from scipy.fftpack import fft
169-
from scipy.integrate import cumulative_trapezoid
170168

171169
with torch.no_grad():
172170
if not torch.is_tensor(train_x) or not torch.is_tensor(train_y):
@@ -181,24 +179,28 @@ def initialize_from_data_empspect(self, train_x: torch.Tensor, train_y: torch.Te
181179
train_y = train_y.view(-1)
182180

183181
N = train_x.size(-2)
184-
emp_spect = np.abs(fft(train_y.cpu().detach().numpy())) ** 2 / N
182+
# Use torch.fft instead of scipy for FFT (stay in PyTorch land)
183+
train_y_np = train_y.cpu().detach()
184+
emp_spect = torch.abs(torch.fft.fft(train_y_np)) ** 2 / N
185185
M = math.floor(N / 2)
186186

187-
freq1 = np.arange(M + 1)
188-
freq2 = np.arange(-M + 1, 0)
189-
freq = np.hstack((freq1, freq2)) / N
187+
freq1 = torch.arange(M + 1, dtype=train_y_np.dtype)
188+
freq2 = torch.arange(-M + 1, 0, dtype=train_y_np.dtype)
189+
freq = torch.hstack((freq1, freq2)) / N
190190
freq = freq[: M + 1]
191191
emp_spect = emp_spect[: M + 1]
192192

193-
total_area = np.trapz(emp_spect, freq)
194-
spec_cdf = np.hstack((np.zeros(1), cumulative_trapezoid(emp_spect, freq)))
193+
# Use torch.trapezoid (already in PyTorch)
194+
total_area = torch.trapezoid(emp_spect, freq).item()
195+
spec_cdf = torch.cat([torch.zeros(1), torch.cumulative_trapezoid(emp_spect, freq)]).numpy()
195196
spec_cdf = spec_cdf / total_area
197+
freq_np = freq.numpy()
196198

197199
a = np.random.rand(1000, self.ard_num_dims)
198200
p, q = np.histogram(a, spec_cdf)
199201
bins = np.digitize(a, q)
200-
slopes = (spec_cdf[bins] - spec_cdf[bins - 1]) / (freq[bins] - freq[bins - 1])
201-
intercepts = spec_cdf[bins - 1] - slopes * freq[bins - 1]
202+
slopes = (spec_cdf[bins] - spec_cdf[bins - 1]) / (freq_np[bins] - freq_np[bins - 1])
203+
intercepts = spec_cdf[bins - 1] - slopes * freq_np[bins - 1]
202204
inv_spec = (a - intercepts) / slopes
203205

204206
from sklearn.mixture import GaussianMixture

0 commit comments

Comments
 (0)