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
impl WeightedRandomSampler
Sourcepub fn new(
weights: Vec<f32>,
num_samples: usize,
replacement: bool,
batch_size: usize,
drop_last: bool,
) -> Result<WeightedRandomSampler, DataError>
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
impl Debug for WeightedRandomSampler
Source§impl Sampler for WeightedRandomSampler
impl Sampler for WeightedRandomSampler
Source§fn next_batch(&mut self) -> Vec<usize>
fn next_batch(&mut self) -> Vec<usize>
次バッチの添字列。空の
Vec は epoch 終了の番兵。Source§fn num_batches(&self) -> Option<usize>
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§
impl Freeze for WeightedRandomSampler
impl RefUnwindSafe for WeightedRandomSampler
impl Send for WeightedRandomSampler
impl Sync for WeightedRandomSampler
impl Unpin for WeightedRandomSampler
impl UnsafeUnpin for WeightedRandomSampler
impl UnwindSafe for WeightedRandomSampler
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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> ⓘ
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 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> ⓘ
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