MCPcopy Create free account
hub / github.com/apple/ml-sharp / clamp_with_pushback

Function clamp_with_pushback

src/sharp/utils/math.py:146–167  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

144
145
146def 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
170def hard_sigmoid_with_pushback(x: torch.Tensor, slope: float = 1.0 / 6.0) -> torch.Tensor:

Callers 2

relu_with_pushbackFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected