pytorch_forecasting.metrics.point.TweedieLoss#
- class pytorch_forecasting.metrics.point.TweedieLoss(reduction='mean', p: float = 1.5, **kwargs)[source]#
Tweedie loss.
Tweedie regression with log-link. It might be useful, e.g., for modeling total loss in insurance, or for any target that might be tweedie-distributed.
The loss will take the exponential of the network output before it is returned as prediction. Target normalizer should therefore have no “reverse” transformation, e.g. for the
TimeSeriesDataSetinitialization, one could use:from pytorch_forecasting import TimeSeriesDataSet, EncoderNormalizer dataset = TimeSeriesDataSet( target_normalizer=EncoderNormalizer(transformation=dict(forward=torch.log1p)) )
Note that in this example, the data is log1p-transformed before normalized but not re-transformed. The TweedieLoss applies this “exp”-re-transformation on the network output after it has been de-normalized. The result is the model prediction.
- Parameters:
p (float, optional) – tweedie variance power which is greater equal 1.0 and smaller 2.0. Close to
2shifts to Gamma distribution and close to1shifts to Poisson distribution. Defaults to 1.5.reduction (str, optional) – How to reduce the loss. Defaults to “mean”.
- __init__(reduction='mean', p: float = 1.5, **kwargs)[source]#
- Parameters:
p (float, optional) – tweedie variance power which is greater equal 1.0 and smaller 2.0. Close to
2shifts to Gamma distribution and close to1shifts to Poisson distribution. Defaults to 1.5.reduction (str, optional) – How to reduce the loss. Defaults to “mean”.
Methods
__abs__()Construct compositional metric using the absolute operator.
__add__(metric)Construct compositional metric using the addition operator.
__and__(other)Construct compositional metric using the logical and operator.
__call__(*args, **kwargs)Call self as a function.
__delattr__(name)Implement delattr(self, name).
__dir__()Default dir() implementation.
__eq__(other)Construct compositional metric using the equal operator.
__floordiv__(other)Construct compositional metric using the floor division operator.
__format__(format_spec, /)Default object formatter.
__ge__(other)Construct compositional metric using the greater than or equal operator.
__getattr__(name)__getattribute__(name, /)Return getattr(self, name).
__getitem__(idx)Construct compositional metric using the get item operator.
__getnewargs__()Needed method for construction of new metrics __new__ method.
__getstate__()Get the current state, including all metric states, for the metric.
__gt__(other)Construct compositional metric using the greater than operator.
__hash__()Return an unique hash of the metric.
__init_subclass__This method is called when a class is subclassed.
__inv__()Construct compositional metric using the not operator.
__invert__()Construct compositional metric using the not operator.
__le__(other)Construct compositional metric using the less than or equal operator.
__lt__(other)Construct compositional metric using the less than operator.
__matmul__(other)Construct compositional metric using the matrix multiplication operator.
__mod__(other)Construct compositional metric using the remainder operator.
__mul__(multiplier)Construct compositional metric using the multiplication operator.
__ne__(other)Construct compositional metric using the not equal operator.
__neg__()Construct compositional metric using absolute negative operator.
__new__(*args, **kwargs)__or__(other)Construct compositional metric using the logical or operator.
__pos__()Construct compositional metric using absolute operator.
__pow__(other)Construct compositional metric using the exponential/power operator.
__radd__(other)Construct compositional metric using the addition operator.
__rand__(other)Construct compositional metric using the logical and operator.
__reduce__()Helper for pickle.
__reduce_ex__(protocol, /)Helper for pickle.
__repr__()Return repr(self).
__rfloordiv__(other)Construct compositional metric using the floor division operator.
__rmatmul__(other)Construct compositional metric using the matrix multiplication operator.
__rmod__(other)Construct compositional metric using the remainder operator.
__rmul__(multiplier)Construct compositional metric using the multiplication operator.
__ror__(other)Construct compositional metric using the logical or operator.
__rpow__(other)Construct compositional metric using the exponential/power operator.
__rsub__(other)Construct compositional metric using the subtraction operator.
__rtruediv__(other)Construct compositional metric using the true divide operator.
__rxor__(other)Construct compositional metric using the logical xor operator.
__setattr__(name, value)Overwrite default method to prevent specific attributes from being set by user.
__setstate__(state)Set the state of the metric, based on a input state.
__sizeof__()Size of object in memory, in bytes.
__str__()Return str(self).
__sub__(other)Construct compositional metric using the subtraction operator.
__subclasshook__Abstract classes can override this to customize issubclass().
__truediv__(other)Construct compositional metric using the true divide operator.
__xor__(other)Construct compositional metric using the logical xor operator.
_apply(fn[, exclude_state])Overwrite _apply function such that we can also move metric states to the correct device.
_call_impl(*args, **kwargs)_copy_state_dict()Copy the current state values.
_filter_kwargs(**kwargs)Filter kwargs such that they match the update signature of the metric.
_forward_full_state_update(*args, **kwargs)Forward computation using two calls to update.
_forward_reduce_state_update(*args, **kwargs)Forward computation using single call to update.
_get_backward_hooks()Return the backward hooks for use in the call function.
_get_backward_pre_hooks()_get_name()_load_from_state_dict(state_dict, prefix, ...)Load metric states from state_dict.
_maybe_warn_non_full_backward_hook(inputs, ...)_move_list_states_to_cpu()Move list states to cpu to save GPU memory.
_named_members(get_members_fn[, prefix, ...])Help yield various names + members of modules.
_plot([val, ax])Plot a single or multiple values from the metric.
_reduce_states(incoming_state)Add an incoming metric state to the current state of the metric.
_register_load_state_dict_pre_hook(hook[, ...])See
register_load_state_dict_pre_hook()for details._register_state_dict_hook(hook)Register a post-hook for the
state_dict()method._replicate_for_data_parallel()_save_to_state_dict(destination, prefix, ...)Save module state to the destination dictionary.
_slow_forward(*input, **kwargs)_sync_dist(dist_sync_fn, process_group)_update_losses_and_lengths(losses, lengths)_wrap_compute(compute)_wrap_update(update)_wrapped_call_impl(*args, **kwargs)add_module(name, module)Add a child module to the current module.
add_state(name, default[, dist_reduce_fx, ...])Add metric state variable.
apply(fn)Apply
fnrecursively to every submodule (as returned by.children()) as well as self.bfloat16()Casts all floating point parameters and buffers to
bfloat16datatype.buffers([recurse])Return an iterator over module buffers.
children()Return an iterator over immediate children modules.
clone()Make a copy of the metric.
compile(*args, **kwargs)Compile this Module's forward using
torch.compile().compute()Abstract method that calculates metric
cpu()Move all model parameters and buffers to the CPU.
cuda([device])Move all model parameters and buffers to the GPU.
double()Override default and prevent dtype casting.
eval()Set the module in evaluation mode.
extra_repr()Return the extra representation of the module.
float()Override default and prevent dtype casting.
forward(*args, **kwargs)Aggregate and evaluate batch input directly.
get_buffer(target)Return the buffer given by
targetif it exists, otherwise throw an error.get_extra_state()Return any extra state to include in the module's state_dict.
get_parameter(target)Return the parameter given by
targetif it exists, otherwise throw an error.get_submodule(target)Return the submodule given by
targetif it exists, otherwise throw an error.half()Override default and prevent dtype casting.
ipu([device])Move all model parameters and buffers to the IPU.
load_state_dict(state_dict[, strict, assign])Copy parameters and buffers from
state_dictinto this module and its descendants.loss(y_pred, y_true)Calculate loss without reduction.
mask_losses(losses, lengths[, reduction])Mask losses.
merge_state(incoming_state)Merge incoming metric state to the current state of the metric.
modules([remove_duplicate])Return an iterator over all modules in the network.
mtia([device])Move all model parameters and buffers to the MTIA.
named_buffers([prefix, recurse, ...])Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children()Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules([memo, prefix, remove_duplicate])Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters([prefix, recurse, ...])Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters([recurse])Return an iterator over module parameters.
persistent([mode])Change post-init if metric states should be saved to its state_dict.
plot(*_, **__)Override this method plot the metric value.
reduce_loss(losses, lengths[, reduction])Reduce loss.
register_backward_hook(hook)Register a backward hook on the module.
register_buffer(name, tensor[, persistent])Add a buffer to the module.
register_forward_hook(hook, *[, prepend, ...])Register a forward hook on the module.
register_forward_pre_hook(hook, *[, ...])Register a forward pre-hook on the module.
register_full_backward_hook(hook[, prepend])Register a backward hook on the module.
register_full_backward_pre_hook(hook[, prepend])Register a backward pre-hook on the module.
register_load_state_dict_post_hook(hook)Register a post-hook to be run after module's
load_state_dict()is called.register_load_state_dict_pre_hook(hook)Register a pre-hook to be run before module's
load_state_dict()is called.register_module(name, module)Alias for
add_module().register_parameter(name, param)Add a parameter to the module.
register_state_dict_post_hook(hook)Register a post-hook for the
state_dict()method.register_state_dict_pre_hook(hook)Register a pre-hook for the
state_dict()method.requires_grad_([requires_grad])Change if autograd should record operations on parameters in this module.
rescale_parameters(parameters, target_scale, ...)Rescale normalized parameters into the scale required for the output.
reset()Reset metric state variables to their default value.
set_dtype(dst_type)Transfer all metric state to specific dtype.
set_extra_state(state)Set extra state contained in the loaded state_dict.
set_submodule(target, module[, strict])Set the submodule given by
targetif it exists, otherwise throw an error.share_memory()See
torch.Tensor.share_memory_().state_dict([destination, prefix, keep_vars])Get the current state of metric as an dictionary.
sync([dist_sync_fn, process_group, ...])Sync function for manually controlling when metrics states should be synced across processes.
sync_context([dist_sync_fn, process_group, ...])Context manager to synchronize states.
to(*args, **kwargs)Move and/or cast the parameters and buffers.
to_empty(*, device[, recurse])Move the parameters and buffers to the specified device without copying storage.
to_prediction(out)Convert network prediction into a point prediction.
to_quantiles(y_pred[, quantiles])Convert network prediction into a quantile prediction.
train([mode])Set the module in training mode.
type(dst_type)Override default and prevent dtype casting.
unsync([should_unsync])Unsync function for manually controlling when metrics states should be reverted back to their local states.
update(y_pred, target)Update method of metric that handles masking of values.
xpu([device])Move all model parameters and buffers to the XPU.
zero_grad([set_to_none])Reset gradients of all model parameters.
Attributes
T_destination__abstractmethods____annotations____dict____doc____iter____jit_ignored_attributes____jit_unused_properties____module____slots____weakref__list of weak references to the object
_abc_impl_compiled_call_impl_update_called_versionThis allows better BC support for
load_state_dict().call_super_initdeviceReturn the device of the metric.
dtypeReturn the default dtype of the metric.
dump_patchesfull_state_updatehigher_is_betteris_differentiablemetric_stateGet the current state of the metric.
plot_legend_nameplot_lower_boundplot_upper_boundupdate_calledReturns True if update or forward has been called initialization or last reset.
update_countGet the number of times update and/or forward has been called since initialization or last reset.
training_parameters_buffers_non_persistent_buffers_set_backward_pre_hooks_backward_hooks_is_full_backward_hook_forward_hooks_forward_hooks_with_kwargs_forward_hooks_always_called_forward_pre_hooks_forward_pre_hooks_with_kwargs_state_dict_hooks_load_state_dict_pre_hooks_state_dict_pre_hooks_load_state_dict_post_hooks_modules