Skip to main content

WeightedRandomSampler

Struct WeightedRandomSampler 

Source
pub struct WeightedRandomSampler { /* private fields */ }
Expand description

重み付き復元・非復元抽出でバッチ添字を供給する(PyTorch WeightedRandomSampler 相当。イシュー #2182)。構築時 (Self::new)に crate::rng::validate_multinomial で検証する (RNG を消費しない)。start_epoch は公開 API crate::rng::multinomial(1 回の with_global_rng を使う原子的な 抽選)を呼び、同一シードの下で rng::multinomial(&weights, num_samples, replacement) と抽選列が bit 完全一致する。

Implementations§

Source§

impl WeightedRandomSampler

Source

pub fn new( weights: Vec<f32>, num_samples: usize, replacement: bool, batch_size: usize, drop_last: bool, ) -> Result<WeightedRandomSampler, DataError>

weights(rank 1: 各要素が対応する添字の重み)・抽選する サンプル総数 num_samples・復元抽出の有無・バッチ化条件を 受け取る。構築時に有限性・非負・総和が正・非復元抽出時の 重み不足を検証する(crate::rng::validate_multinomial を 再利用。RNG は一切消費しない)。batch_size == 0 は DataError::ZeroBatchSize。

Trait Implementations§

Source§

impl Debug for WeightedRandomSampler

Source§

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

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

impl Sampler for WeightedRandomSampler

Source§

fn start_epoch(&mut self) -> Result<(), DataError>

epoch 開始時に 1 回呼ぶ。抽選を伴う実装はここで確定させる。
Source§

fn next_batch(&mut self) -> Vec<usize>

次バッチの添字列。空の Vec は epoch 終了の番兵。
Source§

fn num_batches(&self) -> Option<usize>

1 epoch あたりのバッチ数(既知なら Some。既定は None)。 SamplerBatches::size_hint・HookedBatches::size_hint は 本メソッドの戻り値(epoch 全体の静的総数)と、各イテレータ自身が 追跡する「これまでに yield した数」の差分で残数を計算する (レビュー指摘: イシュー #2182 review・PR #2310 codex-review P2 是正)。カスタム Sampler 実装が進捗追跡用の別メソッドを override する必要はなく、本メソッドだけを実装すれば途中経過・ epoch 終了後のどちらでも Iterator::size_hint の「残り要素数」 契約(下限の非過大申告)を満たす。

Auto Trait Implementations§

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