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

Function create_activation_pair

src/sharp/utils/math.py:32–54  ·  view source on GitHub ↗

Create activation function and corresponding inverse function. Args: activation_type: The activation type to create. Returns: The corresponding activation functions and the corresponding inverse function.

(activation_type: ActivationType)

Source from the content-addressed store, hash-verified

30
31
32def create_activation_pair(activation_type: ActivationType) -> ActivationPair:
33 """Create activation function and corresponding inverse function.
34
35 Args:
36 activation_type: The activation type to create.
37
38 Returns:
39 The corresponding activation functions and the corresponding inverse function.
40 """
41 if activation_type == "linear":
42 return ActivationPair(lambda x: x, lambda x: x)
43 elif activation_type == "exp":
44 return ActivationPair(torch.exp, torch.log)
45 elif activation_type == "sigmoid":
46 return ActivationPair(torch.sigmoid, inverse_sigmoid)
47 elif activation_type == "softplus":
48 return ActivationPair(torch.nn.functional.softplus, inverse_softplus)
49 elif activation_type == "relu_with_pushback":
50 return ActivationPair(relu_with_pushback, lambda x: x)
51 elif activation_type == "hard_sigmoid_with_pushback":
52 return ActivationPair(hard_sigmoid_with_pushback, lambda x: 6.0 * x - 3.0)
53 else:
54 raise ValueError(f"Unsupported activation function: {activation_type}.")
55
56
57def inverse_sigmoid(tensor: torch.Tensor) -> torch.Tensor:

Callers

nothing calls this directly

Calls 1

ActivationPairClass · 0.85

Tested by

no test coverage detected