pub struct DataLoader<D>{ /* private fields */ }Expand description
DataLoader for batched iteration over datasets.
Provides configurable batching, shuffling, and iteration over datasets.
Implementations§
Source§impl<D> DataLoader<D>
impl<D> DataLoader<D>
Sourcepub fn new(dataset: D, batch_size: usize) -> Self
pub fn new(dataset: D, batch_size: usize) -> Self
Creates a new DataLoader with the specified batch size.
Sourcepub fn drop_last(self, drop_last: bool) -> Self
pub fn drop_last(self, drop_last: bool) -> Self
Sets whether to drop the last incomplete batch.
Sourcepub fn num_workers(self, num_workers: usize) -> Self
pub fn num_workers(self, num_workers: usize) -> Self
Sets the number of worker threads for parallel data loading.
Sourcepub fn batch_size(&self) -> usize
pub fn batch_size(&self) -> usize
Returns the batch size.
Sourcepub fn dataset_len(&self) -> usize
pub fn dataset_len(&self) -> usize
Returns the dataset length.
Sourcepub fn iter(&self) -> DataLoaderIter<'_, D> ⓘ
pub fn iter(&self) -> DataLoaderIter<'_, D> ⓘ
Creates an iterator over batches.
Source§impl<D> DataLoader<D>
impl<D> DataLoader<D>
Sourcepub fn prefetch_to_gpu(&self, device: Device) -> GpuPrefetchIter ⓘ
pub fn prefetch_to_gpu(&self, device: Device) -> GpuPrefetchIter ⓘ
Returns an iterator that prefetches batches onto the target GPU device in a background thread.
This overlaps CPU data loading and collation with GPU computation: while the training loop processes the current GPU-resident batch, the background thread loads and transfers the next batch.
Typically provides 10-30% speedup on GPU training workloads.
§Arguments
device- Target GPU device (e.g.,Device::Cuda(0))
§Example
ⓘ
let loader = DataLoader::new(dataset, 64).shuffle(true);
for batch in loader.prefetch_to_gpu(Device::Cuda(0)) {
// batch.data and batch.targets are already on GPU
}Auto Trait Implementations§
impl<D> Freeze for DataLoader<D>where
D: Freeze,
impl<D> RefUnwindSafe for DataLoader<D>where
D: RefUnwindSafe,
impl<D> Send for DataLoader<D>
impl<D> Sync for DataLoader<D>
impl<D> Unpin for DataLoader<D>where
D: Unpin,
impl<D> UnsafeUnpin for DataLoader<D>where
D: UnsafeUnpin,
impl<D> UnwindSafe for DataLoader<D>where
D: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more