Console / terminal sample for time-series prediction with a PyTorch LSTM, supporting Windows 11 and Ubuntu Linux 24.04. A sliding window of --time-seq rows of features predicts the target at the window's last row. The network is an LSTM (hidden 64, 1 layer) plus a linear head, trained full-batch with Adam and MSE loss; inputs are normalized with a MinMaxScaler saved at training time. Two example datasets ship with trained models: sales-forecast-airline (monthly airline passengers, 1949 to 1958, 1 feature) and stock (daily OHLCV, 2015 to 2018, 5 features, predicting the close 5 days ahead).
Disclaimer: the stock example is a machine-learning programming sample, not investment advice.
[Folder Structure]
- CLAUDE.md / AGENT.md: program specification, the two files are kept identical.
- CHANGELOG.md: change log.
- copyright.txt: copyright notice.
- 2604.pdf: the LSTM reference paper.
- data/{dataset}/: the inputs train_input.csv and inference_input.csv (date, features..., target), plus train_output.csv and inference_output.csv written by the scripts (the input columns plus a zero-padded output column).
- model/{dataset}/: model.pth, scaler.pkl, the TensorBoard logs/, and the train_*.png and inference_*.png plots.
- tool/tools.py: shared helpers, project_root, delete_ipynb_checkpoints and resolve_torch_device.
- src/: source code, the launcher scripts and this document.
- venv/: Python virtual environment, created by step 01.
[Instructions]
Every step exists as a Windows batch file (.bat) and a Linux shell script (.sh) with identical behavior. All launchers switch to the project root first, so they can be run from any directory.
1. 01_venv_create.bat / 01_venv_create.sh
Delete the old venv/, create a fresh one, and install src/requirements.txt. Aborts loudly if pip fails.
2. 02_venv_activate.bat / 02_venv_activate.sh
Activate the venv. On Linux, source it: source src/02_venv_activate.sh.
3. 03_train.bat / 03_train.sh
Train the network. Defaults: dataset sales-forecast-airline, 20000 epochs, Adam learning rate 0.01. Extra arguments are forwarded to train.py.
4. 04_tensorboard.bat / 04_tensorboard.sh
Open TensorBoard on model/{dataset}/logs. Positional arguments: [dataset] [port], defaulting to sales-forecast-airline and 6066.
5. 05_inference.bat / 05_inference.sh
Run inference with the trained model. Extra arguments are forwarded to inference.py.
6. 99_venv_clear.bat / 99_venv_clear.sh
Delete the venv/ folder.
[Python Entry Points]
src/train.py
- --dataset sales-forecast-airline: the dataset folder name
- --time-seq 12: the sliding-window length
- --epochs 20000: the number of epochs
- --learning-rate 0.01: the learning rate
- --hidden 64: the LSTM hidden size
- --num-layers 1: the number of LSTM layers
- --device 0: the device id
It writes model.pth, scaler.pkl, fresh TensorBoard logs/, train_fit.png and train_scatter.png to model/{dataset}/, plus data/{dataset}/train_output.csv, and prints RMSE and R². A CUDA request falls back to CPU with a warning.
src/inference.py
- --dataset sales-forecast-airline: the dataset folder name
- --input data/{dataset}/inference_input.csv: the input data path
- --time-seq 12: the sliding-window length
- --hidden 64: the LSTM hidden size
- --num-layers 1: the number of LSTM layers
- --device 0: the device id
The window and architecture arguments must match training. It writes inference_output.csv and the inference_*.png plots, and prints RMSE and R².
src/net.py and src/dataset.py
The shared LSTM definition and the CSV / sliding-window helpers, imported by both scripts.
[Quick Start]
Windows 11
src\01_venv_create.bat
src\03_train.bat --dataset sales-forecast-airline
src\04_tensorboard.bat
src\05_inference.bat --dataset sales-forecast-airline
Ubuntu 24.04
bash src/01_venv_create.sh
bash src/03_train.sh --dataset sales-forecast-airline
bash src/04_tensorboard.sh
bash src/05_inference.sh --dataset sales-forecast-airline
For the stock example, pass --dataset stock. 05_inference also works immediately without training, using the shipped models. CPU training takes about 1 to 2 minutes for airline and about 8 minutes for stock. Training has no fixed random seed, matching the original notebooks, so retrained models differ slightly between runs. TensorBoard opens at http://localhost:6066; stop it with Ctrl+C.
[Dataset Format]
- A CSV with a header row: the first column is a date (plotting only), the last column is the prediction target, and all columns in between are input features. Thousands separators (for example "67,649,997") are stripped automatically.
- A sliding window of --time-seq consecutive rows of features predicts the target at the window's last row; the first time_seq − 1 rows get a zero in the output column of the result CSVs.
- To use your own data, keep the same first and last column convention, put train_input.csv and inference_input.csv under data/{name}/ and pass --dataset {name} (the feature count is detected automatically).
[GPU]
For an NVIDIA GPU, install the torch build matching your driver's CUDA version (check nvidia-smi). For example, with a CUDA 12.8 driver:
pip uninstall -y torch
pip install torch --index-url https://download.pytorch.org/whl/cu128
The scripts fall back to CPU automatically when CUDA is unavailable, and torch.load uses map_location, so a GPU-trained checkpoint loads fine on CPU.
[References]
- LSTM paper (Hochreiter & Schmidhuber 1997, shipped as 2604.pdf): MIT Press
- LSTM applied paper: IET
- PyTorch nn.LSTM: pytorch.org/docs/stable/generated/torch.nn.LSTM.html
- Time-series LSTM tutorial: stackabuse.com
- Stock prediction example: kaggle.com
- PyTorch installation: pytorch.org/get-started/locally
After execution, you can see the results of the LSTM inference.
This SDK is built in AppForAI - AI Dev Tools.
Purchase license separately: USD 600, permanent authorization, single APP authorization, single machine authorization, one-year activation, one-year download, one-year update, one-year email technical support.