From ab439a7c0a8496b9f68930ac8f096fd0fc219de0 Mon Sep 17 00:00:00 2001 From: d-schmitt Date: Thu, 8 Dec 2022 17:47:17 +0100 Subject: [PATCH 1/3] Update sub_modules.py to remove UserWarning The nbeats/sub_module.py raises the following warning: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. This commit replaces the relevant sections with the appropriate code to remove the warning. (similar to #754) --- .../models/nbeats/sub_modules.py | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pytorch_forecasting/models/nbeats/sub_modules.py b/pytorch_forecasting/models/nbeats/sub_modules.py index 4518da43..85f3ab22 100644 --- a/pytorch_forecasting/models/nbeats/sub_modules.py +++ b/pytorch_forecasting/models/nbeats/sub_modules.py @@ -99,19 +99,19 @@ def __init__( backcast_linspace, forecast_linspace = linspace(backcast_length, forecast_length, centered=False) p1, p2 = (thetas_dim // 2, thetas_dim // 2) if thetas_dim % 2 == 0 else (thetas_dim // 2, thetas_dim // 2 + 1) - s1_b = torch.tensor( - [np.cos(2 * np.pi * i * backcast_linspace) for i in self.get_frequencies(p1)], dtype=torch.float32 + s1_b = torch.from_numpy(np.array( + [np.cos(2 * np.pi * i * backcast_linspace) for i in self.get_frequencies(p1)], dtype=np.float32), ) # H/2-1 - s2_b = torch.tensor( - [np.sin(2 * np.pi * i * backcast_linspace) for i in self.get_frequencies(p2)], dtype=torch.float32 + s2_b = torch.from_numpy(np.array( + [np.sin(2 * np.pi * i * backcast_linspace) for i in self.get_frequencies(p2)], dtype=np.float32), ) self.register_buffer("S_backcast", torch.cat([s1_b, s2_b])) - s1_f = torch.tensor( - [np.cos(2 * np.pi * i * forecast_linspace) for i in self.get_frequencies(p1)], dtype=torch.float32 + s1_f = torch.from_numpy(np.array( + [np.cos(2 * np.pi * i * forecast_linspace) for i in self.get_frequencies(p1)], dtype=np.float32), ) # H/2-1 - s2_f = torch.tensor( - [np.sin(2 * np.pi * i * forecast_linspace) for i in self.get_frequencies(p2)], dtype=torch.float32 + s2_f = torch.from_numpy(np.array( + [np.sin(2 * np.pi * i * forecast_linspace) for i in self.get_frequencies(p2)], dtype=np.float32), ) self.register_buffer("S_forecast", torch.cat([s1_f, s2_f])) @@ -151,10 +151,13 @@ def __init__( backcast_linspace, forecast_linspace = linspace(backcast_length, forecast_length, centered=True) norm = np.sqrt(forecast_length / thetas_dim) # ensure range of predictions is comparable to input - coefficients = torch.tensor([backcast_linspace**i for i in range(thetas_dim)], dtype=torch.float32) + coefficients = torch.from_numpy( + np.array([backcast_linspace**i for i in range(thetas_dim)], dtype=np.float32), + ) self.register_buffer("T_backcast", coefficients * norm) - - coefficients = torch.tensor([forecast_linspace**i for i in range(thetas_dim)], dtype=torch.float32) + coefficients = torch.from_numpy( + np.array([forecast_linspace**i for i in range(thetas_dim)], dtype=np.float32), + ) self.register_buffer("T_forecast", coefficients * norm) def forward(self, x) -> Tuple[torch.Tensor, torch.Tensor]: @@ -165,7 +168,9 @@ def forward(self, x) -> Tuple[torch.Tensor, torch.Tensor]: class NBEATSGenericBlock(NBEATSBlock): - def __init__( + def __init__(coefficients = torch.tensor([backcast_linspace**i coefficients = torch.tensor([backcast_linspace**i for i in range(thetas_dim)], dtype=torch.float32) + for i in range(thetas_dim)], dtype=torch.float32) + self, units, thetas_dim, From 6d0eee6197752ed8ee354c9c7c7c53ecc7ca0a5c Mon Sep 17 00:00:00 2001 From: d-schmitt Date: Mon, 10 Jun 2024 20:26:34 +0200 Subject: [PATCH 2/3] Update sub_modules.py Remove faulty copy commit. --- pytorch_forecasting/models/nbeats/sub_modules.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pytorch_forecasting/models/nbeats/sub_modules.py b/pytorch_forecasting/models/nbeats/sub_modules.py index 85f3ab22..7c089734 100644 --- a/pytorch_forecasting/models/nbeats/sub_modules.py +++ b/pytorch_forecasting/models/nbeats/sub_modules.py @@ -168,9 +168,7 @@ def forward(self, x) -> Tuple[torch.Tensor, torch.Tensor]: class NBEATSGenericBlock(NBEATSBlock): - def __init__(coefficients = torch.tensor([backcast_linspace**i coefficients = torch.tensor([backcast_linspace**i for i in range(thetas_dim)], dtype=torch.float32) - for i in range(thetas_dim)], dtype=torch.float32) - + def __init__( self, units, thetas_dim, From 3b39dbdc1e662fb22083dbdbd40529ffbe2a6b22 Mon Sep 17 00:00:00 2001 From: d-schmitt Date: Mon, 10 Jun 2024 20:28:34 +0200 Subject: [PATCH 3/3] Update sub_modules.py Remove spaces. --- pytorch_forecasting/models/nbeats/sub_modules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_forecasting/models/nbeats/sub_modules.py b/pytorch_forecasting/models/nbeats/sub_modules.py index 7c089734..dc224868 100644 --- a/pytorch_forecasting/models/nbeats/sub_modules.py +++ b/pytorch_forecasting/models/nbeats/sub_modules.py @@ -168,7 +168,7 @@ def forward(self, x) -> Tuple[torch.Tensor, torch.Tensor]: class NBEATSGenericBlock(NBEATSBlock): - def __init__( + def __init__( self, units, thetas_dim,