Class: DNN::Activations::SoftmaxWithLoss

Inherits:
Layers::OutputLayer show all
Defined in:
lib/dnn/core/activations.rb

Instance Method Summary collapse

Methods inherited from Layers::Layer

#build, #built?, #initialize, #prev_layer, #shape, #to_hash

Constructor Details

This class inherits a constructor from DNN::Layers::Layer

Instance Method Details

#backward(y) ⇒ Object



168
169
170
# File 'lib/dnn/core/activations.rb', line 168

def backward(y)
  @out - y
end

#forward(x) ⇒ Object



164
165
166
# File 'lib/dnn/core/activations.rb', line 164

def forward(x)
  @out = Xumo::NMath.exp(x) / Xumo::NMath.exp(x).sum(1).reshape(x.shape[0], 1)
end

#loss(y) ⇒ Object



172
173
174
175
# File 'lib/dnn/core/activations.rb', line 172

def loss(y)
  batch_size = y.shape[0]
  -(y * Xumo::NMath.log(@out + 1e-7)).sum / batch_size + ridge
end