Class: DNN::Activations::SoftmaxWithLoss

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

Instance Method Summary collapse

Instance Method Details

#backward(y) ⇒ Object



99
100
101
# File 'lib/dnn/core/activations.rb', line 99

def backward(y)
  @out - y
end

#forward(x) ⇒ Object



95
96
97
# File 'lib/dnn/core/activations.rb', line 95

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

#loss(y) ⇒ Object



103
104
105
106
# File 'lib/dnn/core/activations.rb', line 103

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