Class: Gsplat::Training::Losses::L1

Inherits:
Autograd::Function show all
Defined in:
lib/gsplat/training/losses.rb

Overview

Mean absolute error with a zero subgradient at exact equality.

Class Method Summary collapse

Methods inherited from Autograd::Function

apply

Class Method Details

.backward(context, grad_output) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Propagates the L1 subgradient to prediction and target.



24
25
26
27
28
29
30
31
# File 'lib/gsplat/training/losses.rb', line 24

def backward(context, grad_output)
  difference, count = context.saved_values
  signs = difference.class.zeros(*difference.shape)
  signs[difference.gt(0)] = 1
  signs[difference.lt(0)] = -1
  gradient = signs * (grad_output.to_f / count)
  [gradient, -gradient]
end

.forward(context, prediction, target) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluates mean absolute error.



12
13
14
15
16
17
18
19
20
# File 'lib/gsplat/training/losses.rb', line 12

def forward(context, prediction, target)
  unless prediction.shape == target.shape
    raise ShapeError, "L1 shapes differ: #{prediction.shape.inspect} and #{target.shape.inspect}"
  end

  difference = prediction - target
  context.save(difference, prediction.size)
  prediction.class.cast(difference.abs.mean)
end