Skip to main content

ModuleDict

Struct ModuleDict 

Source
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

Source

pub fn new() -> Self

空の ModuleDict を作る。

Source

pub fn from_pairs( pairs: Vec<(String, Box<dyn Module>)>, ) -> Result<Self, AutodiffError>

(key, module) 列から構築する(挿入順を保持)。不正キーがあれば Err(panic しない)。

Source

pub fn insert( &mut self, key: impl Into<String>, module: Box<dyn Module>, ) -> Result<Option<Box<dyn Module>>, AutodiffError>

key の子を挿入する。同名キーがあれば置換して旧値を返す(位置は保つ)。 不正キー(空・'.' 含み)は Err。

Source

pub fn remove(&mut self, key: &str) -> Option<Box<dyn Module>>

key の子を削除して返す(無ければ None)。

Source

pub fn get(&self, key: &str) -> Option<&dyn Module>

key の子への参照(無ければ None)。

Source

pub fn get_mut(&mut self, key: &str) -> Option<&mut (dyn Module + 'static)>

Self::get の可変版。

Source

pub fn contains_key(&self, key: &str) -> bool

key を保持しているか。

Source

pub fn keys(&self) -> impl Iterator<Item = &str>

キー列の走査(挿入順)。

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &dyn Module)>

(key, &dyn Module) 列の走査(挿入順)。

Source

pub fn iter_mut( &mut self, ) -> impl Iterator<Item = (&str, &mut (dyn Module + 'static))>

Self::iter の可変版。

Source

pub fn len(&self) -> usize

保持している子の数。

Source

pub fn is_empty(&self) -> bool

子を 1 つも保持していないか。

Trait Implementations§

Source§

impl Default for ModuleDict

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Module for ModuleDict

Source§

fn named_parameters(&self) -> Vec<(String, &Tensor<f32>)>

"{key}.{name}" を挿入順で連結する。

Source§

fn set_parameter( &mut self, name: &str, value: Tensor<f32>, ) -> Result<(), AutodiffError>

先頭の '.' までをキーとして子へ委譲する。区切りなし・未知キーは InvalidArgument。

Source§

fn children(&self) -> Vec<(String, &dyn Module)>

名前はキー、順序は挿入順。

Source§

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

パラメータを持つ子のどれかが true なら true。該当する子がいなければ true。

Source§

fn children_mut(&mut self) -> Vec<(String, &mut dyn Module)>

children の可変版(キー・挿入順は同一。#2400)。

Source§

fn forward<'t>( &self, _tape: TapeRef<'t>, _input: &Var<'t>, ) -> Result<Var<'t>, AutodiffError>

このステップの tape 上で 1 回分の forward を計算する。 Read more
Source§

fn set_training(&mut self, training: bool)

学習/評価モードの切替。既定は no-op(無状態層向け)。モード依存層は Self::training と必ず両方を override して自層に保持すること。
Source§

fn training(&self) -> bool

現在のモード。既定は true(PyTorch Module.training の初期値と同じ)。
Source§

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>

Self::state_dict の逆(strict 限定)。 Read more
Source§

fn freeze(&mut self) -> Result<(), AutodiffError>

Self::set_requires_grad(false) の別名(転移学習で backbone を固定する典型呼び出し)。
Source§

fn named_modules(&self) -> Vec<(String, &dyn Module)>

子孫を深さ優先(子自身 → その子孫)・登録順で再帰列挙する (autodiff Module::named_modules の鏡写し。#2134・#2401)。 Read more
Source§

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

実装型名(std::any::type_name::<Self>())。出力形式は標準ライブラリが安定を 保証しないため表示用途に限る(autodiff Module::type_name の鏡写し。#2134・#2401)。 例外として、凍結ロールバックが子の型の照合キーに使う(Self::children_mut の (b)。 異なる型は区別できるが同じ型の別インスタンスは区別できない。偽る override は範囲外)。

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