pub struct ModuleDict { /* private fields */ }Expand description
PyTorch nn.ModuleDict 相当。キー付きの子 Module 保持器(イシュー #2402。
autodiff ModuleDict〈#2134〉の鏡写し)。
内部は挿入順を保つ Vec<(String, Box<dyn Module>)>(列挙順が「登録順」契約のため
HashMap にしない。検索は O(n) だが層数は高々数十)。forward は常に
AutodiffError::InvalidArgument(保持器のみ)。キーは空文字列と '.' 含みを拒否する。
Implementations§
Source§impl ModuleDict
impl ModuleDict
Sourcepub fn from_pairs(
pairs: Vec<(String, Box<dyn Module>)>,
) -> Result<Self, AutodiffError>
pub fn from_pairs( pairs: Vec<(String, Box<dyn Module>)>, ) -> Result<Self, AutodiffError>
(key, module) 列から構築する(挿入順を保持)。不正キーがあれば Err(panic しない)。
Sourcepub fn insert(
&mut self,
key: impl Into<String>,
module: Box<dyn Module>,
) -> Result<Option<Box<dyn Module>>, AutodiffError>
pub fn insert( &mut self, key: impl Into<String>, module: Box<dyn Module>, ) -> Result<Option<Box<dyn Module>>, AutodiffError>
key の子を挿入する。同名キーがあれば置換して旧値を返す(位置は保つ)。
不正キー(空・'.' 含み)は Err。
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
key を保持しているか。
Trait Implementations§
Source§impl Default for ModuleDict
impl Default for ModuleDict
Source§impl Module for ModuleDict
impl Module for ModuleDict
Source§fn set_parameter(
&mut self,
name: &str,
value: Tensor<f32>,
) -> Result<(), AutodiffError>
fn set_parameter( &mut self, name: &str, value: Tensor<f32>, ) -> Result<(), AutodiffError>
先頭の '.' までをキーとして子へ委譲する。区切りなし・未知キーは InvalidArgument。
Source§fn set_requires_grad(
&mut self,
requires_grad: bool,
) -> Result<(), AutodiffError>
fn set_requires_grad( &mut self, requires_grad: bool, ) -> Result<(), AutodiffError>
挿入順に全子へ伝播する。ロールバック・集約は ModuleList と同一
(set_requires_grad_with_rollback。#2402 の申し送りを #2400 で実施)。
Source§fn requires_grad(&self) -> bool
fn requires_grad(&self) -> bool
パラメータを持つ子のどれかが true なら true。該当する子がいなければ true。
Source§fn forward<'t>(
&self,
_tape: TapeRef<'t>,
_input: &Var<'t>,
) -> Result<Var<'t>, AutodiffError>
fn forward<'t>( &self, _tape: TapeRef<'t>, _input: &Var<'t>, ) -> Result<Var<'t>, AutodiffError>
このステップの
tape 上で 1 回分の forward を計算する。 Read moreSource§fn set_training(&mut self, training: bool)
fn set_training(&mut self, training: bool)
学習/評価モードの切替。既定は no-op(無状態層向け)。モード依存層は
Self::training と必ず両方を override して自層に保持すること。Source§fn state_dict(&self) -> HashMap<String, Tensor<f32>>
fn state_dict(&self) -> HashMap<String, Tensor<f32>>
Self::named_parameters のキー付きビュー(各値は clone)。順序契約は持たない。Source§fn load_state_dict(
&mut self,
state: HashMap<String, Tensor<f32>>,
) -> Result<(), AutodiffError>
fn load_state_dict( &mut self, state: HashMap<String, Tensor<f32>>, ) -> Result<(), AutodiffError>
Self::state_dict の逆(strict 限定)。 Read moreSource§fn freeze(&mut self) -> Result<(), AutodiffError>
fn freeze(&mut self) -> Result<(), AutodiffError>
Self::set_requires_grad(false) の別名(転移学習で backbone を固定する典型呼び出し)。Source§fn named_modules(&self) -> Vec<(String, &dyn Module)>
fn named_modules(&self) -> Vec<(String, &dyn Module)>
子孫を深さ優先(子自身 → その子孫)・登録順で再帰列挙する
(autodiff
Module::named_modules の鏡写し。#2134・#2401)。 Read moreSource§fn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Self::named_parameters が公開する学習可能パラメータの総要素数
(autodiff Module::parameter_count の鏡写し。#2134・#2401)。
saturating_add で合計するためオーバーフローでも panic せず usize::MAX に飽和する。Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
実装型名(
std::any::type_name::<Self>())。出力形式は標準ライブラリが安定を
保証しないため表示用途に限る(autodiff Module::type_name の鏡写し。#2134・#2401)。
例外として、凍結ロールバックが子の型の照合キーに使う(Self::children_mut の (b)。
異なる型は区別できるが同じ型の別インスタンスは区別できない。偽る override は範囲外)。Auto Trait Implementations§
impl !RefUnwindSafe for ModuleDict
impl !Send for ModuleDict
impl !Sync for ModuleDict
impl !UnwindSafe for ModuleDict
impl Freeze for ModuleDict
impl Unpin for ModuleDict
impl UnsafeUnpin for ModuleDict
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