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
permalink #initialize(nn) ⇒ Softmax
Returns a new instance of Softmax.
355 356 357 |
# File 'lib/nn.rb', line 355 def initialize(nn) @nn = nn end |
Instance Method Details
permalink #backward(y) ⇒ Object
[View source]
363 364 365 |
# File 'lib/nn.rb', line 363 def backward(y) @out - y end |
permalink #forward(x) ⇒ Object
[View source]
359 360 361 |
# File 'lib/nn.rb', line 359 def forward(x) @out = NMath.exp(x) / NMath.exp(x).sum(1).reshape(x.shape[0], 1) end |
permalink #loss(y) ⇒ Object
[View source]
367 368 369 370 |
# File 'lib/nn.rb', line 367 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 |