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

#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.



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

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

Instance Attribute Details

#eps ⇒ Object

Returns the value of attribute eps.



116
117
118
# File 'lib/dnn/core/losses.rb', line 116

def eps
  @eps
end

Class Method Details

.from_hash(hash) ⇒ Object



118
119
120
# File 'lib/dnn/core/losses.rb', line 118

def self.from_hash(hash)
  self.new(eps: hash[:eps])
end

.softmax(y) ⇒ Object



122
123
124
# File 'lib/dnn/core/losses.rb', line 122

def self.softmax(y)
  Xumo::NMath.exp(y) / Xumo::NMath.exp(y).sum(1).reshape(y.shape[0], 1)
end

Instance Method Details

#backward(y, t) ⇒ Object



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

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

#forward(y, t) ⇒ Object



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

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

#to_hash ⇒ Object



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

def to_hash
  super(eps: @eps)
end