Coder Social home page Coder Social logo

Comments (3)

asukaminato0721 avatar asukaminato0721 commented on June 12, 2024
import math


def cosine_annealing_lr(lr, step_count, T_max, eta_min=0.0):
    lr = eta_min + (lr - eta_min) * (1 + math.cos(math.pi * step_count / T_max)) / (
        1 + math.cos(math.pi * (step_count - 1) / T_max)
    )
    return lr


lr = 0.1
step_count = 10.0
t_max = 100.0
eta_min = 0.0
assert math.isclose(
    0.0995287864543317, cosine_annealing_lr(lr, step_count, t_max, eta_min)
)
use std::f64::consts::PI;

fn cosine_annealing_lr(lr: f64, step_count: f64, t_max: f64, eta_min: f64) -> f64 {
    let cosine_arg = PI * step_count / t_max;
    let lr = eta_min + (lr - eta_min) * (1.0 + f64::cos(cosine_arg)) / (1.0 + f64::cos(PI * (step_count - 1.0) / t_max));
    lr
}

fn main() {
    let lr = 0.1;
    let step_count = 10.0;
    let t_max = 100.0;
    let eta_min = 0.0;

    let new_lr = cosine_annealing_lr(lr, step_count, t_max, eta_min);
    assert_eq!(0.09952878645433175, new_lr);
}

ref https://chat.openai.com/share/1adcedee-7c58-4583-88dc-f504b80fce6b

from fsrs-rs.

L-M-Sherlock avatar L-M-Sherlock commented on June 12, 2024

The next step is to implement the interface (or trait?):

https://github.com/burn-rs/burn/blob/d18d1b0bb9cf641e83f356296e7614845dda64f9/burn-core/src/lr_scheduler/base.rs#L4-L17

https://github.com/burn-rs/burn/blob/d18d1b0bb9cf641e83f356296e7614845dda64f9/burn-core/src/lr_scheduler/noam.rs

from fsrs-rs.

asukaminato0721 avatar asukaminato0721 commented on June 12, 2024

I am not so sure about the correctness of implement of this.

use burn::{lr_scheduler::LRScheduler, LearningRate};
#[derive(Clone, Debug)]
pub struct CosineAnnealingLR {
    t_max: f64,
    eta_min: f64,
    init_lr: LearningRate,
    step_count: f64,
}

impl LRScheduler for CosineAnnealingLR {
    type Record = usize;

    fn step(&mut self) -> LearningRate {
        self.step_count += 1.0;
        use std::f64::consts::PI;
        fn cosine_annealing_lr(lr: f64, step_count: f64, t_max: f64, eta_min: f64) -> f64 {
            let cosine_arg = PI * step_count / t_max;
            let lr = eta_min
                + (lr - eta_min) * (1.0 + f64::cos(cosine_arg))
                    / (1.0 + f64::cos(PI * (step_count - 1.0) / t_max));
            lr
        }
        self.init_lr = cosine_annealing_lr(self.init_lr, self.step_count, self.t_max, self.eta_min);
        self.init_lr
    }

    fn to_record(&self) -> Self::Record {
        self.step_count as usize
    }

    fn load_record(mut self, record: Self::Record) -> Self {
        self.step_count = record as f64;
        self
    }
}

from fsrs-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.