auto_diff.op package¶
Submodules¶
auto_diff.op.op_constant module¶
-
class
auto_diff.op.op_constant.OpConstant(x: Union[int, float, list, numpy.ndarray], **kwargs)[source]¶ Bases:
auto_diff.op.operation.OperationContains a constant.
-
__init__(x: Union[int, float, list, numpy.ndarray], **kwargs)[source]¶ Parameters: - x – The constant value.
- kwargs –
-
auto_diff.op.op_flatten module¶
-
class
auto_diff.op.op_flatten.OpFlatten(x: auto_diff.op.operation.Operation, **kwargs)[source]¶ Bases:
auto_diff.op.op_reshape.OpReshapeFlatten the tensor to 1-D array.
auto_diff.op.op_placeholder module¶
-
class
auto_diff.op.op_placeholder.OpPlaceholder(shape: Sequence[int], **kwargs)[source]¶ Bases:
auto_diff.op.operation.OperationThe placeholder that represents values to be feed.
auto_diff.op.op_reshape module¶
-
class
auto_diff.op.op_reshape.OpReshape(x: auto_diff.op.operation.Operation, shape: Sequence[int], **kwargs)[source]¶ Bases:
auto_diff.op.operation.OperationReshape the tensor to a given shape.
-
__init__(x: auto_diff.op.operation.Operation, shape: Sequence[int], **kwargs)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
auto_diff.op.op_transpose module¶
-
class
auto_diff.op.op_transpose.OpTranspose(x: auto_diff.op.operation.Operation, axes: Optional[Sequence[int]] = None, **kwargs)[source]¶ Bases:
auto_diff.op.operation.OperationTranspose the tensor.
Basic operation without axes:
\[Y = X^T\]Partial derivative of a single element:
\[\begin{split}\begin{array}{rcl} \displaystyle \frac{\partial L}{\partial x_{ij}} &=& \displaystyle \sum_{i,j} \frac{\partial L}{\partial y_{ij}} \cdot \frac{\partial y_{ij}}{\partial x_{ij}} \\ &=& \displaystyle \frac{\partial L}{\partial y_{ji}} \cdot \frac{\partial y_{ji}}{\partial x_{ij}} \\ &=& \displaystyle \frac{\partial L}{\partial y_{ji}} \cdot \frac{\partial x_{ij}}{\partial x_{ij}} \\ &=& \displaystyle \frac{\partial L}{\partial y_{ji}} \\ \end{array}\end{split}\]Matrix derivative:
\[\frac{\partial L}{\partial X} = \left ( \frac{\partial L}{\partial Y} \right )^T\]Generally, axes should be a permutation of the dimensions, suppose there is a function \(f\) that maps from \((0, 1, \dots, k)\) to the new permutation, then this transpose operation would be:
\[y_{i_1, i_2, \dots, i_k} = x_{f(i_1), f(i_2), \dots, f(i_k)}\]The partial derivative of \(x_{i_1, i_2, \dots, i_k}\) is 1 only with \(y_{f(i_1)^{-1}, f(i_2)^{-1}, \dots, f(i_k)^{-1}}\). Therefore the derivative should be another transpose operation with inverse mapping function \(f^{-1}\).
-
__init__(x: auto_diff.op.operation.Operation, axes: Optional[Sequence[int]] = None, **kwargs)[source]¶ Parameters: - x – Input operation.
- axes – A permutation of dimensions. The dimensions will be reversed if it is None.
- kwargs – Arguments for parent.
-
auto_diff.op.op_variable module¶
-
class
auto_diff.op.op_variable.OpVariable(initializer: Union[Callable, int, float, list, numpy.ndarray], shape: tuple = None, **kwargs)[source]¶ Bases:
auto_diff.op.operation.OperationContains weights that could be updated.
-
__init__(initializer: Union[Callable, int, float, list, numpy.ndarray], shape: tuple = None, **kwargs)[source]¶ Parameters: - initializer – A function that accepts shape as its arguments or a numpy array.
- shape – Must be provided if the initializer is a function.
- kwargs –
-
auto_diff.op.operation module¶
-
class
auto_diff.op.operation.Operation(**kwargs)[source]¶ Bases:
objectAbstract operation for building computing graph.
-
__op_counter= None¶ The counter for giving each operation a unique index.
-
__op_collection= None¶ Collection of existing operations.
-
STEP_KEY= '__step__'¶ The key for extracting step information from session.
-
dim¶
-
forward(feed_dict: Mapping[Union[str, Operation], numpy.ndarray] = None) → numpy.ndarray[source]¶ Do the calculations to get the output of the operations.
Parameters: feed_dict – Contains the real values of placeholders, see OpPlaceholder.Returns: A numpy array.
-
_forward(feed_dict: Mapping[Union[str, Operation], numpy.ndarray]) → numpy.ndarray[source]¶ Forward operation to be implemented.
-
backward(gradient: Optional[auto_diff.op.operation.Operation] = None) → None[source]¶ Update gradients recursively.
Parameters: gradient – Current gradient.
-
_backward(gradient: auto_diff.op.operation.Operation) → None[source]¶ Backward operation to be implemented.
-
transpose(axes: Optional[Sequence[int]] = None, **kwargs) → auto_diff.op.operation.Operation[source]¶ See
OpTranspose.
-
expand_dims(axis: Optional[int] = None, **kwargs) → auto_diff.op.operation.Operation[source]¶ See
OpExpandDims.
-
sum(axis: Union[int, Sequence[int], None] = None, keepdims: bool = False, **kwargs) → auto_diff.op.operation.Operation[source]¶ See
OpSum.
-
dot(x: auto_diff.op.operation.Operation, **kwargs) → auto_diff.op.operation.Operation[source]¶ See
OpDot.
-
_Operation__op_collection= {}¶
-
_Operation__op_counter= [0]¶
-