• Version: 20261019
  • Source Code Copyright: Unknown
  • Redistribution Restriction: This source code is integrated by LEADERG INC. Please use it privately. Please DO NOT distribute it to internet.

Console/terminal sample for regression with a small dense (fully connected) PyTorch network: predicting a stock's close price 5 days ahead from daily volume/open/high/low/close, supporting Windows 11, Ubuntu Linux 24.04 and macOS 14+ (Apple Silicon). The network is 5 → 90 → 30 → 1 with tanh activations, trained full-batch with Adam and MSE loss. Inputs are normalized with a MinMaxScaler saved at training time and reloaded for inference. A trained model.pth + scaler.pkl ship with the project, so inference works out of the box. Training and inference run on the GPU — CUDA on Windows/Linux, MPS on Apple Silicon — and fall back to the CPU automatically.

Disclaimer: this is a machine-learning programming sample, not investment advice. Do not make financial decisions based on its output.

 

Jupyter-Data-Dense-Stock-PyTorch-inference.png

 

[Folder Structure]

  • CLAUDE.md / AGENTS.md: Program specification (identical content).
  • CHANGELOG.md: Change log.
  • copyright.txt: Copyright notice.
  • Data-Dense-Stock-PyTorch-Python.html: This document (usage page).
  • data/stock/: Example dataset (809 daily rows, 2015–2018): train_input.csv, inference_input.csv with columns date,volume,open,high,low,close,"close after 5 days". First column = date (plotting only), last column = regression target.
  • model/stock/: Trained artifacts and outputs: model.pth, scaler.pkl, train_*.png / inference_*.png plots, inference_output.csv.
  • tool/tools.py: Shared helpers: project_root, delete_ipynb_checkpoints, select_device.
  • src/: Source code (train.py, inference.py, net.py, dataset.py), launcher scripts and requirements.txt.
  • venv/: Python virtual environment, created by 02_venv_create_windows/linux/mac.

[Prerequisites]

  • Python 3.14, installed by 01_python_install_windows.bat (winget), 01_python_install_linux.sh (apt, or the deadsnakes PPA when the Ubuntu archive has no python3.14; needs sudo) or 01_python_install_mac.sh (Homebrew).
  • 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh create venv/ with Python 3.14, install PyTorch 2.13.0 first, then pandas, numpy, matplotlib and scikit-learn from src/requirements.txt.
  • GPU (Windows / Linux): the venv scripts install the CUDA 13.0 (cu130) build. It uses an NVIDIA GPU of the Turing generation or newer (GTX 16 / RTX 20 and later) with a driver supporting CUDA 13.0+ (check nvidia-smi); without one, the same build runs on the CPU.
  • macOS: the PyPI build uses the Apple GPU through MPS. PyTorch 2.13.0 ships macOS wheels for Apple Silicon (arm64, macOS 14+) only; Intel Macs are not supported.
  • pandas, scikit-learn and matplotlib have no GPU backend (CPU only); only the network runs on the GPU.
  • Windows 11 uses the .bat launchers; Ubuntu 24.04 and macOS use the .sh launchers.

[Launcher Scripts]

Every step exists as a Windows batch file (.bat) and a shell script (.sh, shared by Linux and macOS) with identical behavior. All launchers switch to the project root first, so they can be run from any directory. Extra arguments are forwarded to the Python script (e.g. src\05_inference.bat --device cpu).

1. 01_python_install_windows.bat / 01_python_install_linux.sh / 01_python_install_mac.sh

Install Python 3.14 (winget / apt or deadsnakes PPA, needs sudo / Homebrew). Skips the install when Python 3.14 is already present. On Windows, open a new terminal afterwards.

 

2. 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh

Delete the old venv/, create a fresh one with Python 3.14, install PyTorch 2.13.0 (cu130 on Windows/Linux, PyPI/MPS build on macOS), then src/requirements.txt. Aborts loudly if pip fails; prints whether CUDA (or MPS) is available.

 

3. 03_venv_activate.bat / 03_venv_activate.sh

Activate the venv. On Linux/macOS, source it: source src/03_venv_activate.sh (bash or zsh). If the project folder was renamed after the venv was created, re-create the venv with 02_venv_create_windows/linux/mac first; the other launchers do not need activation.

 

4. 04_train.bat / 04_train.sh

Train the network (defaults: stock, 50000 epochs, Adam lr 0.01). Extra args are forwarded to train.py.

 

5. 05_inference.bat / 05_inference.sh

Run inference with the trained model. Extra args are forwarded to inference.py.

 

6. 99_venv_clean.bat / 99_venv_clean.sh

Delete the venv/ folder.

 

