Class: DNN::Losses::SoftmaxCrossEntropy

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

Returns a new instance of SoftmaxCrossEntropy.

Parameters:

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

    Value to avoid nan.



140
141
142
# File 'lib/dnn/core/losses.rb', line 140

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

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



129
130
131
# File 'lib/dnn/core/losses.rb', line 129

def eps
  @eps
end

Class Method Details

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



132
133
134
# File 'lib/dnn/core/losses.rb', line 132

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

Instance Method Details

#backward(y, t) ⇒ Object



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

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

#forward(y, t) ⇒ Object



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

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

#load_hash(hash) ⇒ Object



157
158
159
# File 'lib/dnn/core/losses.rb', line 157

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

#to_hashObject



153
154
155
# File 'lib/dnn/core/losses.rb', line 153

def to_hash
  super(eps: @eps)
end