lib/dnn/core/optimizers.rb in ruby-dnn-0.4.1 vs lib/dnn/core/optimizers.rb in ruby-dnn-0.4.2
- old
+ new
@@ -1,61 +1,53 @@
module DNN
module Optimizers
- #Super class of all optimizer classes.
+ # Super class of all optimizer classes.
class Optimizer
attr_accessor :learning_rate
def initialize(learning_rate)
@learning_rate = learning_rate
end
- #Update layer has params.
+ # Update layer has params.
def update(layer) end
- def to_hash
- {name: self.class.name, learning_rate: @learning_rate}
+ def to_hash(hash)
+ {name: self.class.name, learning_rate: @learning_rate}.merge(hash)
end
end
class SGD < Optimizer
attr_accessor :momentum
+ def self.load_hash(hash)
+ self.new(hash[:learning_rate], momentum: hash[:momentum])
+ end
+
def initialize(learning_rate = 0.01, momentum: 0)
super(learning_rate)
@momentum = momentum
- @amounts = {}
+ @v = {}
end
-
- def self.load_hash(hash)
- self.new(hash[:learning_rate], hash[:momentum])
- end
def update(layer)
- amount = if @amounts[layer]
- @amounts[layer]
- else
- @amounts[layer] = {}
- end
+ @v[layer] ||= {}
layer.params.each_key do |key|
- amount[key] = layer.grads[key] * @learning_rate
+ amount = layer.grads[key] * @learning_rate
if @momentum > 0
- @amounts[layer][key] ||= 0
- amount[key] += @momentum * @amounts[layer][key]
- @amounts[layer] = amount
+ @v[layer][key] ||= 0
+ amount += @momentum * @v[layer][key]
+ @v[layer][key] = amount
end
- layer.params[key] -= amount[key]
+ layer.params[key] -= amount
end
end
def to_hash
- {
- name: self.class.name,
- learning_rate: @learning_rate,
- momentum: @momentum,
- }
+ super({momentum: @momentum})
end
end
class AdaGrad
@@ -100,15 +92,11 @@
layer.params[key] -= (@learning_rate / NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
end
end
def to_hash
- {
- name: self.class.name,
- learning_rate: @learning_rate,
- muse: @muse,
- }
+ super({muse: @muse})
end
end
class Adam < Optimizer
@@ -143,15 +131,10 @@
layer.params[key] -= lr * @m[layer][key] / NMath.sqrt(@v[layer][key] + 1e-7)
end
end
def to_hash
- {
- name: self.class.name,
- learning_rate: @learning_rate,
- beta1: @beta1,
- beta2: @beta2,
- }
+ super({beta1: @beta1, beta2: @beta2})
end
end
end
end