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.

Parameters:

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

    L1 regularizer coefficient.



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

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)
  self.new(hash[:l1_lambda])
end

Instance Method Details

#backward ⇒ Object



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

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

#forward(x) ⇒ Object



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

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

#to_hash ⇒ Object



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

def to_hash
  super(l1_lambda: @l1_lambda)
end