Module: Qoa::LossFunctions

Included in:
NeuralNetwork
Defined in:
lib/qoa/loss_functions.rb

Class Method Summary collapse

Class Method Details

.binary_cross_entropy(prediction, target) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/qoa/loss_functions.rb', line 14

def binary_cross_entropy(prediction, target)
  raise ArgumentError, 'prediction and target must have the same length' if prediction.size != target.size
  -prediction.zip(target).map { |p, t| t * Math.log(p) + (1 - t) * Math.log(1 - p) }.sum / prediction.size
end

.categorical_cross_entropy(prediction, target) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/qoa/loss_functions.rb', line 19

def categorical_cross_entropy(prediction, target)
  raise ArgumentError, 'prediction and target must have the same length' if prediction.size != target.size
  -prediction.zip(target).map { |p, t| t * Math.log(p) }.sum / prediction.size
end

.cross_entropy_loss(prediction, target) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/qoa/loss_functions.rb', line 9

def cross_entropy_loss(prediction, target)
  raise ArgumentError, 'prediction and target must have the same length' if prediction.size != target.size
  -prediction.zip(target).map { |p, t| t * Math.log(p) }.sum / prediction.size
end

.mean_absolute_error(prediction, target) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/qoa/loss_functions.rb', line 24

def mean_absolute_error(prediction, target)
  raise ArgumentError, 'prediction and target must have the same length' if prediction.size != target.size
  prediction.zip(target).map { |p, t| (p - t).abs }.sum / prediction.size
end

.mean_squared_error(prediction, target) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
# File 'lib/qoa/loss_functions.rb', line 4

def mean_squared_error(prediction, target)
  raise ArgumentError, 'prediction and target must have the same length' if prediction.size != target.size
  prediction.zip(target).map { |p, t| (p - t) ** 2 }.sum / prediction.size
end