Class: DNN::Losses::SoftmaxCrossEntropy

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) ⇒ SoftmaxCrossEntropy

Returns a new instance of SoftmaxCrossEntropy.

Parameters:

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

    Value to avoid nan.



145
146
147
# File 'lib/dnn/core/losses.rb', line 145

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

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



134
135
136
# File 'lib/dnn/core/losses.rb', line 134

def eps
  @eps
end

Class Method Details

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



137
138
139
# File 'lib/dnn/core/losses.rb', line 137

def softmax(y)
  Xumo::NMath.exp(y) / Xumo::NMath.exp(y).sum(1, keepdims: true)
end

Instance Method Details

#backward_node(d) ⇒ Object



155
156
157
# File 'lib/dnn/core/losses.rb', line 155

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

#forward_node(y, t) ⇒ Object



149
150
151
152
153
# File 'lib/dnn/core/losses.rb', line 149

def forward_node(y, t)
  @t = t
  @x = SoftmaxCrossEntropy.softmax(y)
  -(t * Xumo::NMath.log(@x + @eps)).mean(0).sum
end

#load_hash(hash) ⇒ Object



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

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

#to_hashObject



159
160
161
# File 'lib/dnn/core/losses.rb', line 159

def to_hash
  super(eps: @eps)
end