Class: DNN::Regularizers::L2

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(l2_lambda = 0.01) ⇒ L2

Returns a new instance of L2.

Parameters:

  • l2_lambda (Float) (defaults to: 0.01)

    L2 regularizer coefficient.



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

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

Instance Attribute Details

#l2_lambdaObject

Returns the value of attribute l2_lambda.



64
65
66
# File 'lib/dnn/core/regularizers.rb', line 64

def l2_lambda
  @l2_lambda
end

Instance Method Details

#backwardObject



75
76
77
# File 'lib/dnn/core/regularizers.rb', line 75

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

#forward(x) ⇒ Object



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

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

#load_hash(hash) ⇒ Object



83
84
85
# File 'lib/dnn/core/regularizers.rb', line 83

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

#to_hashObject



79
80
81
# File 'lib/dnn/core/regularizers.rb', line 79

def to_hash
  super(l2_lambda: @l2_lambda)
end