pub struct Param<T>where
T: Parameter,{ /* private fields */ }
Expand description
Parameters are the fundamental building blocks of modules where they serve as containers for tensors that can be updated during training, and loaded during inference. If you don’t want to save the tensors with a record and/or don’t want to update it during training, you don’t need this type to wrap your tensor.
§Laziness
The initialization of parameters can be lazy when created using uninitialized, which can be done using an initializer.
This reduces the amount of allocations done when loading a model for inference without having to create a custom initialization function only for inference.
§Example
let device = Device::default();
let config = ModuleConfig::default();
let record = Recorder::new().load("/path/to/module", &device);
// No tensor allocation
let module = config.init(device);
// Will use the tensor allocated for the record if the same device is used.
let module = module.load_record(record);
Implementations§
source§impl<T> Param<T>where
T: Parameter,
impl<T> Param<T>where
T: Parameter,
sourcepub fn initialized(id: ParamId, value: T) -> Param<T>
pub fn initialized(id: ParamId, value: T) -> Param<T>
Create a new parameter that is already initialized.
sourcepub fn uninitialized<F>(
id: ParamId,
init: F,
device: <T as Parameter>::Device,
is_require_grad: bool
) -> Param<T>
pub fn uninitialized<F>( id: ParamId, init: F, device: <T as Parameter>::Device, is_require_grad: bool ) -> Param<T>
Create a new parameter that is not already initialized.
sourcepub fn into_value(self) -> T
pub fn into_value(self) -> T
Gets the parameter’s value while consuming the parameter.
sourcepub fn consume(self) -> (ParamId, T)
pub fn consume(self) -> (ParamId, T)
Gets the parameter id and value while consuming the parameter.
sourcepub fn map<F>(self, func: F) -> Param<T>where
F: Fn(T) -> T,
pub fn map<F>(self, func: F) -> Param<T>where
F: Fn(T) -> T,
Execute the given function on the inner value.
sourcepub fn set_require_grad(self, require_grad: bool) -> Param<T>
pub fn set_require_grad(self, require_grad: bool) -> Param<T>
Override the gradient requirement for the current parameter.
source§impl<B, const D: usize> Param<Tensor<B, D>>where
B: Backend,
impl<B, const D: usize> Param<Tensor<B, D>>where
B: Backend,
sourcepub fn from_tensor(value: Tensor<B, D>) -> Param<Tensor<B, D>>
pub fn from_tensor(value: Tensor<B, D>) -> Param<Tensor<B, D>>
Create a new parameter from a float tensor.
§Warnings
We strongly recommend using Param::uninitialized if you are using this method to initialize parameters inside a module, since the tensor initialization will be lazy, making the loading of weights more performant.