Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Sktime Pytorch forecasting TimeSeriesDataSet To Dataloader

From Leeroopedia


Knowledge Sources
Domains Time_Series, Data_Engineering, Deep_Learning
Last Updated 2026-02-08 07:00 GMT

Overview

Concrete tool for constructing PyTorch DataLoaders from TimeSeriesDataSet instances provided by the pytorch-forecasting library.

Description

The TimeSeriesDataSet.to_dataloader method constructs a PyTorch DataLoader with appropriate defaults for time series training and evaluation. For training mode (train=True), it enables shuffling and drops the last incomplete batch. For evaluation mode (train=False), it uses sequential sampling. It supports a special synchronized batch sampler mode that ensures all samples in a batch share the same decoder time index, which is useful for models that exploit cross-series patterns.

Usage

Call this method on both training and validation TimeSeriesDataSet instances to produce DataLoaders for Trainer.fit(). The batch_size parameter should be tuned based on available GPU memory. Typical values range from 32 to 128.

Code Reference

Source Location

  • Repository: pytorch-forecasting
  • File: pytorch_forecasting/data/timeseries/_timeseries.py
  • Lines: L2540-2663

Signature

def to_dataloader(
    self,
    train: bool = True,
    batch_size: int = 64,
    batch_sampler: Sampler | str = None,
    **kwargs,
) -> DataLoader:
    """
    Construct dataloader from dataset.

    Parameters
    ----------
    train : bool, optional, default=True
        Whether for training (shuffle, drop_last) or prediction.
    batch_size : int, optional, default=64
        Batch size for training.
    batch_sampler : Sampler, str, or None, optional, default=None
        "synchronized" for time-aligned batches, or any PyTorch Sampler.
    **kwargs : additional arguments passed to DataLoader constructor.

    Returns
    -------
    DataLoader
    """

Import

from pytorch_forecasting import TimeSeriesDataSet
# Then call: dataset.to_dataloader(train=True, batch_size=64)

I/O Contract

Inputs

Name Type Required Description
train bool No Training mode (shuffle + drop_last) vs eval mode (default: True)
batch_size int No Number of samples per batch (default: 64)
batch_sampler Sampler or str No "synchronized" for time-aligned batching, or a custom Sampler
**kwargs dict No Additional arguments for PyTorch DataLoader (e.g., num_workers)

Outputs

Name Type Description
return DataLoader DataLoader yielding (x_dict, (y, weight)) tuples per batch

The x_dict contains keys: encoder_cat, encoder_cont, encoder_target, encoder_lengths, decoder_cat, decoder_cont, decoder_target, decoder_lengths, decoder_time_idx, groups, target_scale.

Usage Examples

Standard Train/Val DataLoaders

# Create DataLoaders from existing datasets
train_dataloader = training.to_dataloader(train=True, batch_size=64, num_workers=0)
val_dataloader = validation.to_dataloader(train=False, batch_size=64, num_workers=0)

# Inspect a batch
x, (y, weight) = next(iter(train_dataloader))
print(f"Encoder shape: {x['encoder_cont'].shape}")  # (batch, encoder_len, features)
print(f"Target shape: {y.shape}")  # (batch, prediction_len)

N-BEATS with Larger Batch Size

# N-BEATS typically uses larger batches
train_dataloader = training.to_dataloader(train=True, batch_size=128, num_workers=2)
val_dataloader = validation.to_dataloader(train=False, batch_size=128, num_workers=2)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment