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}
}

Metadata

Release files for tslens 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tslens 1.0.1
File Size Uploaded
tslens-1.0.1.tar.gz 31.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tslens 1.0.1
File Interpreter ABI Platform
tslens-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 56.0 kB

Release files / tslens-1.0.1.tar.gz

Download URL tslens-1.0.1.tar.gz
Size 31.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7178b4f56ff135067566063eb9a8fdbea2c0066b0071e77efe8d52fe8e274f80
BLAKE2b-256 checksum
How to use checksums
aec45a7c1a9afa8e0575df1e12b8a50380fe1e1b614d0e273ada15498df78d7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release files / tslens-1.0.1-py3-none-any.whl

Download URL tslens-1.0.1-py3-none-any.whl
Size 24.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
978ba0402c80151bc93ff0d191f652dd9fc95dcdc67809443f4c6a68d5ac90b2
BLAKE2b-256 checksum
How to use checksums
f528bc1d65a850f50adab0ea9682bd79877408995b8a10a306e63e8845c4e906
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.1.1

2 release files

0.1.0

2 release 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