lib/dnn/core/layers.rb in ruby-dnn-0.5.5 vs lib/dnn/core/layers.rb in ruby-dnn-0.5.6
- old
+ new
@@ -140,10 +140,18 @@
def shape
[@num_nodes]
end
+ def ridge
+ if @weight_decay > 0
+ 0.5 * @weight_decay * (@params[:weight]**2).sum
+ else
+ 0
+ end
+ end
+
def to_hash
super({num_nodes: @num_nodes,
weight_initializer: @weight_initializer.to_hash,
bias_initializer: @bias_initializer.to_hash,
weight_decay: @weight_decay})
@@ -207,11 +215,11 @@
class OutputLayer < Layer
private
def ridge
- 0.5 * @model.layers.select { |layer| layer.respond_to?(:weight_decay) }
- .reduce(0) { |sum, layer| layer.weight_decay * (layer.params[:weight]**2).sum }
+ @model.layers.select { |layer| layer.respond_to?(:ridge) }
+ .reduce(0) { |sum, layer| sum + layer.ridge }
end
end
class Dropout < Layer