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.
24 25 26 27 28 |
# File 'lib/dnn/core/optimizers.rb', line 24 def initialize(learning_rate = 0.01, momentum: 0) super(learning_rate) @momentum = momentum @amounts = {} 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
30 31 32 |
# File 'lib/dnn/core/optimizers.rb', line 30 def self.load_hash(hash) self.new(hash[:learning_rate], hash[:momentum]) end |
Instance Method Details
#to_hash ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/dnn/core/optimizers.rb', line 51 def to_hash { name: self.class.name, learning_rate: @learning_rate, momentum: @momentum, } end |
#update(layer) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dnn/core/optimizers.rb', line 34 def update(layer) amount = if @amounts[layer] @amounts[layer] else @amounts[layer] = {} end layer.params.each_key do |key| amount[key] = layer.grads[key] * @learning_rate if @momentum > 0 @amounts[layer][key] ||= 0 amount[key] += @momentum * @amounts[layer][key] @amounts[layer] = amount end layer.params[key] -= amount[key] end end |