pub struct DataLoader<D> { /* private fields */ }Expand description
DataLoader that iterates a dataset with shuffling, batching, and prefetching.
Operates on CPU tensors only. Transfer the resulting batches to GPU
with Tensor::to_device() in the training loop for efficient bulk transfer.
§Lifecycle
let loader = DataLoader::new(dataset, batch_size, seed, &device);
for epoch in 0..num_epochs {
for batch in loader.iter(epoch) {
let batch = batch?;
// batch.inputs: [batch_size, ...]
// batch.targets: [batch_size, ...]
}
}Implementations§
Source§impl<D> DataLoader<D>where
D: Dataset<CpuRuntime>,
impl<D> DataLoader<D>where
D: Dataset<CpuRuntime>,
Sourcepub fn new(dataset: D, batch_size: usize, seed: u64, device: CpuDevice) -> Self
pub fn new(dataset: D, batch_size: usize, seed: u64, device: CpuDevice) -> Self
Create a new DataLoader.
§Arguments
dataset- The dataset to iteratebatch_size- Number of samples per batchseed- Random seed for deterministic shufflingdevice- CPU device for tensor creation
Sourcepub fn with_prefetch(self, count: usize) -> Self
pub fn with_prefetch(self, count: usize) -> Self
Set the number of batches to prefetch in the background thread.
Default is 2. Set to 0 to disable prefetching (synchronous iteration).
Sourcepub fn num_batches(&self) -> usize
pub fn num_batches(&self) -> usize
Number of batches per epoch.
Sourcepub fn iter(&self, epoch: u64) -> DataLoaderIter<'_, D> ⓘ
pub fn iter(&self, epoch: u64) -> DataLoaderIter<'_, D> ⓘ
Create a synchronous iterator for one epoch.
Indices are shuffled deterministically from seed + epoch.
The last incomplete batch (if any) is dropped.
Source§impl<D> DataLoader<D>
impl<D> DataLoader<D>
Sourcepub fn prefetch_iter(&self, epoch: u64) -> PrefetchIter ⓘ
pub fn prefetch_iter(&self, epoch: u64) -> PrefetchIter ⓘ
Create a prefetching iterator that loads batches in a background thread.
The background thread loads up to prefetch_count batches ahead.
Requires D: Clone because the dataset is moved into the background thread.
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>where
D: Send,
impl<D> Sync for DataLoader<D>where
D: Sync,
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> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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>
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>
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.