Skip to main content

DataLoaderBuilder

Struct DataLoaderBuilder 

Source
pub struct DataLoaderBuilder<D: Dataset> { /* private fields */ }
Expand description

Builder for DataLoader

Provides a fluent API for configuring DataLoader instances with various options such as batch size, shuffling, number of workers, and memory pinning.

§Examples

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

let dataset = TensorDataset::new(vec![1, 2, 3, 4, 5]);
let dataloader = DataLoaderBuilder::new(dataset)
    .batch_size(32)
    .shuffle(true)
    .num_workers(4)
    .pin_memory(true)
    .drop_last(true)
    .build()?;

Implementations§

Source§

impl<D: Dataset> DataLoaderBuilder<D>

Source

pub fn new(dataset: D) -> Self

Create a new builder

§Arguments
  • dataset - The dataset to create a DataLoader for
Source

pub fn batch_size(self, batch_size: usize) -> Self

Set batch size

§Arguments
  • batch_size - Number of samples per batch
Source

pub fn shuffle(self, shuffle: bool) -> Self

Set whether to shuffle the data

§Arguments
  • shuffle - Whether to randomly shuffle the dataset
Source

pub fn num_workers(self, num_workers: usize) -> Self

Set the number of worker threads

§Arguments
  • num_workers - Number of worker threads for parallel data loading
Source

pub fn pin_memory(self, pin_memory: bool) -> Self

Set whether to pin memory

§Arguments
  • pin_memory - Whether to pin memory for faster GPU transfers
Source

pub fn drop_last(self, drop_last: bool) -> Self

Set whether to drop the last incomplete batch

§Arguments
  • drop_last - Whether to drop the last batch if it’s smaller than batch_size
Source

pub fn timeout(self, timeout: Duration) -> Self

Set timeout for collecting a batch

§Arguments
  • timeout - Maximum time to wait for batch collection
Source

pub fn generator(self, seed: u64) -> Self

Set random generator seed

§Arguments
  • seed - Random seed for reproducible shuffling
Source

pub fn build( self, ) -> Result<DataLoader<D, BatchingSampler<SequentialSampler>, DefaultCollate>>

Build the DataLoader with sequential sampling

Creates a DataLoader that processes the dataset in sequential order. This is the default behavior when shuffle is false or not specified.

Source

pub fn build_with_random_sampling( self, ) -> Result<DataLoader<D, BatchingSampler<RandomSampler>, DefaultCollate>>

Build the DataLoader with random sampling (shuffled)

Creates a DataLoader that randomly shuffles the dataset order. Useful for training scenarios where data order should be randomized.

Source

pub fn build_auto( self, ) -> Result<Box<dyn DataLoaderTrait<D, DefaultCollate> + Send + Sync>>
where D: Send + Sync + 'static, D::Item: Send + Sync + 'static, DefaultCollate: Collate<D::Item>, <DefaultCollate as Collate<D::Item>>::Output: Send,

Build the DataLoader with auto-selected sampling strategy

Automatically chooses between sequential and random sampling based on the shuffle setting configured in the builder.

Auto Trait Implementations§

§

impl<D> Freeze for DataLoaderBuilder<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for DataLoaderBuilder<D>
where D: RefUnwindSafe,

§

impl<D> Send for DataLoaderBuilder<D>

§

impl<D> Sync for DataLoaderBuilder<D>

§

impl<D> Unpin for DataLoaderBuilder<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for DataLoaderBuilder<D>
where D: UnsafeUnpin,

§

impl<D> UnwindSafe for DataLoaderBuilder<D>
where D: 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