autodiff package¶
Submodules¶
autodiff.operations module¶
Collection of tools to make new elementary operations
-
autodiff.operations.
acos
(x)¶ Arccos
Parameters: x (Number) – Value Returns: acos(x) Return type: Number
-
autodiff.operations.
acos_deriv
(x)¶ Derivative of arccos
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.
add
(x, y)¶ add two numbers together, one of x and y has to be a Number object
Parameters: - x – a Number object or an int/float
- y – a Number object or an int/float to be added
Returns: value of the sum
-
autodiff.operations.
add_deriv
(x, y)¶ Derivative of additions, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be added
Returns: The derivative of the sum of x and y
-
autodiff.operations.
asin
(x)¶ Arcsin
Parameters: x (Number) – Value Returns: asin(x) Return type: Number
-
autodiff.operations.
asin_deriv
(x)¶ Arcsin derivative
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.
atan
(x)¶ Arctan
Parameters: x (Number) – Value Returns: atan(x) Return type: Number
-
autodiff.operations.
atan_deriv
(x)¶ Derivative of atan
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.
cos
(x)¶ Take the cos(x)
Parameters: x (Number) – Number to take the cos of Returns: cos(x.val) Return type: float
-
autodiff.operations.
cos_deriv
(x)¶ Derivative of cos(x)
Parameters: x (structures.Number()) – Number to take the cos of. Must have a deriv
attributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.
cosh
(x)¶ Hyperbolic cosine
Parameters: x (Number) – Value Returns: cosh(x) Return type: Number
-
autodiff.operations.
cosh_deriv
(x)¶ Hyperbolic cosine
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.
div
(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.
div_deriv
(x, y)¶ Derivative of division, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be divided
Returns: The derivative of the quotient of x and y
-
autodiff.operations.
elementary
(deriv_func)¶ Decorator to create an elementary operation
This takes as an argument a function that calculates the derivative of the function the user is calculating. When the decorated function is called, @elementary also calls deriv_func and stores both the value and derivative in a new Number() object
Example
>>> import numpy as np >>> def sin_deriv(x): ... return {x: np.cos(x.val) * x.deriv[x]} >>> @elementary(sin_deriv) ... def sin(x): ... return np.sin(x.val) >>> a = Number(np.pi / 2) >>> sina = sin(a) >>> sina.val 1.0 >>> sina.deriv[a] 0.0
Parameters: deriv_func (function) – Function specifying the derivative of this function. Must return a dictionary where each key-value pair is the partial derivative of the decorated function Returns: Decorated function Return type: function
-
autodiff.operations.
exp
(x)¶ Take the exp(x)
Parameters: x (Number) – Number to take the exp of Returns: exp(x.val) Return type: float
-
autodiff.operations.
exp_deriv
(x)¶ Derivative of exp(x)
Parameters: x (structures.Number()) – Number to take the exp of. Must have a deriv
attributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.
log
(x, y=2.718281828459045)¶ Take the log(x) at base y
Parameters: - x (Number) – Number to take the log of
- y (a Number object or an int/float) – Base of the logarithm.
Returns: log(x.val)
Return type: float
-
autodiff.operations.
log_deriv
(x, y=2.718281828459045)¶ Derivative of log(x) at base y
Parameters: - x (structures.Number()) – Number to take the log of. Must have a
deriv
attribute - y (a Number object or an int/float) – Base of the logarithm.
Returns: dictionary of partial derivatives
Return type: dict
- x (structures.Number()) – Number to take the log of. Must have a
-
autodiff.operations.
logistic
(x)¶ Take the logistic(x)
Parameters: x (Number) – Number to the natural exponential in the logistic Returns: logistic(x.val) Return type: float
-
autodiff.operations.
logistic_deriv
(x)¶ Derivative of logistic(x)
Parameters: - x (structures.Number()) – Number to the natural exponential in the logistic function.
- have a deriv attribute (Must) –
Returns: dictionary of partial derivatives
Return type: dict
-
autodiff.operations.
mul
(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.
mul_deriv
(x, y)¶ Derivative of multiplication, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be multiplied
Returns: The derivative of the product of x and y
-
autodiff.operations.
negate
(x)¶ Negate
Parameters: x (Number) – Negate a number Returns: -x Return type: Number
-
autodiff.operations.
negate_deriv
(x)¶ Derivatives of the elementary negation function
Parameters: x (Number) – Number to be negated Returns: the partial derivatives of the negated Number Return type: dictionary
-
autodiff.operations.
pow_deriv
(x, a)¶ Derivative of power of a Number
Parameters: - x – a Number
- a – a Number
Returns: The derivative of the power
-
autodiff.operations.
power
(x, y)¶ power of one number by another, one of x and y has to be a Number object
Parameters: - x – a Number object or an int/float to be powered
- y – a Number object or an int/float to be the exponential
Returns: value of the difference
-
autodiff.operations.
sin
(x)¶ Take the sin(x)
Parameters: x (Number) – Number to take the sin of Returns: sin(x.val) Return type: float
-
autodiff.operations.
sin_deriv
(x)¶ Derivative of sin(x)
Parameters: x (structures.Number()) – Number to take the sin of. Must have a deriv
attributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.
sinh
(x)¶ Hyperbolic sine
Parameters: x (Number) – Value Returns: sinh(x) Return type: Number
-
autodiff.operations.
sinh_deriv
(x)¶ Hyperbolic sin derivative
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.
sqrt
(x)¶ Square root of a number
Parameters: x (Number) – Number to take the square root of Returns: the square root of Number x’s value Return type: float
-
autodiff.operations.
sqrt_deriv
(x)¶ Derivative of the square root function
Parameters: x (Number) – Number to take the square root of Returns: the partial derivatives of the square root of number Return type: dictionary
-
autodiff.operations.
subtract
(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.
subtract_deriv
(x, y)¶ Derivative of subtractions, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be subtracted
Returns: The derivative of the difference of x and y
-
autodiff.operations.
tan
(x)¶ Take the tan(x)
Parameters: x (Number) – Number to take the tan of Returns: tan(x.val) Return type: float
-
autodiff.operations.
tan_deriv
(x)¶ Derivative of tan(x)
Parameters: x (structures.Numbers()) – Number to take the tan of. Must have a deriv
attributeReturns: dictionary of partial derivatives Return type: dict
autodiff.structures module¶
Data structures for autodiff
-
class
autodiff.structures.
Array
(iterable)¶ Bases:
object
Array class is another core data structure for ‘autodiff’. It instantiates an Array object as a list of Number objects. It holds the list internally as an np.ndarray
Parameters: - iterable – a iterable objects of Numbers to be held in Array. If lists of
- are passed, it will convert them to Number objects first (ints/floats) –
Returns: Array, an object to perform automatic differentiation on.
Example
>>> import autodiff >>> x = autodiff.structures.Number(3) >>> a = autodiff.structures.Number(3,2) >>> arr = autodiff.structures.Array([x, a]) Array([Number(val=3) Number(val=3)]) >>> arr.jacobian(a) array([0, 2])
-
dot
(other)¶ Defines the dot product on two Array objects.
Returns: a Number object, which is the dot product.
-
jacobian
(order)¶ Returns the jacobian matrix by the order specified.
Parameters: order – the order to return the jacobian matrix in. Has to be not null Returns: a np.ndarray of partial derivatives specified by the order. Each row is an element in the original array, each column is the order specified. When order is a single element, it returns a flat array.
-
class
autodiff.structures.
Number
(val, deriv=None)¶ Bases:
object
Number class is the core data structure for ‘autodiff’. It instantiates a Number object by specifying a value and a derivative. Derivative with respect to itself is automatically instantiated to 1.
Parameters: - val – value of the Number
- deriv – a dictionary of partial derivatives. It is automatically instantiated to {self: 1} unless otherwise specified.
Returns: Number, an object to perform automatic differentiation on.
Example
>>> import autodiff >>> x = autodiff.structures.Number(3) >>> x.value 3 >>> x.jacobian(x) 1 >>> a = autodiff.structures.Number(3,2) >>> a.value 3 >>> a.jacobian(x) 2
-
jacobian
(order)¶ Returns the jacobian matrix by the order specified.
Parameters: order – the order to return the jacobian matrix in. Has to be not null Returns: a np.ndarray of partial derivatives specified by the order. When order is a single element, it returns a scaler