Class: DNN::Regularizers::L1

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(l1_lambda = 0.01) ⇒ L1

Returns a new instance of L1.



29
30
31
# File 'lib/dnn/core/regularizers.rb', line 29

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

Instance Attribute Details

#l1_lambda ⇒ Object

Returns the value of attribute l1_lambda.



23
24
25
# File 'lib/dnn/core/regularizers.rb', line 23

def l1_lambda
  @l1_lambda
end

Class Method Details

.from_hash(hash) ⇒ Object



25
26
27
# File 'lib/dnn/core/regularizers.rb', line 25

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

Instance Method Details

#backward ⇒ Object



37
38
39
40
41
# File 'lib/dnn/core/regularizers.rb', line 37

def backward
  dparam = Xumo::SFloat.ones(*@param.data.shape)
  dparam[@param.data < 0] = -1
  @param.grad += @l1_lambda * dparam
end

#forward(x) ⇒ Object



33
34
35
# File 'lib/dnn/core/regularizers.rb', line 33

def forward(x)
  x + @l1_lambda * @param.data.abs.sum
end

#to_hash ⇒ Object



43
44
45
# File 'lib/dnn/core/regularizers.rb', line 43

def to_hash
  super(l1_lambda: @l1_lambda)
end