Skip to main content

DataLoader

Struct DataLoader 

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

dataset を DataLoaderConfig に従ってミニバッチへ分割する (PyTorch torch.utils.data.DataLoader 相当)。

Implementations§

Source§

impl<D> DataLoader<D>
where D: Dataset,

Source

pub fn new( dataset: D, config: DataLoaderConfig, ) -> Result<DataLoader<D>, DataError>

config.batch_size == 0 は DataError::ZeroBatchSize。 dataset.validate() が失敗した場合はその結果をそのまま返す (タプルデータセットの長さ不一致を構築時に fail-closed に拒否 する。モジュール冒頭 A08 注記)。

Source

pub fn dataset(&self) -> &D

保持するデータセットへの参照。

Source

pub fn config(&self) -> DataLoaderConfig

構成のコピー(DataLoaderConfig: Copy)。

Source

pub fn len(&self) -> usize

1 epoch あたりのバッチ数。

Source

pub fn is_empty(&self) -> bool

len() == 0 か。

Source

pub fn iter(&self) -> Batches<'_, D> ⓘ

1 epoch 分のイテレータを返す。shuffle=true のときは呼び出し ごとに順列を引き直す(PyTorch の epoch ごと再シャッフルと同義。 モジュール冒頭「シャッフル契約」参照)。

n = self.dataset.len() から確定する添字順列 Vec<usize> は checked_numel_for::<usize>(&[n]) で確保前にバイトサイズまで 検査してから構築する。Dataset::len() は実装者が任意の値を 返せるため(例: 要素数ゼロの Tensor::new(vec![], &[usize::MAX, 0]) から len() == usize::MAX のデータセットを作れる)、検査 なしに (0..n).collect()/shuffled_indices の Vec::with_ capacity を呼ぶと numel * size_of::<usize>() > isize::MAX で capacity overflow パニックしうる(本番経路 panic 禁止規約 .claude/rules/coding-rust.md。PR #1867 codex-review P1 是正)。

この iter() 自体は(設計どおり)Result を返さない infallible な API のまま維持する。検査に失敗した場合は order を空のまま にし、shuffle=true でも with_global_rng を呼ばず(RNG を一切 消費しない)、DataError::Shape(ShapeError:: ElementCountOverflow)を Batches へ持ち越して最初の next() 呼び出しで 1 回だけ Err として yield し、以降は None を返す(ExactSizeIterator::len() もこの 1 件に整合させ る。下記 Batches::next 参照)。

Source

pub fn into_dataset(self) -> D

内部の dataset を取り出す。

Trait Implementations§

Source§

impl<D> Debug for DataLoader<D>
where D: Dataset,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a, D> IntoIterator for &'a DataLoader<D>
where D: Dataset,

Source§

type Item = Result<<D as Dataset>::Batch, DataError>

The type of the elements being iterated over.
Source§

type IntoIter = Batches<'a, D>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> <&'a DataLoader<D> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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