tracel-ai/burn · error
Index out of bounds for WindowsDataset: {index} >= {}
Error message
Index out of bounds for WindowsDataset: {index} >= {} What it means
`WindowsDataset::get` asks the wrapped dataset for a sliding window of `self.size` items starting at `index`; when the window provider returns `None` (the window would run past the end of the underlying data), it panics with the index and the number of windows (`self.len()`). The dataset exposes `stride * (n - size) + 1` valid windows; anything beyond that is invalid.
Source
Thrown at crates/burn-dataset/src/transform/window.rs:171
I: Send + Sync,
{
/// Retrieves a window of items from the dataset.
///
/// # Parameters
///
/// - `index`: The index of the window.
///
/// # Returns
///
/// A vector representing the window.
///
/// # Panics
///
/// Panics if `index >= len()`.
fn get(&self, index: usize) -> Result<Vec<I>, DatasetError> {
match self.dataset.window(index, self.size)? {
Some(window) => Ok(window),
None => panic!(
"Index out of bounds for WindowsDataset: {index} >= {}",
self.len()
),
}
}
/// Retrieves the number of windows in the dataset.
///
/// # Returns
///
/// A size representing the number of windows.
fn len(&self) -> usize {
let len = self.dataset.len() as isize - self.size.get() as isize + 1;
max(len, 0) as usize
}
}
#[cfg(test)]View on GitHub (pinned to d16f7ba2ed)
Solutions
- Iterate `0..windows.len()` and let `WindowsDataset` compute valid window count.
- If computing manually, use `(data_len - size) / stride + 1` (for size >= 1) rather than `data_len / stride`.
- Guard: `if index < windows.len() { windows.get(index) } else { skip }`.
- Verify `size` <= underlying dataset length; otherwise `len()` is 0 and any access panics.
Example fix
// before
let n = underlying.len() / stride; // ignores window size
for i in 0..n { let w = windows.get(i).unwrap(); }
// after
let n = windows.len(); // accounts for size and stride
for i in 0..n { let w = windows.get(i)?; } Defensive patterns
Strategy: validation
Validate before calling
let n_windows = windows.len(); // (src_len - size) / stride + 1
if index < n_windows {
let window = windows.get(index)?;
} else {
// out of range: handle or skip Prevention
- Always take the window count from `windows.len()` rather than dividing source length by stride
- Verify `size <= source.len()`; otherwise the dataset is empty and any access panics
- Update loop bounds when changing window size/stride configuration
- Prefer iterating the WindowsDataset directly (it implements Dataset with a correct len) over recomputing bounds
When it happens
Trigger: Calling `windows.get(index)` with `index >= windows.len()`; computing the number of windows yourself as `data.len()/stride` instead of using `windows.len()` (which accounts for window size); iterating with the source dataset's length; stride/size changed but loop bounds not updated.
Common situations: Time-series datasets where window size + stride interact and the last full window bounds differ from naive division; DataLoader workers using stale lengths; changing `size` or `stride` in config without updating expectations of `len()`.
Related errors
- Index out of bounds for InMemDataset: {index} >= {}
- Index out of bounds for ComposedDataset: {index} >= {}
- Index out of bounds for wrapped dataset size: {idx} >= {size
- Index out of bounds for SelectionDataset: {index} >= {}
- Index out of bounds for SelectionDataset: {i} >= {}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/ba63fff2355a8dac.
Report an issue: GitHub.