lib/dnn/core/optimizers.rb in ruby-dnn-0.13.2 vs lib/dnn/core/optimizers.rb in ruby-dnn-0.13.3

- old
+ new

@@ -36,11 +36,11 @@ .map { |layer| layer.get_params.values }.flatten.compact .select { |param| param.grad } clip_grads(target_params) if @clip_norm update_params(target_params) target_params.each do |param| - param.grad = Xumo::SFloat.zeros(*param.data.shape) + param.grad = Xumo::SFloat[0] end end def dump { hash: to_hash, status: @status } @@ -56,10 +56,10 @@ private def update_params(params) raise NotImplementedError.new("Class '#{self.class.name}' has implement method 'update_params'") end private def clip_grads(params) - norm = Math.sqrt(params.reduce(0) { |sum, param| sum + (param.grad == 0 ? 0 : (param.grad ** 2).sum) }) + norm = Math.sqrt(params.reduce(0) { |total, param| total + (param.grad ** 2).sum }) return if norm <= @clip_norm rate = @clip_norm / (norm + 1e-7) params.each do |param| param.grad *= rate end