7. 99_venv_deactivate.bat / 99_venv_deactivate.sh

Deactivate the venv in the current shell. On Linux/macOS, source it: source src/99_venv_deactivate.sh (bash or zsh).

[Python Entry Points]

src/train.py

--dataset stock --epochs 50000 --learning-rate 0.01 --hidden 30 --device auto. Optimizer: Adam. Writes model.pth (CPU tensors), scaler.pkl, train_fit.png (actual vs predicted over time), train_scatter.png to model/{dataset}/ and prints the loss every 100 epochs plus RMSE / R-squared on the training set (console output only).

 

src/inference.py

--dataset stock --input data/{dataset}/inference_input.csv --hidden 30 --device auto. --hidden must match training. Writes inference_output.csv (input columns + predicted), inference_fit.png, inference_scatter.png to model/{dataset}/ and prints RMSE / R-squared against the target column.

 

src/net.py

The shared Net model class (n_feature → n_hidden*3 → n_hidden → n_output, tanh) imported by both scripts.

 

src/dataset.py

The shared CSV loader: strips thousands separators from volume, splits off the leading date column for the time-axis plots, exits with a clear report on NaN values.

 

--device (both scripts): auto (default) picks CUDA → MPS → CPU; cuda, cuda:N, mps or cpu forces a device (a bare GPU index such as 0 means cuda:0). A forced CUDA/MPS device that is not available falls back to the CPU with a warning. The selected device is printed once at startup (Device: cuda).

[Quick Start]

Windows 11

src\01_python_install_windows.bat

REM open a new terminal

src\02_venv_create_windows.bat

src\04_train.bat

src\05_inference.bat

 

Ubuntu 24.04

bash src/01_python_install_linux.sh

bash src/02_venv_create_linux.sh

bash src/04_train.sh

bash src/05_inference.sh

 

macOS 14+ (Apple Silicon)

bash src/01_python_install_mac.sh

bash src/02_venv_create_mac.sh

bash src/04_train.sh

bash src/05_inference.sh

A full 50000-epoch training run takes about 2 to 3 minutes (see GPU). 05_inference also works immediately without training, using the shipped model.

[Dataset Format]

  • CSV with a header row: the first column is a date (plotting only, not a feature), the last column is the regression target, all columns in between are input features.
  • The example stock dataset: date,volume,open,high,low,close,"close after 5 days". The volume column may contain thousands separators ("67,649,997"); they are stripped automatically.
  • To use your own data, keep the same first/last column convention, put train_input.csv / inference_input.csv under data/{name}/ and pass --dataset {name}; adjust --hidden as needed (same value for training and inference).

[GPU]

  • Device order: CUDA → MPS → CPU (select_device in tool/tools.py), overridable with --device.
  • Windows / Linux: the venv scripts install the CUDA 13.0 (cu130) build directly — no extra step. It needs a Turing-or-newer NVIDIA GPU and a driver supporting CUDA 13.0+. Pascal/Volta GPUs (GTX 10xx, V100) are not supported by cu130; on those, reinstall torch from the cu126 index inside the venv: pip install torch==2.13.0 --index-url https://download.pytorch.org/whl/cu126 --force-reinstall
  • macOS (Apple Silicon): the network runs on MPS; PYTORCH_ENABLE_MPS_FALLBACK=1 is set by both scripts so ops MPS lacks fall back to the CPU. All tensors are float32 (MPS has no float64).
  • Without a usable GPU everything runs on the CPU.
  • The network has only ~3,300 parameters and 809 training rows, so the GPU gives only a small speed-up here: on the test PC (NVIDIA Quadro RTX 5000) a full 50000-epoch run took about 125 s on CUDA and about 165 s with --device cpu (including start-up). Pass --device cpu if you prefer the CPU.
  • train.py saves the weights as CPU tensors and inference.py loads them with map_location="cpu" before moving the network to the selected device, so checkpoints trained on CUDA, MPS or CPU load on any of them.

[Troubleshooting]

  • InconsistentVersionWarning when loading scaler.pkl: the shipped model/stock/scaler.pkl was pickled with an older scikit-learn release than the one the venv installs. The warning is harmless (the shipped model still reports RMSE 3.92575, R-squared 0.98953); run 04_train once to regenerate model.pth and scaler.pkl.
  • Device: cpu although the PC has an NVIDIA GPU: the cu130 build needs a driver supporting CUDA 13.0+ (nvidia-smi) and a Turing-or-newer GPU. Force --device cuda to get a warning when CUDA is not usable.

[References]

 

After execution, you can see the results of the neural network inference.

 

This SDK is built in AppForAI - AI Dev Tools.