leaspy.utils.weighted_tensor¶
Attributes¶
Classes¶
A torch.tensor, with optional (non-negative) weights (0 <-> masked). |
Functions¶
|
Factory/decorator to create a weighted-tensor compatible function from the provided unary-tensor function. |
|
Expand shape of tensor at left with provided shape. |
|
Expand shape of tensor at right with provided shape. |
|
Sum dimension(s) of provided tensor (regular or weighted - |
|
Adds ndim dimensions to tensor, from right-side, without |
|
Sum dimension(s) of provided weighted tensor. |
|
Sum dimension(s) of provided weighted tensor like wsum_dim but |
|
Sum dimension(s) of provided weighted tensor like wsum_dim but |
Package Contents¶
- factory_weighted_tensor_unary_operator(f, *, fill_value=None)[source]¶
Factory/decorator to create a weighted-tensor compatible function from the provided unary-tensor function.
- expand_left(t, *, shape)[source]¶
Expand shape of tensor at left with provided shape.
- Parameters:
t (leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S])
shape (Tuple[int, Ellipsis])
- Return type:
leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S]
- expand_right(t, *, shape)[source]¶
Expand shape of tensor at right with provided shape.
- Parameters:
t (leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S])
shape (Tuple[int, Ellipsis])
- Return type:
leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S]
- sum_dim(x, *, fill_value=0, dim=None, but_dim=None, **kws)[source]¶
Sum dimension(s) of provided tensor (regular or weighted - filling with fill_value aggregates without any summed weighting if any).
- unsqueeze_right(t, *, ndim)[source]¶
Adds ndim dimensions to tensor, from right-side, without copy (useful for right broadcasting which is non-standard).
- Parameters:
t (leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S])
ndim (int)
- Return type:
leaspy.utils.weighted_tensor._weighted_tensor.TensorOrWeightedTensor[S]
- wsum_dim(x, *, fill_value=0, dim=None, but_dim=None, **kws)[source]¶
Sum dimension(s) of provided weighted tensor.
The weighted tensor is filled with fill_value if provided. The function returns the sum of weights as well.
- Parameters:
- xWeightedTensor
The weighted tensor on which to compute the sum.
- fill_valuefloat
The value to use for filling the weighted tensor.
- dimint or tuple of int, optional
The dimension(s) on which to sum.
- but_dimint or tuple of int, optional
The dimension(s) to omit when summing.
- Returns:
- weighted_sumtorch.Tensor
Weighted sum, with totally un-weighted aggregates filled with fill_value.
- sum_weightstorch.Tensor
The sum of weights.
- Parameters:
- Return type:
- wsum_dim_return_sum_of_weights_only(x, *, fill_value=0, dim=None, but_dim=None, **kws)[source]¶
Sum dimension(s) of provided weighted tensor like wsum_dim but only return the sum of weights and not the weighted sum.
- Parameters:
- xWeightedTensor
The weighted tensor on which to compute the sum.
- fill_valuefloat
The value to use for filling the weighted tensor.
- dimint or tuple of int, optional
The dimension(s) on which to sum.
- but_dimint or tuple of int, optional
The dimension(s) to omit when summing.
- Returns:
- torch.Tensor
The sum of weights.
- Parameters:
- Return type:
- wsum_dim_return_weighted_sum_only(x, *, fill_value=0, dim=None, but_dim=None, **kws)[source]¶
Sum dimension(s) of provided weighted tensor like wsum_dim but only return the weighted sum and not the sum of weights.
- Parameters:
- xWeightedTensor
The weighted tensor on which to compute the sum.
- fill_valuefloat
The value to use for filling the weighted tensor.
- dimint or tuple of int, optional
The dimension(s) on which to sum.
- but_dimint or tuple of int, optional
The dimension(s) to omit when summing.
- Returns:
- torch.Tensor
Weighted sum, with totally un-weighted aggregates filled with fill_value.
- Parameters:
- Return type:
- TensorOrWeightedTensor¶
- class WeightedTensor[source]¶
Bases:
Generic[VT]A torch.tensor, with optional (non-negative) weights (0 <-> masked).
- Parameters:
- value
torch.Tensor Raw values, without any mask.
- weight
torch.Tensor, optional Relative weights for values. Default: None
- value
- Attributes:
- value
torch.Tensor Raw values, without any mask.
- weight
torch.Tensor Relative weights for values. If weight is a tensor[bool], it can be seen as a mask (valid value <-> weight is True). More generally, meaningless values <-> indices where weights equal 0. Default: None
- value
- property weighted_value: Tensor¶
Get the weighted value tensor. This is the value tensor multiplied by the weight tensor.
- Returns:
torch.Tensor:The weighted value tensor. If weight is None, the value tensor is returned.
- Return type:
- filled(fill_value=None)[source]¶
Return the values tensor with masked zeros filled with the specified value.
Return the values tensor filled with fill_value where the weight is exactly zero.
- Parameters:
- fill_value
VT, optional The value to fill the tensor with for aggregates where weights were all zero. Default: None
- fill_value
- Returns:
torch.Tensor:The filled tensor. If weight or fill_value is None, the original tensor is returned.
- Parameters:
fill_value (Optional[VT])
- Return type:
- valued(value)[source]¶
Return a new WeightedTensor with same weight as self but with new value provided.
- Parameters:
- value
torch.Tensor The new value to be set.
- value
- Returns:
WeightedTensor:A new WeightedTensor with the same weight as self but with the new value provided.
- Parameters:
value (Tensor)
- Return type:
- map(func, *args, fill_value=None, **kws)[source]¶
Apply a function to the values tensor while preserving weights.
The function is applied only to the values tensor after optionally filling zero-weight positions. The weights remain unchanged in the returned tensor.
- Parameters:
- funcCallable[[
torch.Tensor],torch.Tensor] The function to be applied to the values.
- *args
Positional arguments to be passed to the function.
- fill_value
VT, optional The value to fill the tensor with for aggregates where weights were all zero. Default: None
- **kws
Keyword arguments to be passed to the function.
- funcCallable[[
- Returns:
WeightedTensor:A new WeightedTensor with the result of the operation and the same weights.
- Parameters:
- Return type:
- map_both(func, *args, fill_value=None, **kws)[source]¶
Apply a function to both values and weights tensors.
The same function is applied to both components of the weighted tensor. Zero-weight positions in the values tensor are filled before applying the function.
- Parameters:
- funcCallable[[ :obj`torch.Tensor` ],
torch.Tensor] The function to be applied to both values and weights.
- *args
Positional arguments to be passed to the function.
- fill_value
VT, optional The value to fill the tensor with for aggregates where weights were all zero. Default: None
- **kws
Keyword arguments to be passed to the function.
- funcCallable[[ :obj`torch.Tensor` ],
- Returns:
WeightedTensor:A new WeightedTensor with the result of the operation and the appropriate weights.
- Parameters:
- Return type:
- index_put(indices, values, *, accumulate=False)[source]¶
Out-of-place
torch.index_put()on values (no modification of weights).- Parameters:
- indices
tuple[torch.Tensor, …] The indices to put the values at.
- values
torch.Tensor The values to put at the specified indices.
- accumulate
bool, optional Whether to accumulate the values at the specified indices. Default: False
- indices
- Returns:
WeightedTensor[VT]A new
WeightedTensorwith the updated values and the same weights.
- Parameters:
- Return type:
WeightedTensor[VT]
- wsum(*, fill_value=0, **kws)[source]¶
Get the weighted sum of tensor together with sum of weights.
<!> The result is NOT a WeightedTensor any more since weights are already taken into account. <!> We always fill values with 0 prior to weighting to prevent 0 * nan = nan that would propagate nans in sums.
- Parameters:
- fill_value
VT, optional The value to fill the sum with for aggregates where weights were all zero. Default: 0
- **kws
Optional keyword-arguments for torch.sum (such as dim=… or keepdim=…)
- fill_value
- Returns:
tuple[torch.Tensor,torch.Tensor]:- Tuple containing:
- weighted_sum
torch.Tensor Weighted sum, with totally un-weighted aggregates filled with fill_value.
- weighted_sum
- sum_weights
torch.Tensor(may be of other type than cls.weight_dtype) The sum of weights (useful if some average are needed).
- sum_weights
- Parameters:
fill_value (VT)
- Return type:
- sum(*, fill_value=0, **kws)[source]¶
Compute weighted sum of values.
For unweighted tensors, this is equivalent to regular
torch.sum(). For weighted tensors, returns the same as the first element of wsum().- Parameters:
- fill_value
VT, optional The value to fill the sum with for aggregates where weights were all zero. Default: 0
- **kws
Optional keyword-arguments
- fill_value
- Returns:
torch.Tensor:The weighted sum, with totally un-weighted aggregates filled with fill_value.
- Parameters:
fill_value (VT)
- Return type:
- view(*shape)[source]¶
Return a view of the weighted tensor with a different shape.
- Parameters:
- Returns:
WeightedTensor[VT]:A new
WeightedTensorwith the same weights but with the new shape provided.
- Return type:
WeightedTensor[VT]
- expand(*shape)[source]¶
Expand the weighted tensor to a new shape.
- Parameters:
- Returns:
WeightedTensor[VT]:A new
WeightedTensorwith the same weights but with the new shape provided.
- Return type:
WeightedTensor[VT]
- to(*, device)[source]¶
Move the weighted tensor to a different device.
- Parameters:
- device
torch.device The device to be set.
- device
- Returns:
- Parameters:
device (device)
- Return type:
WeightedTensor[VT]
- cpu()[source]¶
Move the weighted tensor to CPU memory.
Applies the torch.Tensor.cpu() operation to both the value tensor and weight tensor (if present), returning a new weighted tensor with all components on the CPU.
- Returns:
WeightedTensor[VT]:A new
WeightedTensorwith the same weights but with the new device provided.
- Return type:
WeightedTensor[VT]
- property shape: Size¶
Shape of the values tensor.
- Returns:
torch.Size:The shape of the values tensor.
- Return type:
- property dtype: dtype¶
Type of values.
- Returns:
torch.dtype:The type of values.
- Return type:
- property device: device¶
Device of values.
- Returns:
torch.device:The device of values.
- Return type:
- abs()[source]¶
Compute the absolute value of the weighted tensor.
- Returns:
WeightedTensorA new WeightedTensor with the absolute value of the values tensor.
- Return type:
- static get_filled_value_and_weight(t, *, fill_value=None)[source]¶
Method to get tuple (value, weight) for both regular and weighted tensors.
- Parameters:
- tclass:TensorOrWeightedTensor
The tensor to be converted.
- fill_value
VT, optional The value to fill the tensor with for aggregates where weights were all zero. Default: None
- Returns:
Tuple`[:obj:`torch.Tensor, Optional[torch.Tensor]]:Tuple containing: - value :
torch.TensorThe filled tensor. If weight is None, the original tensor is returned.
- weight
torch.Tensor, optional The weight tensor.
- weight
- Parameters:
t (TensorOrWeightedTensor[VT])
fill_value (Optional[VT])
- Return type: