Variant of clamp function which avoid the vanishing gradient problem. This function is equivalent to adding a regularizer of the form pushback * sum_i ( relu(min - preactivation_i) + relu(preactivation_i - max) ) to the full loss function, which pushes clamped
(
tensor: torch.Tensor,
min: float | None = None,
max: float | None = None,
pushback: float = 1e-2,
)
| 144 | |
| 145 | |
| 146 | def clamp_with_pushback( |
| 147 | tensor: torch.Tensor, |
| 148 | min: float | None = None, |
| 149 | max: float | None = None, |
| 150 | pushback: float = 1e-2, |
| 151 | ) -> torch.Tensor: |
| 152 | """Variant of clamp function which avoid the vanishing gradient problem. |
| 153 | |
| 154 | This function is equivalent to adding a regularizer of the form |
| 155 | |
| 156 | pushback * sum_i ( |
| 157 | relu(min - preactivation_i) + relu(preactivation_i - max) |
| 158 | ) |
| 159 | |
| 160 | to the full loss function, which pushes clamped values back. |
| 161 | |
| 162 | When used in minimization problems, pushback should be greater than |
| 163 | zero. In maximization problems, pushback should be smaller than zero. |
| 164 | """ |
| 165 | output = ClampWithPushback.apply(tensor, min, max, pushback) |
| 166 | assert isinstance(output, torch.Tensor) |
| 167 | return output |
| 168 | |
| 169 | |
| 170 | def hard_sigmoid_with_pushback(x: torch.Tensor, slope: float = 1.0 / 6.0) -> torch.Tensor: |
no outgoing calls
no test coverage detected