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.



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

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

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



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

def eps
  @eps
end

Class Method Details

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



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

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

Instance Method Details

#backward(y, t) ⇒ Object



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

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

#forward(y, t) ⇒ Object



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

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

#load_hash(hash) ⇒ Object



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

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

#to_hashObject



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

def to_hash
  super(eps: @eps)
end