Class: DNN::Optimizers::SGD
- Defined in:
- lib/dnn/core/optimizers.rb
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(layer) ⇒ Object
Constructor Details
#initialize(learning_rate = 0.01, momentum: 0) ⇒ SGD
Returns a new instance of SGD.
28 29 30 31 32 |
# File 'lib/dnn/core/optimizers.rb', line 28 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.
22 23 24 |
# File 'lib/dnn/core/optimizers.rb', line 22 def momentum @momentum end |
Class Method Details
.load_hash(hash) ⇒ Object
24 25 26 |
# File 'lib/dnn/core/optimizers.rb', line 24 def self.load_hash(hash) self.new(hash[:learning_rate], momentum: hash[:momentum]) end |
Instance Method Details
#to_hash ⇒ Object
47 48 49 |
# File 'lib/dnn/core/optimizers.rb', line 47 def to_hash super({momentum: @momentum}) end |
#update(layer) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dnn/core/optimizers.rb', line 34 def update(layer) @v[layer] ||= {} layer.params.each_key do |key| amount = layer.grads[key] * @learning_rate if @momentum > 0 @v[layer][key] ||= 0 amount += @momentum * @v[layer][key] @v[layer][key] = amount end layer.params[key] -= amount end end |