dhopp1 / nowcast_lstm Goto Github PK
View Code? Open in Web Editor NEWLSTM neural networks for nowcasting economic data.
License: MIT License
LSTM neural networks for nowcasting economic data.
License: MIT License
Hi, I need help fixing an error. My target value is either 0 or 1. I already tried scaling my data with MinMaxScaler, but the error persists.
Thank you in advance.
def lstm_nowcast(train_data,target,new_data):
from nowcast_lstm.LSTM import LSTM
import pandas as pd
train_data['date'] = pd.to_datetime(train_data.index)
new_data['date'] = pd.to_datetime(new_data.index)
train_data['target'] = target['high_c']
test_data = pd.concat([train_data, new_data], ignore_index=True)
import torch.nn as nn
loss = nn.BCELoss()
model = LSTM(train_data, "target", n_timesteps=12,n_models=3,train_episodes=20,batch_size=32,criterion=loss)
model.train()
result = model.predict(test_data)
print(result)
error :
Traceback (most recent call last):
File "c:/Users/User/Documents/GitHub/test_lstm/main.py", line 175, in
lstm_nowcast(train_data,target,new_data)
File "c:/Users/User/Documents/GitHub/test_lstm/main.py", line 91, in lstm_nowcast
model.train()
File "C:\Users\User.conda\envs\practice\lib\site-packages\nowcast_lstm\LSTM.py", line 144, in train
trained = self.modelling.train_model(
File "C:\Users\User.conda\envs\practice\lib\site-packages\nowcast_lstm\modelling.py", line 138, in train_model
loss = criterion(output.view(-1), batch_y)
File "C:\Users\User.conda\envs\practice\lib\site-packages\torch\nn\modules\module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "C:\Users\User.conda\envs\practice\lib\site-packages\torch\nn\modules\loss.py", line 613, in forward
return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
File "C:\Users\User.conda\envs\practice\lib\site-packages\torch\nn\functional.py", line 2762, in binary_cross_entropy
return torch._C._nn.binary_cross_entropy(input, target, weight, reduction_enum)
RuntimeError: all elements of input should be between 0 and 1
Hi,
would like to suggest adding support for early stopping and choose best weight based on lowest loss or highest accuracy, or highest validation accuracy.
The integration of ARMA (AutoRegressive Moving Average) models with LSTM (Long Short-Term Memory) models is a known approach in time series forecasting. ARMA models capture linear dependencies in time series data, while LSTM models are effective at capturing non-linear and sequential patterns. Combining them can potentially improve forecasting accuracy.
Regarding your mention of ARDL (AutoRegressive Distributed Lag), VAR (Vector AutoRegression), and GARCH (Generalized AutoRegressive Conditional Heteroskedasticity) models with LSTM, it's possible to explore such combinations, but it largely depends on the specific characteristics of your data and the forecasting objectives.
the question is that is it possible to set hybrid model(ARDL-LSTM, GHARGH-LSTM, VAR-LSTM)?
Hello Daniel,
I would appreciate your help. After installation via pip
and the following command
from nowcast_lstm.LSTM import LSTM
I get the this dependency error
raceback (most recent call last):
File "/opt/conda/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/opt/conda/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/etc/share/code-server/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
cli.main()
File "/etc/share/code-server/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/etc/share/code-server/extensions/ms-python.python-2020.10.332292344/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "/opt/conda/lib/python3.8/runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "/opt/conda/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/opt/conda/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/jovyan/test_nowcast_lstm.py", line 3, in <module>
from nowcast_lstm.LSTM import LSTM
File "/opt/conda/lib/python3.8/site-packages/nowcast_lstm/LSTM.py", line 6, in <module>
import nowcast_lstm.data_setup
File "/opt/conda/lib/python3.8/site-packages/nowcast_lstm/data_setup.py", line 5, in <module>
from pmdarima.arima import auto_arima, ARIMA
ModuleNotFoundError: No module named 'pmdarima'
How can I assess the performance of the model? Typically, I employ the model.evaluate() method. However, with your modified LSTM, I'm uncertain about how to obtain the accuracy of the model. because we have Nan values for actual target value
Hi. Great add-in. This is more of a question than issue. When specifying lags (n_timesteps) does it apply this lookback window automatically to just the X variables/features or to the target variable as well? Or would I need to create another variable based on a copy of the target to factor in the target's own lag? Also where can I see details the architecture/parameters of the LTSM model is built on (number of layers, neurons etc.)
Many Thanks
Lucas
In the context of LSTM applied to mixed-frequency data, the initial step involves addressing missing data. This is particularly crucial when dealing with a target variable that follows a monthly frequency, while the features are recorded daily. Before any computations take place, it becomes necessary to impute the missing data in the target variable. To illustrate, consider a scenario where, within a one-month timeframe, there exist 29 instances of missing data. My specific inquiry pertains to the underlying logic employed by the LSTM model in handling such a dataset.
pred_dict = {k: [] for k in lags}
for date in dates:
# training the actual model
train = test.loc[test.date <= str(pd.to_datetime(date) - pd.tseries.offsets.DateOffset(months=3))[:10],:] # data as it would have appeared at beginning of prediction period
model = LSTM(
data = train,
target_variable = target_variable,
n_timesteps = 6,
fill_na_func = np.nanmean,
fill_ragged_edges_func = np.nanmean,
n_models = 10,
train_episodes = 100,
batch_size = 50,
decay = 0.98,
n_hidden = 10,
n_layers = 1,
dropout = 0.0,
criterion = torch.nn.MSELoss(),
optimizer = torch.optim.Adam,
optimizer_parameters = {"lr":1e-2, "weight_decay":0.0}
)
model.train(quiet=True)
for lag in lags:
# the data available for this date at this artificial vintage
tmp_data = gen_lagged_data(metadata, test, date, lag)
# the predict function will give a whole dataframe, only interested in the prediction for this date
pred = model.predict(tmp_data).loc[lambda x: x.date == date, "predictions"].values[0]
pred_dict[lag].append(pred)
There is an error when running the above code, how should I modify it? Thank you!
RuntimeError Traceback (most recent call last)
Cell In[9], line 23
4 train = test.loc[test.date <= str(pd.to_datetime(date) - pd.tseries.offsets.DateOffset(months=3))[:10],:] # data as it would have appeared at beginning of prediction period
6 model = LSTM(
7 data = train,
8 target_variable = target_variable,
(...)
21 optimizer_parameters = {"lr":1e-2, "weight_decay":0.0}
22 )
---> 23 model.train(quiet=True)
25 for lag in lags:
26 # the data available for this date at this artificial vintage
27 tmp_data = gen_lagged_data(metadata, test, date, lag)
File D:\Anaconda3\envs\Pytorch\lib\site-packages\nowcast_lstm\LSTM.py:144, in LSTM.train(self, num_workers, shuffle, quiet)
142 optimizer = instantiated["optimizer"]
143 # train the model
--> 144 trained = self.modelling.train_model(
145 self.X,
146 self.y,
147 mv_lstm,
148 criterion,
149 optimizer,
150 train_episodes=self.train_episodes,
151 batch_size=self.batch_size,
152 decay=self.decay,
153 num_workers=num_workers,
154 shuffle=shuffle,
155 quiet=quiet,
156 )
157 self.mv_lstm.append(trained["mv_lstm"])
158 self.train_loss.append(trained["train_loss"])
File D:\Anaconda3\envs\Pytorch\lib\site-packages\nowcast_lstm\modelling.py:139, in train_model(X, y, mv_lstm, criterion, optimizer, train_episodes, batch_size, decay, num_workers, shuffle, quiet)
136 batch_X, batch_y = batch_X.to(device), batch_y.to(device)
138 mv_lstm.init_hidden(batch_X.size(0))
--> 139 output = mv_lstm(batch_X)
140 loss = criterion(output.view(-1), batch_y)
142 loss.backward()
File D:\Anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
File D:\Anaconda3\envs\Pytorch\lib\site-packages\nowcast_lstm\mv_lstm.py:49, in MV_LSTM.forward(self, x)
46 batch_size, n_timesteps, _ = x.size()
48 # model layers
---> 49 x, self.hidden = self.l_lstm(x, self.hidden)
50 x = x.contiguous().view(batch_size, -1) # make tensor of right dimensions
51 x = self.l_linear(x)
File D:\Anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
File D:\Anaconda3\envs\Pytorch\lib\site-packages\torch\nn\modules\rnn.py:812, in LSTM.forward(self, input, hx)
810 self.check_forward_args(input, hx, batch_sizes)
811 if batch_sizes is None:
--> 812 result = _VF.lstm(input, hx, self._flat_weights, self.bias, self.num_layers,
813 self.dropout, self.training, self.bidirectional, self.batch_first)
814 else:
815 result = _VF.lstm(input, batch_sizes, hx, self._flat_weights, self.bias,
816 self.num_layers, self.dropout, self.training, self.bidirectional)
RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu
Dear Professor Daniel Hopp, when I used your LSTM model code to make prediction, the RMSE I got was about 45,000, which was much larger than the ARMA model. I used the weekly degree data and daily degree data to predict the weekly degree data. May I ask which parameters in the LSTM model should I modify to improve the prediction accuracy? I would appreciate it if you could read and answer my questions carefully!
Hello, when I use the following command to import a package:
from nowcast_lstm.LSTM import LSTM
the following error message appears:
ModuleNotFoundError: No module named 'nowcast_lstm.LSTM'; 'nowcast_lstm' is not a package
How to solve it? Thanks a lot.
Suppose the daily features span from 2010 to 2023, and the target variable covers the same time period. Now, the objective is to predict target values for the initial month of 2024. Could you please guide me on implementing this for an LSTM model, considering that the target variable is recorded monthly while the features are captured daily, thus involving mixed frequencies?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.