Class: DNN::Activations::SoftmaxWithLoss

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

Constant Summary collapse

NMath =
Xumo::NMath

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



230
231
232
# File 'lib/dnn/core/activations.rb', line 230

def backward(y)
  @out - y
end

#forward(x) ⇒ Object



226
227
228
# File 'lib/dnn/core/activations.rb', line 226

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

#loss(y) ⇒ Object



234
235
236
237
# File 'lib/dnn/core/activations.rb', line 234

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