Class: DNN::Losses::SigmoidCrossEntropy

Inherits:
Loss
  • Object
show all
Defined in:
lib/dnn/core/losses.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Loss

#clean, from_hash, #loss, #regularizers_backward, #regularizers_forward

Constructor Details

#initialize(eps: 1e-7) ⇒ SigmoidCrossEntropy

Returns a new instance of SigmoidCrossEntropy.

Parameters:

  • eps (Float) (defaults to: 1e-7)

    Value to avoid nan.



174
175
176
# File 'lib/dnn/core/losses.rb', line 174

def initialize(eps: 1e-7)
  @eps = eps
end

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



163
164
165
# File 'lib/dnn/core/losses.rb', line 163

def eps
  @eps
end

Class Method Details

.sigmoid(y) ⇒ Object Also known as: activation



166
167
168
# File 'lib/dnn/core/losses.rb', line 166

def sigmoid(y)
  Layers::Sigmoid.new.forward(y)
end

Instance Method Details

#backward(y, t) ⇒ Object



183
184
185
# File 'lib/dnn/core/losses.rb', line 183

def backward(y, t)
  @x - t
end

#forward(y, t) ⇒ Object



178
179
180
181
# File 'lib/dnn/core/losses.rb', line 178

def forward(y, t)
  @x = SigmoidCrossEntropy.sigmoid(y)
  -(t * Xumo::NMath.log(@x + @eps) + (1 - t) * Xumo::NMath.log(1 - @x + @eps)).mean(0).sum
end

#load_hash(hash) ⇒ Object



191
192
193
# File 'lib/dnn/core/losses.rb', line 191

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

#to_hashObject



187
188
189
# File 'lib/dnn/core/losses.rb', line 187

def to_hash
  super(eps: @eps)
end