pub trait ModuleExt: Module {
Show 20 methods
// Provided methods
fn and_then<F>(&self, input: &Tensor, f: F) -> Result<Tensor>
where F: FnOnce(Tensor) -> Result<Tensor> { ... }
fn map<F>(&self, input: &Tensor, f: F) -> Result<Tensor>
where F: FnOnce(Tensor) -> Tensor { ... }
fn with_input<F>(&self, input: &Tensor, f: F) -> Result<Tensor>
where F: FnOnce(&Tensor) -> Result<Tensor> { ... }
fn summary(&self) -> String { ... }
fn print_summary(&self) { ... }
fn parameter_stats(&self) -> ParameterStats { ... }
fn has_finite_parameters(&self) -> bool { ... }
fn parameter_names(&self) -> Vec<String> { ... }
fn get_parameter(&self, name: &str) -> Option<Parameter> { ... }
fn freeze_matching(&mut self, pattern: &str) -> usize { ... }
fn unfreeze_matching(&mut self, pattern: &str) -> usize { ... }
fn frozen_parameters(&self) -> Vec<String> { ... }
fn trainable_parameters(&self) -> Vec<String> { ... }
fn clone_state_dict(&self) -> HashMap<String, Tensor> { ... }
fn apply_to_parameters<F>(&self, f: F)
where F: FnMut(&str, &Parameter) { ... }
fn parameters_by_type(&self) -> HashMap<String, usize> { ... }
fn validate(&self) -> Result<ValidationReport> { ... }
fn device(&self) -> Option<DeviceType> { ... }
fn is_cpu(&self) -> bool { ... }
fn is_cuda(&self) -> bool { ... }
}Expand description
Extension trait providing additional ergonomic methods for Module
This trait is automatically implemented for all types that implement Module, providing additional convenience methods without requiring changes to existing code.
§Design Philosophy
This extension follows Rust’s extension trait pattern to:
- Add functionality without breaking backward compatibility
- Keep the core Module trait focused on essential methods
- Provide advanced features for users who need them
- Enable fluent/builder-style APIs
Provided Methods§
Sourcefn with_input<F>(&self, input: &Tensor, f: F) -> Result<Tensor>
fn with_input<F>(&self, input: &Tensor, f: F) -> Result<Tensor>
Sourcefn print_summary(&self)
fn print_summary(&self)
Print module summary to stdout
Sourcefn parameter_stats(&self) -> ParameterStats
fn parameter_stats(&self) -> ParameterStats
Sourcefn has_finite_parameters(&self) -> bool
fn has_finite_parameters(&self) -> bool
Sourcefn parameter_names(&self) -> Vec<String>
fn parameter_names(&self) -> Vec<String>
Sourcefn get_parameter(&self, name: &str) -> Option<Parameter>
fn get_parameter(&self, name: &str) -> Option<Parameter>
Sourcefn freeze_matching(&mut self, pattern: &str) -> usize
fn freeze_matching(&mut self, pattern: &str) -> usize
Freeze parameters whose name contains pattern (sets requires_grad = false)
Pass an empty pattern ("") to freeze every parameter, since every name
contains the empty string.
§Arguments
pattern- Substring to match against parameter names
§Returns
usize- Number of parameters frozen
§Note
A parameter’s requires_grad flag is shared across clones (see
crate::Parameter), so updating the by-value clones handed out by
Module::all_named_parameters is observable on the module’s own
parameters and on any subsequently fetched copies.
Sourcefn unfreeze_matching(&mut self, pattern: &str) -> usize
fn unfreeze_matching(&mut self, pattern: &str) -> usize
Unfreeze parameters whose name contains pattern (sets requires_grad = true)
Pass an empty pattern ("") to unfreeze every parameter, since every name
contains the empty string.
§Arguments
pattern- Substring to match against parameter names
§Returns
usize- Number of parameters unfrozen
§Note
See ModuleExt::freeze_matching for why mutating the by-value parameter
clones is observable on the owning module.
Sourcefn frozen_parameters(&self) -> Vec<String>
fn frozen_parameters(&self) -> Vec<String>
Sourcefn trainable_parameters(&self) -> Vec<String>
fn trainable_parameters(&self) -> Vec<String>
Sourcefn clone_state_dict(&self) -> HashMap<String, Tensor>
fn clone_state_dict(&self) -> HashMap<String, Tensor>
Clone module parameters into a new state dict
§Returns
HashMap<String, Tensor>- Cloned state dictionary
Sourcefn apply_to_parameters<F>(&self, f: F)
fn apply_to_parameters<F>(&self, f: F)
Sourcefn parameters_by_type(&self) -> HashMap<String, usize>
fn parameters_by_type(&self) -> HashMap<String, usize>
Sourcefn validate(&self) -> Result<ValidationReport>
fn validate(&self) -> Result<ValidationReport>
Validate module configuration
Performs comprehensive validation of module state.
§Returns
Result<ValidationReport>- Validation results
Sourcefn device(&self) -> Option<DeviceType>
fn device(&self) -> Option<DeviceType>
Get the device the module’s parameters reside on
§Returns
Option<DeviceType>- The device of the module’s parameters, orNoneonly when the module genuinely has no parameters (recursively).
§Note
The device is read from the actual underlying parameter tensors. If a module’s parameters span multiple devices, the device of the first parameter encountered is reported.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".