Skip to content

Let a loss function take the batch inputs - #676

Open
georgeguimaraes wants to merge 1 commit into
elixir-nx:mainfrom
georgeguimaraes:loss-with-inputs
Open

georgeguimaraes wants to merge 1 commit into
elixir-nx:mainfrom
georgeguimaraes:loss-with-inputs

Conversation

@georgeguimaraes

Copy link
Copy Markdown
Member

A loss can now be an arity-3 function loss(y_true, y_pred, x) that also receives the batch inputs. The train and eval step states keep the batch as :x, so the "loss" metric and custom metrics can read it too, and validate/4 passes it along. Arity-2 losses and the built-in ones are unchanged.

We needed this in soothsayer to weight recent rows more in the loss, NeuralProphet's newer samples weight. The weight is per sample, so it belongs with the batch, but the loss only saw targets and predictions and we had to keep a training loop of our own outside Axon.Loop to get it in. With this the weight rides under its own input key and the loss picks it up.

A loss can now be an arity-3 function loss(y_true, y_pred, x) that also
receives the batch inputs, for losses that need something from the batch
besides the targets, such as a per-sample weight carried under its own
input key. The train and eval step states keep the batch as :x so the
loss metric, and any custom metric, can read it as well. Arity-2 losses
and the built-in ones are unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant