Class: NN::Softmax
Instance Method Summary collapse
- #backward(y) ⇒ Object
- #forward(x) ⇒ Object
-
#initialize(nn) ⇒ Softmax
constructor
A new instance of Softmax.
- #loss(y) ⇒ Object
Constructor Details
#initialize(nn) ⇒ Softmax
Returns a new instance of Softmax.
362 363 364 |
# File 'lib/nn.rb', line 362 def initialize(nn) @nn = nn end |
Instance Method Details
#backward(y) ⇒ Object
370 371 372 |
# File 'lib/nn.rb', line 370 def backward(y) (@out - y) / @nn.batch_size end |
#forward(x) ⇒ Object
366 367 368 |
# File 'lib/nn.rb', line 366 def forward(x) @out = NMath.exp(x) / NMath.exp(x).sum(1).reshape(x.shape[0], 1) end |
#loss(y) ⇒ Object
374 375 376 377 |
# File 'lib/nn.rb', line 374 def loss(y) ridge = 0.5 * @nn.weight_decay * @nn.weights.reduce(0){|sum, weight| sum + (weight ** 2).sum} -(y * NMath.log(@out + 1e-7)).sum / @nn.batch_size + ridge end |