Tinkex.Types.RegularizerSpec (Tinkex v0.3.4)
View SourceSpecification for a single regularizer in the composition pipeline.
Fields
:fn- The regularizer function. Must accept(data, logprobs)and return{loss_tensor, metrics_map}. For async regularizers, should return aTask.t()that resolves to the same tuple.:weight- Non-negative float multiplier for the regularizer loss. The contribution to total loss isweight * regularizer_loss.:name- String identifier for telemetry and metrics. Must be unique within a regularizer list.:async- Boolean flag indicating whetherfnreturns a Task (default: false). When true, the executor willTask.await/2the result.
Examples
# Synchronous regularizer
%RegularizerSpec{
fn: fn _data, logprobs ->
{Nx.sum(Nx.abs(logprobs)), %{"l1" => 1.0}}
end,
weight: 0.01,
name: "l1_sparsity"
}
# Async regularizer (I/O-bound)
%RegularizerSpec{
fn: fn data, _logprobs ->
Task.async(fn ->
result = external_api_call(data)
{Nx.tensor(result.penalty), %{"validated" => true}}
end)
end,
weight: 0.1,
name: "external_validation",
async: true
}
Summary
Types
@type async_regularizer_fn() :: ([Tinkex.Types.Datum.t()], Nx.Tensor.t() -> Task.t())
@type regularizer_fn() :: ([Tinkex.Types.Datum.t()], Nx.Tensor.t() -> regularizer_result())
@type regularizer_result() :: {Nx.Tensor.t(), %{required(String.t()) => number()}}
@type t() :: %Tinkex.Types.RegularizerSpec{ async: boolean(), fn: regularizer_fn() | async_regularizer_fn(), name: String.t(), weight: number() }
Functions
Create a new RegularizerSpec with validation.
Examples
RegularizerSpec.new(%{
fn: &my_regularizer/2,
weight: 0.01,
name: "l1"
})
RegularizerSpec.new(fn: &my_reg/2, weight: 0.5, name: "entropy")
@spec validate!(map()) :: :ok
Validate regularizer spec attributes.
Raises ArgumentError if any validation fails.
Validations
:fnmust be a function of arity 2:weightmust be a non-negative number:namemust be a non-empty string:asyncmust be a boolean (if provided)