Skip to main content

tslens

Which time steps and which features did your time series model actually use?

tslens is a Captum-compatible interpretability toolkit for deep time series models, giving you a consistent PyTorch interface to over a dozen interpretation methods. Unlike methods borrowed from vision and NLP, the ones implemented natively here account for the temporal dependency between neighbouring time steps and score the time and feature dimensions jointly rather than separately.

Docs: khairulislam.github.io/tslens · Paper: arXiv:2412.04532 · Code: github.com/khairulislam/tslens

Why tslens?

Capability tslens Captum Time Interpret TSInterpret
Time-series-native attribution
Classification and regression/forecasting workflows Generic Method-dependent Classification-focused
Individual forecast-horizon explanations Manual Manual
Modern deep forecaster integrations
Tested LLM-backed time-series models

Among these libraries, tslens is the only one with documented, tested integrations spanning recent TSlib architectures and LLM-backed time-series models, while also supporting classification, regression, individual forecast horizons, and multivariate time–feature attribution. A dash means no first-class documented support; custom integration may still be possible. See the full comparison.

Install

pip install tslens

Use

Works with any PyTorch model that maps (batch, seq_len, n_features) to predictions. No training framework to adopt, no dataset format to conform to.

import torch
from tslens import WinTSR

inputs = torch.randn(16, 96, 7)          # (batch, seq_len, n_features)
attr = WinTSR(model).attribute(
    inputs,
    baselines=torch.zeros_like(inputs),
    threshold=0.5,                        # skip the least relevant time steps
)

attr.shape  # (16, n_output, 96, 7) -- (batch, n_output, seq_len, n_features)

Plot it as a heatmap over (seq_len, n_features) and you can read off what the model used.

Options that matter

Argument Effect
threshold Quantile of time-relevance below which steps are skipped in stage two. Higher is faster and sparser; 0.0 keeps every step.
sliding_window_shapes Window over (time, features). Defaults to (1, 1). Widen the first entry to attribute over multi-step windows.
baselines Replacement values for occluded regions. Defaults to zeros; tslens.get_baseline(inputs, "normal") gives other options.
unflatten True (default) returns (batch, n_output, seq_len, n_features). False returns the flat (batch * n_output, ...) layout used internally.
legacy_normalize Constructor flag. Restores the exact normalization used to produce the published numbers — see below.

Multi-input models

Pass a tuple, get a tuple back. This is how you explain a TSlib model (DLinear, iTransformer, TimesNet, ...) — its four forward arguments split into two attributed inputs and two context tensors, with no wrapper class:

attr_enc, attr_mark = WinTSR(model).attribute(
    inputs=(x_enc, x_mark_enc),
    baselines=(torch.zeros_like(x_enc), torch.zeros_like(x_mark_enc)),
    additional_forward_args=(x_dec, x_mark_dec),
)

More recipes

The integration cookbook has copy-paste snippets for dict/tuple model outputs, classification models, baseline choice, explaining a single forecast horizon, speed tuning, and troubleshooting. Two runnable notebooks: quickstart and TSlib models.

Requirements

torch, numpy, captum, and time-interpret.

Citation

Please cite the following if you use this work, also the related paper arXiv:2412.04532.

@software{islam_2026_22088943,
  author       = {Islam, Md Khairul},
  title        = {tslens: A PyTorch Framework for Interpreting Time Series Deep Learning Models},
  month        = aug,
  year         = 2026,
  publisher    = {Zenodo},
  version      = {v1.0.0},
  doi          = {10.5281/zenodo.22088943},
  url          = {https://doi.org/10.5281/zenodo.22088943}
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tslens-1.0.1.tar.gz (31.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tslens-1.0.1-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file tslens-1.0.1.tar.gz.

File metadata

  • Download URL: tslens-1.0.1.tar.gz
  • Upload date:
  • Size: 31.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for tslens-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7178b4f56ff135067566063eb9a8fdbea2c0066b0071e77efe8d52fe8e274f80
MD5 c9f4074712a4f6bcc071523167829df0
BLAKE2b-256 aec45a7c1a9afa8e0575df1e12b8a50380fe1e1b614d0e273ada15498df78d7d

See more details on using hashes here.

Provenance

The following attestation bundles were made for tslens-1.0.1.tar.gz:

Publisher: publish.yml on khairulislam/tslens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tslens-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tslens-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for tslens-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 978ba0402c80151bc93ff0d191f652dd9fc95dcdc67809443f4c6a68d5ac90b2
MD5 ad2454c4131f6a87ca12232ce62419ca
BLAKE2b-256 f528bc1d65a850f50adab0ea9682bd79877408995b8a10a306e63e8845c4e906

See more details on using hashes here.

Provenance

The following attestation bundles were made for tslens-1.0.1-py3-none-any.whl:

Publisher: publish.yml on khairulislam/tslens

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 files

1.0.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page