Class: Gsplat::Optim::ExponentialLR
- Inherits:
-
Object
- Object
- Gsplat::Optim::ExponentialLR
- Defined in:
- lib/gsplat/optim/lr_scheduler.rb
Overview
Closed-form exponential learning-rate schedule.
Instance Attribute Summary collapse
-
#max_steps ⇒ Object
readonly
Returns the value of attribute max_steps.
-
#step_count ⇒ Object
readonly
Returns the value of attribute step_count.
Instance Method Summary collapse
-
#initialize(optimizer, lr_final:, max_steps:) ⇒ ExponentialLR
constructor
A new instance of ExponentialLR.
-
#step(step = nil) ⇒ Numeric, Hash{Symbol=>Numeric}
Advances or sets the schedule and updates every optimizer group.
Constructor Details
#initialize(optimizer, lr_final:, max_steps:) ⇒ ExponentialLR
Returns a new instance of ExponentialLR.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/gsplat/optim/lr_scheduler.rb', line 9 def initialize(optimizer, lr_final:, max_steps:) raise ArgumentError, "lr_final must be positive" unless lr_final.positive? raise ArgumentError, "max_steps must be positive" unless max_steps.is_a?(Integer) && max_steps.positive? @optimizer = optimizer @lr_final = lr_final @max_steps = max_steps @step_count = 0 @initial_rates = optimizer.groups.transform_values(&:lr) end |
Instance Attribute Details
#max_steps ⇒ Object (readonly)
Returns the value of attribute max_steps.
7 8 9 |
# File 'lib/gsplat/optim/lr_scheduler.rb', line 7 def max_steps @max_steps end |
#step_count ⇒ Object (readonly)
Returns the value of attribute step_count.
7 8 9 |
# File 'lib/gsplat/optim/lr_scheduler.rb', line 7 def step_count @step_count end |
Instance Method Details
#step(step = nil) ⇒ Numeric, Hash{Symbol=>Numeric}
Advances or sets the schedule and updates every optimizer group.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gsplat/optim/lr_scheduler.rb', line 24 def step(step = nil) @step_count = step || (step_count + 1) progress = [step_count.to_f / max_steps, 1.0].min @optimizer.groups.each_key do |name| initial = @initial_rates.fetch(name) rate = initial * ((@lr_final / initial)**progress) @optimizer.set_learning_rate(name, rate) end @optimizer.learning_rate end |