burn_optim/optim/
decay.rs1use burn_core as burn;
2
3use crate::RecordState;
4use burn::config::Config;
5use burn::tensor::Device;
6use burn::tensor::Tensor;
7
8#[derive(Config, Debug)]
10pub struct WeightDecayConfig {
11 pub penalty: f32,
13}
14
15#[derive(RecordState, Clone, new)]
17pub struct WeightDecayState<const D: usize> {
18 pub(crate) grad_last_step: Tensor<D>,
19}
20
21#[derive(Clone)]
23pub struct WeightDecay {
24 penalty: f32,
25}
26
27impl WeightDecay {
28 pub fn new(config: &WeightDecayConfig) -> Self {
30 Self {
31 penalty: config.penalty,
32 }
33 }
34
35 pub fn transform<const D: usize>(&self, grad: Tensor<D>, tensor: Tensor<D>) -> Tensor<D> {
46 tensor.mul_scalar(self.penalty).add(grad)
47 }
48}
49
50impl<const D: usize> WeightDecayState<D> {
51 pub fn to_device(mut self, device: &Device) -> Self {
61 self.grad_last_step = self.grad_last_step.to_device(device);
62 self
63 }
64}