Class: GRX::Loss::BCELoss

Inherits:
Object
  • Object
show all
Defined in:
lib/grx/loss.rb

Overview

================================================================

BCELoss — Binary Cross-Entropy L = -mean(t*log(p) + (1-t)*log(1-p)) pred must be in (0,1) — apply Sigmoid before if using logits.

Constant Summary collapse

EPS =
1e-7

Instance Method Summary collapse

Instance Method Details

#call(pred, target) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
# File 'lib/grx/loss.rb', line 35

def call(pred, target)
  raise ShapeError, "Incompatible shapes: #{pred.shape} vs #{target.shape}" if pred.shape != target.shape
  p_clamped = pred.clip(EPS, 1.0 - EPS)
  ones = Tensor.ones_like(target)
  term1 = target * p_clamped.log
  term2 = (ones - target) * (ones - p_clamped).log
  (-(term1 + term2)).mean
end