Skip to main content

DataLoader

Struct DataLoader 

Source
pub struct DataLoader<D, S, C> { /* private fields */ }
Expand description

DataLoader for batching and iterating over datasets

The DataLoader provides an efficient way to iterate over datasets in batches, with support for parallel loading, shuffling, and various optimization strategies.

§Type Parameters

  • D: Dataset type implementing the Dataset trait
  • S: Sampler type implementing the BatchSampler trait
  • C: Collate function type implementing the Collate trait

§Examples

use torsh_data::dataloader::core::DataLoader;
use torsh_data::dataset::TensorDataset;

let dataset = TensorDataset::new(vec![1, 2, 3, 4, 5]);
let dataloader = DataLoader::builder(dataset)
    .batch_size(2)
    .num_workers(4)
    .build()?;

for batch in dataloader.iter() {
    // Process batch
}

Implementations§

Source§

impl<D: Dataset> DataLoader<D, (), ()>

Source

pub fn builder(dataset: D) -> DataLoaderBuilder<D>

Create a new DataLoader builder

This provides a fluent API for configuring DataLoader options.

§Arguments
  • dataset - The dataset to iterate over
§Returns

A DataLoaderBuilder for configuring the DataLoader

Source§

impl<D, S, C> DataLoader<D, S, C>
where D: Dataset, S: BatchSampler, C: Collate<D::Item>,

Source

pub fn iter(&self) -> DataLoaderIterator<'_, D, S, C>

Create an iterator over the dataset

Returns a DataLoaderIterator that will yield batches according to the configured sampler and collation function.

Source

pub fn len(&self) -> usize

Get the number of batches

Returns the total number of batches that will be produced by this DataLoader, based on the underlying sampler’s batch count.

Source

pub fn is_empty(&self) -> bool

Check if the dataloader is empty

Returns true if the DataLoader will produce zero batches.

Source

pub fn dataset(&self) -> &D

Get the dataset

Source

pub fn sampler(&self) -> &S

Get the sampler

Source

pub fn collate_fn(&self) -> &C

Get the collate function

Source

pub fn num_workers(&self) -> usize

Get the number of workers

Trait Implementations§

Source§

impl<D, S, C> DataLoaderTrait<D, C> for DataLoader<D, S, C>
where D: Dataset + Sync, S: BatchSampler + Sync, C: Collate<D::Item> + Sync, D::Item: Send, C::Output: Send, S::Iter: Iterator<Item = Vec<usize>>,

Source§

fn len(&self) -> usize

Get the number of batches
Source§

fn is_empty(&self) -> bool

Check if the dataloader is empty

Auto Trait Implementations§

§

impl<D, S, C> Freeze for DataLoader<D, S, C>
where D: Freeze, S: Freeze, C: Freeze,

§

impl<D, S, C> RefUnwindSafe for DataLoader<D, S, C>

§

impl<D, S, C> Send for DataLoader<D, S, C>
where D: Send, S: Send, C: Send,

§

impl<D, S, C> Sync for DataLoader<D, S, C>
where D: Sync, S: Sync, C: Sync,

§

impl<D, S, C> Unpin for DataLoader<D, S, C>
where D: Unpin, S: Unpin, C: Unpin,

§

impl<D, S, C> UnsafeUnpin for DataLoader<D, S, C>

§

impl<D, S, C> UnwindSafe for DataLoader<D, S, C>
where D: UnwindSafe, S: UnwindSafe, C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V