Class: DNN::Losses::SigmoidCrossEntropy

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DNN::Layers::LayerNode

#forward

Methods inherited from Loss

call, #call, #clean, #forward, from_hash, #loss, #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.



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

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

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



171
172
173
# File 'lib/dnn/core/losses.rb', line 171

def eps
  @eps
end

Class Method Details

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



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

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

Instance Method Details

#backward_node(d) ⇒ Object



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

def backward_node(d)
  d * (@x - @t) / @x.shape[0]
end

#forward_node(y, t) ⇒ Object



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

def forward_node(y, t)
  @t = 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



200
201
202
# File 'lib/dnn/core/losses.rb', line 200

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

#to_hashObject



196
197
198
# File 'lib/dnn/core/losses.rb', line 196

def to_hash
  super(eps: @eps)
end