Class: Torch::Optim::Adam
- Defined in:
- lib/torch/optim/adam.rb
Instance Attribute Summary
Attributes inherited from Optimizer
Instance Method Summary collapse
-
#initialize(params, lr: 1e-3, betas: [0.9, 0.999], eps: 1e-8, weight_decay: 0, amsgrad: false) ⇒ Adam
constructor
A new instance of Adam.
- #step(closure = nil) ⇒ Object
Methods inherited from Optimizer
#add_param_group, #load_state_dict, #state_dict, #zero_grad
Constructor Details
#initialize(params, lr: 1e-3, betas: [0.9, 0.999], eps: 1e-8, weight_decay: 0, amsgrad: false) ⇒ Adam
Returns a new instance of Adam.
5 6 7 8 9 10 11 12 13 |
# File 'lib/torch/optim/adam.rb', line 5 def initialize(params, lr: 1e-3, betas: [0.9, 0.999], eps: 1e-8, weight_decay: 0, amsgrad: false) raise ArgumentError, "Invalid learning rate: #{lr}" if lr < 0 raise ArgumentError, "Invalid epsilon value: #{eps}" if eps < 0 raise ArgumentError, "Invalid beta parameter at index 0: #{betas[0]}" if betas[0] < 0 || betas[0] >= 1 raise ArgumentError, "Invalid beta parameter at index 1: #{betas[1]}" if betas[1] < 0 || betas[1] >= 1 defaults = {lr: lr, betas: betas, eps: eps, weight_decay: weight_decay, amsgrad: amsgrad} super(params, defaults) end |
Instance Method Details
#step(closure = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/torch/optim/adam.rb', line 15 def step(closure = nil) loss = nil if closure loss = closure.call end @param_groups.each do |group| group[:params].each do |p| next unless p.grad grad = p.grad.data if grad.sparse? raise Error, "Adam does not support sparse gradients, please consider SparseAdam instead" end amsgrad = group[:amsgrad] state = @state[p] # State initialization if state.size == 0 state[:step] = 0 # Exponential moving average of gradient values state[:exp_avg] = Torch.zeros_like(p.data) # Exponential moving average of squared gradient values state[:exp_avg_sq] = Torch.zeros_like(p.data) if amsgrad # Maintains max of all exp. moving avg. of sq. grad. values state[:max_exp_avg_sq] = Torch.zeros_like(p.data) end end exp_avg, exp_avg_sq = state[:exp_avg], state[:exp_avg_sq] if amsgrad max_exp_avg_sq = state[:max_exp_avg_sq] end beta1, beta2 = group[:betas] state[:step] += 1 bias_correction1 = 1 - beta1 ** state[:step] bias_correction2 = 1 - beta2 ** state[:step] if group[:weight_decay] != 0 grad.add!(p.data, alpha: group[:weight_decay]) end # Decay the first and second moment running average coefficient exp_avg.mul!(beta1).add!(grad, alpha: 1 - beta1) exp_avg_sq.mul!(beta2).addcmul!(grad, grad, value: 1 - beta2) if amsgrad # Maintains the maximum of all 2nd moment running avg. till now Torch.max(max_exp_avg_sq, exp_avg_sq, out: max_exp_avg_sq) # Use the max. for normalizing running avg. of gradient denom = (max_exp_avg_sq.sqrt / Math.sqrt(bias_correction2)).add!(group[:eps]) else denom = (exp_avg_sq.sqrt / Math.sqrt(bias_correction2)).add!(group[:eps]) end step_size = group[:lr] / bias_correction1 p.data.addcdiv!(exp_avg, denom, value: -step_size) end end loss end |