Class: DNN::Regularizers::L1L2

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

Instance Attribute Summary

Attributes inherited from Regularizer

#param

Instance Method Summary collapse

Methods inherited from Regularizer

from_hash

Constructor Details

#initialize(l1_lambda = 0.01, l2_lambda = 0.01) ⇒ L1L2

Returns a new instance of L1L2.

Parameters:

  • l1_lambda (Float) (defaults to: 0.01)

    L1 regularizer coefficient.

  • l2_lambda (Float) (defaults to: 0.01)

    L2 regularizer coefficient.



88
89
90
91
# File 'lib/dnn/core/regularizers.rb', line 88

def initialize(l1_lambda = 0.01, l2_lambda = 0.01)
  @l1 = Layers::Lasso.new(l1_lambda)
  @l2 = Layers::Ridge.new(l2_lambda)
end

Instance Method Details

#forward(x) ⇒ Object



93
94
95
# File 'lib/dnn/core/regularizers.rb', line 93

def forward(x)
  x + @l1.(@param) + @l2.(@param)
end

#l1_lambdaObject



97
98
99
# File 'lib/dnn/core/regularizers.rb', line 97

def l1_lambda
  @l1.l1_lambda
end

#l1_lambda=(lam) ⇒ Object



101
102
103
# File 'lib/dnn/core/regularizers.rb', line 101

def l1_lambda=(lam)
  @l1.l1_lambda = lam
end

#l2_lambdaObject



105
106
107
# File 'lib/dnn/core/regularizers.rb', line 105

def l2_lambda
  @l2.l2_lambda
end

#l2_lambda=(lam) ⇒ Object



109
110
111
# File 'lib/dnn/core/regularizers.rb', line 109

def l2_lambda=(lam)
  @l2.l2_lambda = lam
end

#load_hash(hash) ⇒ Object



117
118
119
# File 'lib/dnn/core/regularizers.rb', line 117

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

#to_hashObject



113
114
115
# File 'lib/dnn/core/regularizers.rb', line 113

def to_hash
  super(l1_lambda: l1_lambda, l2_lambda: l2_lambda)
end