tensor.optimize – Symbolic Optimization Routines#

pytensor.tensor.optimize.minimize(objective, x, method='BFGS', jac=True, hess=False, optimizer_kwargs=None)[source]#

Minimize a scalar objective function using scipy.optimize.minimize.

Parameters:
  • objective (TensorVariable) – The objective function to minimize. This should be a pytensor variable representing a scalar value.

  • x (TensorVariable) – The variable with respect to which the objective function is minimized. It must be an input to the computational graph of objective.

  • method (str, optional) – The optimization method to use. Default is “BFGS”. See scipy.optimize.minimize for other options.

  • jac (bool, optional) – Whether to compute and use the gradient of teh objective function with respect to x for optimization. Default is True.

  • optimizer_kwargs – Additional keyword arguments to pass to scipy.optimize.minimize

Returns:

  • solution (TensorVariable) – The optimized value of the vector of inputs x that minimizes objective(x, *args). If the success flag is False, this will be the final state of the minimization routine, but not necessarily a minimum.

  • success (TensorVariable) – Symbolic boolean flag indicating whether the minimization routine reported convergence to a minimum value, based on the requested convergence criteria.

pytensor.tensor.optimize.minimize_scalar(objective, x, method='brent', optimizer_kwargs=None)[source]#

Minimize a scalar objective function using scipy.optimize.minimize_scalar.

Parameters:
  • objective (TensorVariable) – The objective function to minimize. This should be a PyTensor variable representing a scalar value.

  • x (TensorVariable) – The variable with respect to which the objective function is minimized. It must be a scalar and an input to the computational graph of objective.

  • method (str, optional) – The optimization method to use. Default is “brent”. See scipy.optimize.minimize_scalar for other options.

  • optimizer_kwargs (dict, optional) – Additional keyword arguments to pass to scipy.optimize.minimize_scalar.

Returns:

  • solution (TensorVariable) – Value of x that minimizes objective(x, *args). If the success flag is False, this will be the final state returned by the minimization routine, not necessarily a minimum.

  • success (TensorVariable) – Symbolic boolean flag indicating whether the minimization routine reported convergence to a minimum value, based on the requested convergence criteria.

pytensor.tensor.optimize.root(equations, variables, method='hybr', jac=True, optimizer_kwargs=None)[source]#

Find roots of a system of equations using scipy.optimize.root.

Parameters:
  • equations (TensorVariable) – The system of equations for which to find roots. This should be a PyTensor variable representing a vector (or scalar) value. The function will find variables such that equations(variables, *args) = 0.

  • variables (TensorVariable) – The variable(s) with respect to which the system of equations is solved. It must be an input to the computational graph of equations and have the same number of dimensions as equations.

  • method (str, optional) – The root-finding method to use. Default is “hybr”. See scipy.optimize.root for other options.

  • jac (bool, optional) – Whether to compute and use the Jacobian of the equations with respect to variables. Default is True. Most methods require this.

  • optimizer_kwargs (dict, optional) – Additional keyword arguments to pass to scipy.optimize.root.

Returns:

  • solution (TensorVariable) – The final state of the root-finding routine. When success is True, this is the value of variables that causes all equations to evaluate to zero. Otherwise it is the final state returned by the root-finding routine, but not necessarily a root.

  • success (TensorVariable) – Boolean indicating whether the root-finding was successful. If True, the solution is a root of the equation

pytensor.tensor.optimize.root_scalar(equation, variable, method='secant', jac=False, hess=False, optimizer_kwargs=None)[source]#

Find roots of a scalar equation using scipy.optimize.root_scalar.

Parameters:
  • equation (TensorVariable) – The equation for which to find roots. This should be a PyTensor variable representing a single equation in one variable. The function will find variables such that equation(variables, *args) = 0.

  • variable (TensorVariable) – The variable with respect to which the equation is solved. It must be a scalar and an input to the computational graph of equation.

  • method (str, optional) – The root-finding method to use. Default is “secant”. See scipy.optimize.root_scalar for other options.

  • jac (bool, optional) – Whether to compute and use the first derivative of the equation with respect to variables. Default is False. Some methods require this.

  • hess (bool, optional) – Whether to compute and use the second derivative of the equation with respect to variables. Default is False. Some methods require this.

  • optimizer_kwargs (dict, optional) – Additional keyword arguments to pass to scipy.optimize.root_scalar.

Returns:

  • solution (TensorVariable) – The final state of the root-finding routine. When success is True, this is the value of variables that causes equation to evaluate to zero. Otherwise it is the final state returned by the root-finding routine, but not necessarily a root.

  • success (TensorVariable) – Boolean indicating whether the root-finding was successful. If True, the solution is a root of the equation