Gradients#
The module aesara.grad allows to compute the gradient of an Aesara graph.
- aesara.gradient.grad(cost: Optional[Variable], wrt: Union[Variable, Sequence[Variable]], consider_constant: Optional[Sequence[Variable]] = None, disconnected_inputs: Literal['ignore', 'warn', 'raise'] = 'raise', add_names: bool = True, known_grads: Optional[Mapping[Variable, Variable]] = None, return_disconnected: Literal['none', 'zero', 'disconnected'] = 'zero', null_gradients: Literal['raise', 'return'] = 'raise') Union[Variable, None, Sequence[Optional[Variable]]][source]#
Return symbolic gradients of one cost with respect to one or more variables.
For more information about how automatic differentiation works in Aesara, see
gradient. For information on how to implement the gradient of a certain Op, seegrad().- Parameters:
cost – Value that we are differentiating (i.e. for which we want the gradient). May be
Noneifknown_gradsis provided.wrt – The term(s) with respect to which we want gradients.
consider_constant – Expressions not to backpropagate through.
disconnected_inputs ({'ignore', 'warn', 'raise'}) –
Defines the behaviour if some of the variables in
wrtare not part of the computational graph computingcost(or if all links are non-differentiable). The possible values are:'ignore': considers that the gradient on these parameters is zero'warn': consider the gradient zero, and print a warning'raise': raiseDisconnectedInputError
add_names – If
True, variables generated bygradwill be named(d<cost.name>/d<wrt.name>)provided that bothcostandwrthave names.known_grads – An ordered dictionary mapping variables to their gradients. This is useful in the case where you know the gradients of some variables but do not know the original cost.
return_disconnected –
'zero': Ifwrt[i]is disconnected, return valueiwill bewrt[i].zeros_like()'none': Ifwrt[i]is disconnected, return valueiwill beNone'disconnected': returns variables of typeDisconnectedType
null_gradients –
Defines the behaviour when some of the variables in
wrthave a null gradient. The possibles values are:'raise': raise aNullTypeGradErrorexception'return': return the null gradients
- Returns:
A symbolic expression for the gradient of
costwith respect to eachof the
wrtterms. If an element ofwrtis not differentiable withrespect to the output, then a zero variable is returned.
This section of the documentation is organized as follows:
Derivatives in Aesara gives a hands-on introduction to how to build gradient graphs in Aesara.
Gradients API is an API reference for the
aesara.gradientmodule.