Class: NN::Softmax

Inherits:
Object
  • Object
show all
Includes:
Numo
Defined in:
lib/nn.rb

Instance Method Summary collapse

Constructor Details

#initialize(nn) ⇒ Softmax

Returns a new instance of Softmax.



360
361
362
# File 'lib/nn.rb', line 360

def initialize(nn)
  @nn = nn
end

Instance Method Details

#backward(y) ⇒ Object



368
369
370
# File 'lib/nn.rb', line 368

def backward(y)
  @out - y
end

#forward(x) ⇒ Object



364
365
366
# File 'lib/nn.rb', line 364

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

#loss(y) ⇒ Object



372
373
374
375
# File 'lib/nn.rb', line 372

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