Class: DNN::Regularizers::L2

Inherits:
Regularizer show all
Defined in:
lib/dnn/core/regularizers.rb

Instance Attribute Summary collapse

Attributes inherited from Regularizer

#param

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(l2_lambda = 0.01) ⇒ L2

Returns a new instance of L2.

Parameters:

  • l2_lambda (Float) (defaults to: 0.01) —

    L2 regularizer coefficient.



58
59
60
# File 'lib/dnn/core/regularizers.rb', line 58

def initialize(l2_lambda = 0.01)
  @l2_lambda = l2_lambda
end

Instance Attribute Details

#l2_lambda ⇒ Object

Returns the value of attribute l2_lambda.



51
52
53
# File 'lib/dnn/core/regularizers.rb', line 51

def l2_lambda
  @l2_lambda
end

Class Method Details

.from_hash(hash) ⇒ Object



53
54
55
# File 'lib/dnn/core/regularizers.rb', line 53

def self.from_hash(hash)
  self.new(hash[:l2_lambda])
end

Instance Method Details

#backward ⇒ Object



66
67
68
# File 'lib/dnn/core/regularizers.rb', line 66

def backward
  @param.grad += @l2_lambda * @param.data
end

#forward(x) ⇒ Object



62
63
64
# File 'lib/dnn/core/regularizers.rb', line 62

def forward(x)
  x + 0.5 * @l2_lambda * (@param.data ** 2).sum
end

#to_hash ⇒ Object



70
71
72
# File 'lib/dnn/core/regularizers.rb', line 70

def to_hash
  super(l2_lambda: @l2_lambda)
end