LeakyClamp

class pfhedge.nn.LeakyClamp(clamped_slope=0.01, inverted_output='mean')[source]

Leakily clamp all elements in input into the range \([\min, \max]\).

The bounds \(\min\) and \(\max\) can be tensors.

If \(\min \leq \max\):

\[\begin{split}\text{output} = \begin{cases} \min + \text{clampled_slope} * (\text{input} - \min) & \text{input} < \min \\ \text{input} & \min \leq \text{input} \leq \max \\ \max + \text{clampled_slope} * (\text{input} - \max) & \max < \text{input} \end{cases}\end{split}\]

If \(\min > \max\):

\[\begin{split}\text{output} = \begin{cases} \frac12 (\min + \max) & \text{inverted_output} = \text{'mean'} \\ \max & \text{inverted_output} = \text{'max'} \\ \end{cases}\end{split}\]
Parameters
  • clamped_slope (float, default=0.01) – Controls the slope in the clampled regions.

  • inverted_output ({'mean', ''max'}, default='mean') – Controls the output when \(\min > \max\). ‘max’ is consistent with torch.clamp().

Shape:
  • input: \((N, *)\) where \(*\) means any number of additional dimensions.

  • min: \((N, *)\) or any size broadcastable to input.

  • max: \((N, *)\) or any size broadcastable to input.

  • output: \((N, *)\), same shape as the input.

Examples

>>> import torch
>>> from pfhedge.nn import LeakyClamp
>>> m = LeakyClamp()
>>> input = torch.linspace(-2, 12, 15) * 0.1
>>> input
tensor([-0.2000, -0.1000,  0.0000,  0.1000,  0.2000,  0.3000,  0.4000,  0.5000,
         0.6000,  0.7000,  0.8000,  0.9000,  1.0000,  1.1000,  1.2000])
>>> m(input, 0.0, 1.0)
tensor([-2.0000e-03, -1.0000e-03,  0.0000e+00,  1.0000e-01,  2.0000e-01,
         3.0000e-01,  4.0000e-01,  5.0000e-01,  6.0000e-01,  7.0000e-01,
         8.0000e-01,  9.0000e-01,  1.0000e+00,  1.0010e+00,  1.0020e+00])
forward(input, min=None, max=None)[source]

Clamp all elements in input into the range \([\min, \max]\).

Parameters
  • input (torch.Tensor) – The input tensor.

  • min (torch.Tensor, optional) – Lower-bound of the range to be clamped to.

  • max (torch.Tensor, optional) – Upper-bound of the range to be clamped to.

Returns

torch.Tensor