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

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.



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

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

Instance Attribute Details

#epsObject

Returns the value of attribute eps.



125
126
127
# File 'lib/dnn/core/losses.rb', line 125

def eps
  @eps
end

Class Method Details

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



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

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

Instance Method Details

#backward(y, t) ⇒ Object



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

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

#forward(y, t) ⇒ Object



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

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



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

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

#to_hashObject



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

def to_hash
  super(eps: @eps)
end