Downloads · 30 days
0
ash001/timeseries-forecast-lstm
timeseries-forecast-lstm is a time series forecasting model from ash001. Use it for the time series forecasting task on the model card, and read the license before you ship it in a product. It is set up for pytorch. The card lists the license as apache-2.0.
A simple PyTorch LSTM model for univariate time-series forecasting using a sliding window and recursive (multi-step) prediction.
Downloads · 30 days
0
Access
Public
Updated Mar 2, 2026
Repo size
169 KB
Likes
0
Public
Click a slice to open those files.
.pt168 KB · 94%
From the Hugging Face model README
A simple PyTorch LSTM model for univariate time-series forecasting using a sliding window and recursive (multi-step) prediction.
This Hugging Face repo hosts the trained model + scaler used by a Streamlit inference app.
pages/05_Time_Series_Forecast_LSTM.py)artifacts/model_state.pt — PyTorch LSTM state_dictartifacts/scaler.pkl — scaler fitted on the training series (used for scaling + inverse-scaling)artifacts/config.json — key inference settings:
window_size (lookback length)hidden_sizeforecast_horizon (default steps)series_id (which column to use in the CSV)Alcohol_Sales.csv — the example time series used by the demo appwindow_size points as input context.scaler.pkl).N stepsimport json
import numpy as np
import torch
import torch.nn as nn
import pickle
from huggingface_hub import hf_hub_download
REPO_ID = "ash001/timeseries-forecast-lstm"
# --- download artifacts ---
cfg_path = hf_hub_download(REPO_ID, "artifacts/config.json")
pt_path = hf_hub_download(REPO_ID, "artifacts/model_state.pt")
sc_path = hf_hub_download(REPO_ID, "artifacts/scaler.pkl")
cfg = json.load(open(cfg_path, "r"))
window_size = int(cfg["window_size"])
hidden_size = int(cfg["hidden_size"])
with open(sc_path, "rb") as f:
scaler = pickle.load(f)
# --- define model (same structure as Streamlit app) ---
class LSTMnetwork(nn.Module):
def __init__(self, input_size=1, hidden_size=100, output_size=1):
super().__init__()
self.hidden_size = hidden_size
self.lstm = nn.LSTM(input_size, hidden_size)
self.linear = nn.Linear(hidden_size, output_size)
self.hidden = (torch.zeros(1, 1, hidden_size), torch.zeros(1, 1, hidden_size))
def reset_hidden(self):
self.hidden = (torch.zeros(1, 1, self.hidden_size), torch.zeros(1, 1, self.hidden_size))
def forward(self, seq):
lstm_out, self.hidden = self.lstm(seq.view(len(seq), 1, -1), self.hidden)
pred = self.linear(lstm_out.view(len(seq), -1))
return pred[-1]
model = LSTMnetwork(input_size=1, hidden_size=hidden_size, output_size=1)
model.load_state_dict(torch.load(pt_path, map_location="cpu"))
model.eval()
def forecast(series_values: np.ndarray, steps: int):
scaled = scaler.transform(series_values.reshape(-1, 1)).astype(np.float32).reshape(-1)
window = scaled[-window_size:].copy()
preds_scaled = []
for _ in range(steps):
seq = torch.tensor(window, dtype=torch.float32)
model.reset_hidden()
with torch.no_grad():
pred = float(model(seq).item())
preds_scaled.append(pred)
window = np.concatenate([window[1:], [pred]])
preds = scaler.inverse_transform(np.array(preds_scaled).reshape(-1, 1)).reshape(-1)
return preds
# Example:
# series = np.array([...], dtype=np.float32)
# preds = forecast(series, steps=12)