Class: DNN::Regularizers::L1

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

Instance Attribute Summary collapse

Attributes inherited from Regularizer

#param

Instance Method Summary collapse

Methods inherited from Regularizer

from_hash

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.



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

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

Instance Attribute Details

#l1_lambdaObject

Returns the value of attribute l1_lambda.



36
37
38
# File 'lib/dnn/core/regularizers.rb', line 36

def l1_lambda
  @l1_lambda
end

Instance Method Details

#backwardObject



47
48
49
50
51
# File 'lib/dnn/core/regularizers.rb', line 47

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

#forward(x) ⇒ Object



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

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

#load_hash(hash) ⇒ Object



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

def load_hash(hash)
  initialize(hash[:l1_lambda])
end

#to_hashObject



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

def to_hash
  super(l1_lambda: @l1_lambda)
end