tracel-ai/burn · error
Index out of bounds for InMemDataset: {index} >= {}
Error message
Index out of bounds for InMemDataset: {index} >= {} What it means
`InMemDataset::get` indexes into an in-memory `Vec` of items and panics when `index` is beyond the dataset length instead of returning a `DatasetError`. It is thrown from the `Dataset` trait's `get` implementation, so any code doing `dataset.get(i)` with `i >= dataset.len()` aborts.
Source
Thrown at crates/burn-dataset/src/dataset/in_memory.rs:30
pub struct InMemDataset<I> {
items: Vec<I>,
}
impl<I> InMemDataset<I> {
/// Creates a new in memory dataset from the given items.
pub fn new(items: Vec<I>) -> Self {
InMemDataset { items }
}
}
impl<I> Dataset<I> for InMemDataset<I>
where
I: Clone + Send + Sync,
{
fn get(&self, index: usize) -> Result<I, DatasetError> {
match self.items.get(index) {
Some(item) => Ok(item.clone()),
None => panic!(
"Index out of bounds for InMemDataset: {index} >= {}",
self.items.len()
),
}
}
fn len(&self) -> usize {
self.items.len()
}
}
impl<I> InMemDataset<I>
where
I: Clone + DeserializeOwned,
{
/// Create from a dataset. All items are loaded in memory.
pub fn from_dataset<E>(dataset: &impl Dataset<I, E>) -> Self
where
E: std::error::Error + Send + Sync + 'static,View on GitHub (pinned to d16f7ba2ed)
Solutions
- Check `index < dataset.len()` before calling `get`, or iterate with a bounded range `0..dataset.len()`.
- Verify the dataset loaded as expected (log `dataset.len()`; an unexpected 0 means the source file/path/records are wrong).
- Use `dataset.get(...)` only through APIs that respect bounds (e.g. iterators, sampler with replacement=... sized correctly).
- Catch/avoid upstream: prefer `Dataset::iter()` or window/sampler transforms that compute valid indices.
Example fix
// before
for i in 0..num_samples {
let item = dataset.get(i).unwrap(); // panics when i >= dataset.len()
}
// after
let num_samples = dataset.len().min(num_samples);
for i in 0..num_samples {
let item = dataset.get(i)?;
} Defensive patterns
Strategy: validation
Validate before calling
if index >= dataset.len() {
return Err(DatasetError::InvalidArgument(format!("index {index} out of bounds (len={})", dataset.len())));
}
let item = dataset.get(index)?; Try / catch
// panics, not Result; pre-check instead
let item = if index < dataset.len() { Some(dataset.get(index).ok()) } else { None }; Prevention
- Always derive loop bounds from `dataset.len()` at the call site
- Beware `0..=len` off-by-one loops; use half-open ranges
- Log dataset length after loading to catch empty/partially loaded datasets
- Prefer `dataset.iter()` over manual indexing where possible
When it happens
Trigger: Calling `dataset.get(index)` (directly or via iterators/batching/transforms) where `index >= InMemDataset::len()`; empty dataset accessed with index 0; off-by-one loops like `for i in 0..=dataset.len()`.
Common situations: Splitting data manually with wrong bounds; loading an empty file/record source producing a zero-length dataset; batch-size loops that compute `len/ batch_size` and then index the remainder without a guard; changed dataset size after data updates.
Related errors
- 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} >= {}
- Index out of bounds for WindowsDataset: {index} >= {}
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/552ede30136e00a7.
Report an issue: GitHub.