TensorFlow
Dataset API
sliding window
batch processing
machine learning

Sliding window of a batch in Tensorflow using Dataset API

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Sliding windows are a core pattern for time series, NLP context frames, and sequence forecasting. In TensorFlow, the tf.data API provides an efficient way to build these windows without pre-materializing all samples in memory. The main idea is to transform a one-dimensional or multi-feature stream into overlapping chunks of fixed length, then map each chunk into (features, label) pairs. Most implementation problems come from the order of window, flat_map, batch, and map, or from mismatched label offsets.

Core Sections

Build a basic sliding-window dataset

A typical pipeline for one-step forecasting looks like this.

python
1import tensorflow as tf
2
3series = tf.range(20, dtype=tf.float32)
4window_size = 5
5batch_size = 4
6
7# Windows of length window_size + 1 so last item can be label.
8ds = tf.data.Dataset.from_tensor_slices(series)
9ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
10ds = ds.flat_map(lambda w: w.batch(window_size + 1))
11ds = ds.map(lambda w: (w[:-1], w[-1]))
12ds = ds.batch(batch_size).prefetch(tf.data.AUTOTUNE)
13
14for x, y in ds.take(1):
15    print(x.shape, y.shape)

This creates overlapping windows and splits each into input sequence and next-step target.

Multi-feature sequences

If input is shape (time, features), the pattern is similar, but labels may be one feature or all features.

python
1data = tf.random.normal((100, 3))
2
3ds = tf.data.Dataset.from_tensor_slices(data)
4ds = ds.window(11, shift=1, drop_remainder=True)
5ds = ds.flat_map(lambda w: w.batch(11))
6# Predict next step for all 3 features
7nds = ds.map(lambda w: (w[:-1], w[-1]))

Be explicit about whether you predict one scalar, one feature vector, or a future horizon.

Performance ordering rules

For large datasets, order transforms carefully:

  1. create windows,
  2. flatten to tensors,
  3. map to features/labels,
  4. shuffle (if appropriate),
  5. batch,
  6. prefetch.

Shuffling before windows usually breaks temporal assumptions. For autoregressive tasks, shuffle windows but not timesteps inside a window.

Debug shape and offset early

Use take() and print values/shapes before model training. It is much cheaper to debug dataset offsets than training a model on misaligned labels.

python
for w in tf.data.Dataset.from_tensor_slices(series).window(6, 1, True).take(1):
    print(list(w.as_numpy_iterator()))

Integrate with Keras

Once ds yields correctly shaped batches, pass it directly to model.fit. Keep model input shape aligned with window length and feature count.

Common Pitfalls

  • Using window(window_size) instead of window(window_size + 1) for next-step labels.
  • Forgetting flat_map(...batch(...)), leaving nested datasets that models cannot consume.
  • Shuffling raw timesteps instead of complete windows, which destroys sequence meaning.
  • Building labels with wrong index (w[0] or w[:-1]) and training on shifted targets.
  • Ignoring dataset shape checks before training and debugging only after poor metrics.

Implementation Playbook

Treat dataset construction as a tested component, not glue code. Define a small deterministic fixture series and expected windows, then assert exact outputs in unit tests. Include at least one edge-case fixture where data length is smaller than window size to verify behavior is explicit (drop, pad, or error). In CI, run a smoke training step with two mini-batches to ensure dataset and model input signatures remain compatible after refactors.

For production workloads, monitor input pipeline throughput and latency. A correct but slow pipeline can starve accelerators and reduce end-to-end training performance dramatically. Use TensorFlow profiler to confirm prefetch and parallelism are effective. If windows are expensive to produce repeatedly, evaluate caching on preprocessed steps where memory allows.

text
11. Validate expected windows on toy sequence
22. Assert feature/label offsets explicitly
33. Run mini fit() smoke test in CI
44. Profile pipeline throughput under load
55. Document window and label conventions
66. Re-verify after dependency upgrades

Operational Readiness

Converting a technically correct implementation into a reliable production behavior requires explicit operational guardrails. Begin by defining success criteria in measurable terms: expected output shape, acceptable latency range, and acceptable failure rate under normal load. Then build a minimal verification harness that exercises the same code path with deterministic fixtures so behavioral drift is detected early when dependencies or runtime versions change. This harness should run quickly enough to execute on every change and should fail loudly when assumptions break.

Next, establish observability that captures both correctness and health. Structured logs should include correlation identifiers, key decision branches, and error classifications. Metrics should track throughput, latency percentiles, and error categories relevant to this workflow. If external integrations are involved, include dependency status and timeout counters so incident triage can isolate whether failures originate locally or downstream. Avoid relying on manual spot checks because intermittent regressions are often timing-sensitive and disappear outside repeatable test conditions.

Finally, define a controlled rollout and rollback process. Deploy incrementally, compare live metrics against baseline, and keep rollback criteria explicit before release starts. Store configuration assumptions in a short runbook so future maintainers can reproduce intended behavior quickly. A disciplined rollout model dramatically reduces recovery time when unexpected behavior appears after infrastructure, network, or platform changes.

text
11. Define measurable success and failure thresholds
22. Run deterministic fixture-based smoke checks
33. Capture structured logs and core metrics
44. Validate downstream dependency behavior
55. Roll out incrementally with explicit rollback triggers
66. Keep runbook assumptions current

Summary

Sliding windows with tf.data are powerful and scalable when you compose transformations in the right order and verify offsets early. Use window plus flat_map to generate overlapping chunks, split to (x, y) deterministically, and optimize with batching and prefetching. A small validation harness prevents subtle sequence bugs from leaking into expensive training runs.


Course illustration
Course illustration

All Rights Reserved.