Scalar¶
toydl.core.scalar.context.Context
dataclass
¶
Context class is used by ScalarFunction
to store information during the forward pass.
save_for_backward ¶
save_for_backward(*values: Any) -> None
Store the given values
if they need to be used during backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values |
Any
|
the values that should be saved for backward |
()
|
Source code in toydl/core/scalar/context.py
14 15 16 17 18 19 20 21 |
|
toydl.core.scalar.scalar.Add ¶
toydl.core.scalar.scalar.EQ ¶
toydl.core.scalar.scalar.Exp ¶
toydl.core.scalar.scalar.Inv ¶
toydl.core.scalar.scalar.LT ¶
toydl.core.scalar.scalar.Log ¶
toydl.core.scalar.scalar.Mul ¶
toydl.core.scalar.scalar.Neg ¶
toydl.core.scalar.scalar.ReLU ¶
toydl.core.scalar.scalar.Scalar ¶
Scalar(
v: float,
history: ScalarHistory = ScalarHistory(),
name: Optional[str] = None,
)
A reimplementation of scalar values for auto-differentiation
tracking. Scalar Variables behave as close as possible to standard
Python numbers while also tracking the operations that led to the
number's creation. They can only be manipulated by
ScalarFunction
.
Source code in toydl/core/scalar/scalar.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
accumulate_derivative ¶
accumulate_derivative(x: Any) -> None
Add x
to the derivative accumulated on this variable.
Should only be called during auto-differentiation on leaf variables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Any
|
value to be accumulated |
required |
Source code in toydl/core/scalar/scalar.py
111 112 113 114 115 116 117 118 119 120 121 |
|
backward ¶
backward(d_output: Optional[float] = None) -> None
Calls autodiff to fill in the derivatives for the history of this object.
Args: d_output (number, opt): starting derivative to backpropagate through the model (typically left out, and assumed to be 1.0).
Source code in toydl/core/scalar/scalar.py
153 154 155 156 157 158 159 160 161 162 163 |
|
is_leaf ¶
is_leaf() -> bool
True if this variable created by the user (no last_fn
)
Source code in toydl/core/scalar/scalar.py
123 124 125 |
|
requires_grad_ ¶
requires_grad_(flag: bool = True)
Set the requires_grad flag to flag
on variable.
Ensures that operations on this variable will trigger backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
flag |
bool
|
whether to require grad |
True
|
Source code in toydl/core/scalar/scalar.py
99 100 101 102 103 104 105 106 107 108 109 |
|
toydl.core.scalar.scalar.ScalarFunction ¶
A wrapper for a mathematical function that processes and produces Scalar variables.
This is a static class and is never instantiated. We use class
here to group together the forward
and backward
code.