Class: DNN::Optimizers::SGD
- Defined in:
- lib/dnn/core/optimizers.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#momentum ⇒ Object
Returns the value of attribute momentum.
Attributes inherited from Optimizer
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(learning_rate = 0.01, momentum: 0) ⇒ SGD
constructor
A new instance of SGD.
- #to_hash ⇒ Object
- #update(params) ⇒ Object
Constructor Details
#initialize(learning_rate = 0.01, momentum: 0) ⇒ SGD
Returns a new instance of SGD.
33 34 35 36 37 |
# File 'lib/dnn/core/optimizers.rb', line 33 def initialize(learning_rate = 0.01, momentum: 0) super(learning_rate) @momentum = momentum @v = {} end |
Instance Attribute Details
#momentum ⇒ Object
Returns the value of attribute momentum.
27 28 29 |
# File 'lib/dnn/core/optimizers.rb', line 27 def momentum @momentum end |
Class Method Details
.load_hash(hash) ⇒ Object
29 30 31 |
# File 'lib/dnn/core/optimizers.rb', line 29 def self.load_hash(hash) self.new(hash[:learning_rate], momentum: hash[:momentum]) end |
Instance Method Details
#to_hash ⇒ Object
51 52 53 |
# File 'lib/dnn/core/optimizers.rb', line 51 def to_hash super({momentum: @momentum}) end |
#update(params) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dnn/core/optimizers.rb', line 39 def update(params) params.select { |key, param| param.grad }.each_value do |param| amount = param.grad * @learning_rate if @momentum > 0 @v[param] ||= 0 amount += @momentum * @v[param] @v[param] = amount end param.data -= amount end end |