Class: Torch::Optim::AdamW

Inherits:
Optimizer show all
Defined in:
lib/torch/optim/adamw.rb

Instance Attribute Summary

Attributes inherited from Optimizer

#param_groups

Instance Method Summary collapse

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: 1e-2, amsgrad: false) ⇒ AdamW

Returns a new instance of AdamW.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
# File 'lib/torch/optim/adamw.rb', line 5

def initialize(params, lr: 1e-3, betas: [0.9, 0.999], eps: 1e-8, weight_decay: 1e-2, 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
79
# File 'lib/torch/optim/adamw.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

      # Perform stepweight decay
      p.data.mul!(1 - group[:lr] * group[:weight_decay])

      # Perform optimization step
      grad = p.grad.data
      if grad.sparse?
        raise Error, "AdamW 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]

      # Decay the first and second moment running average coefficient
      exp_avg.mul!(beta1).add!(1 - beta1, grad)
      exp_avg_sq.mul!(beta2).addcmul!(1 - beta2, grad, grad)
      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!(-step_size, exp_avg, denom)
    end
  end

  loss
end