Sign#
- class deeptrack.elementwise.Sign(feature: Feature | None = None, **kwargs: Any)#
Bases:
ElementwiseFeatureApply the sign function elementwise.
This class uses a backend-aware dispatch to handle NumPy and PyTorch tensors safely. It returns: - -1 for negative values, - 0 for zero, - +1 for positive values.
For complex numbers, it returns x / abs(x) when x != 0.
Parameters#
- feature: Feature | None, optional
The input feature to which the sign function will be applied. If None, the function is applied directly to the input array or tensor.
Examples#
>>> import deeptrack as dt >>> from deeptrack.elementwise import Sign
Use with NumPy directly:
>>> import numpy as np >>> result = Sign()(np.array([-5.0, 0.0, 2.0])) >>> result array([-1., 0., 1.])
Use with PyTorch directly:
>>> import torch >>> result = Sign()(torch.tensor([-5.0, 0.0, 2.0])) >>> result tensor([-1., 0., 1.])
Use in a pipeline with a NumPy value:
>>> value = dt.Value(value=np.array([-5.0, 0.0, 2.0])) >>> pipeline = value >> Sign() >>> result = pipeline() >>> result array([-1., 0., 1.])
Use in a pipeline with a PyTorch value:
>>> value = dt.Value(value=torch.tensor([-5.0, 0.0, 2.0])) >>> pipeline = value >> Sign() >>> result = pipeline() >>> result tensor([-1., 0., 1.])
These are equivalent to:
>>> pipeline = Sign(value)