Skip to content

Operator

toydl.core.operator.add

add(x: float, y: float) -> float

\(f(x, y) = x + y\)

Source code in toydl/core/operator.py
18
19
20
def add(x: float, y: float) -> float:
    """$f(x, y) = x + y$"""
    return x + y

toydl.core.operator.id_

id_(x: float) -> float

\(f(x) = x\)

Source code in toydl/core/operator.py
13
14
15
def id_(x: float) -> float:
    """$f(x) = x$"""
    return x

toydl.core.operator.lt

lt(x: float, y: float) -> float

\(f(x, y) = x < y\)

Source code in toydl/core/operator.py
32
33
34
def lt(x: float, y: float) -> float:
    """$f(x, y) = x < y$"""
    return float(x < y)

toydl.core.operator.mul

mul(x: float, y: float) -> float

\(f(x, y) = x * y\)

Source code in toydl/core/operator.py
4
5
6
def mul(x: float, y: float) -> float:
    """$f(x, y) = x * y$"""
    return x * y

toydl.core.operator.neg

neg(x: float) -> float

\(f(x) = -x\)

Source code in toydl/core/operator.py
23
24
25
def neg(x: float) -> float:
    """$f(x) = -x$"""
    return -x

toydl.core.operator.relu

relu(x: float) -> float

f(x) = x if x is greater than 0, else 0

Source code in toydl/core/operator.py
72
73
74
75
76
def relu(x: float) -> float:
    """
    f(x) = x if x is greater than 0, else 0
    """
    return x if x > 0 else 0

toydl.core.operator.sigmoid

sigmoid(x: float) -> float
\[f(x) = \frac{1.0}{(1.0 + e^{-x})}\]

Calculate as

\[ f(x) = \begin{cases} \frac{1.0}{1.0 + e^{-x}} & \text{if } x \geq 0 \\ \frac{e^x}{1.0 + e^x} & \text{otherwise} \end{cases} \]

for stability. The key is to make sure the x in exp(x) is always negative to avoid exp(x) overflow.

Source code in toydl/core/operator.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def sigmoid(x: float) -> float:
    r"""
    $$f(x) =  \frac{1.0}{(1.0 + e^{-x})}$$

    Calculate as

    $$
    f(x) = \begin{cases}
    \frac{1.0}{1.0 + e^{-x}} & \text{if } x \geq 0 \\
    \frac{e^x}{1.0 + e^x} & \text{otherwise}
    \end{cases}
    $$

    for stability.
    The key is to make sure the `x` in exp(x) is always negative to avoid exp(x) overflow.
    """
    return 1 / (1 + math.exp(-x)) if x >= 0 else math.exp(x) / (1 + math.exp(x))