Class: Torch::Optim::SGD

Inherits:
Optimizer show all
Defined in:
lib/torch/optim/sgd.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:, momentum: 0, dampening: 0, weight_decay: 0, nesterov: false) ⇒ SGD

Returns a new instance of SGD.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/torch/optim/sgd.rb', line 5

def initialize(params, lr:, momentum: 0, dampening: 0, weight_decay: 0, nesterov: false)
  raise ArgumentError, "Invalid learning rate: #{lr}" if lr < 0.0
  raise ArgumentError, "Invalid momentum value: #{momentum}" if momentum < 0.0
  raise ArgumentError, "Invalid weight_decay value: #{weight_decay}" if weight_decay < 0.0

  defaults = {lr: lr, momentum: momentum, dampening: dampening, weight_decay: weight_decay, nesterov: nesterov}

  if nesterov && (momentum <= 0 || dampening != 0)
    raise ArgumentError, "Nesterov momentum requires a momentum and zero dampening"
  end

  super(params, defaults)
end

Instance Method Details

#step(closure = nil) ⇒ Object



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
# File 'lib/torch/optim/sgd.rb', line 19

def step(closure = nil)
  loss = nil
  if closure
    loss = closure.call
  end

  @param_groups.each do |group|
    weight_decay = group[:weight_decay]
    momentum = group[:momentum]
    dampening = group[:dampening]
    nesterov = group[:nesterov]

    group[:params].each do |p|
      next unless p.grad
      d_p = p.grad.data
      if weight_decay != 0
        d_p.add!(p.data, alpha: weight_decay)
      end
      if momentum != 0
        param_state = @state[p]
        if !param_state.key?(:momentum_buffer)
          buf = param_state[:momentum_buffer] = Torch.clone(d_p).detach
        else
          buf = param_state[:momentum_buffer]
          buf.mul!(momentum).add!(d_p, alpha: 1 - dampening)
        end
        if nesterov
          d_p = d_p.add(buf, alpha: momentum)
        else
          d_p = buf
        end
      end

      p.data.add!(d_p, alpha: -group[:lr])
    end
  end

  loss
end