ML Community Day is November 9! Join us for updates from TensorFlow, JAX, and more Learn more


Applies the BFGS algorithm to minimize a differentiable function.

Performs unconstrained minimization of a differentiable function using the BFGS scheme. For details of the algorithm, see [Nocedal and Wright(2006)][1].


The following example demonstrates the BFGS optimizer attempting to find the minimum for a simple two dimensional quadratic objective function.

  minimum = np.array([1.0, 1.0])  # The center of the quadratic bowl.
  scales = np.array([2.0, 3.0])  # The scales along the two axes.

  # The objective function and the gradient.
  def quadratic_loss_and_gradient(x):
    return tfp.math.value_and_gradient(
        lambda x: tf.reduce_sum(
            scales * tf.math.squared_difference(x, minimum), axis=-1),

  start = tf.constant([0.6, 0.8])  # Starting point for the search.
  optim_results = tfp.optimizer.bfgs_minimize(
      quadratic_loss_and_gradient, initial_position=start, tolerance=1e-8)

  # Check that the search converged
  # Check that the argmin is close to the actual value.
  np.testing.assert_allclose(optim_results.position, minimum)
  # Print out the total number of function evaluations it took. Should be 5.
  print ("Function evaluations: %d" % optim_results.num_objective_evaluations)


[1]: Jorge Nocedal, Stephen Wright. Numerical Optimization. Springer Series in Operations Research. pp 136-140. 2006

value_and_gradients_function A Python callable that accepts a point as a real Tensor and returns a tuple of Tensors of real dtype containing the value of the function and its gradient at that point. The function to be minimized. The input should be of shape [..., n], where n is the size of the domain of input points, and all others are batching dimensions. The first component of the return value should be a real Tensor of matching shape [...]. The second component (the gradient) should also be of shape [..., n] like the input value to the function.
initial_position real Tensor of shape [..., n]. The starting point, or points when using batching dimensions, of the search procedure. At these points the function value and the gradient norm should be finite.
tolerance Scalar Tensor of real dtype. Specifies the gradient tolerance for the procedure. If the supremum norm of the gradient vector is below this number, the algorithm is stopped.
x_tolerance Scalar Tensor of real dtype. If the absolute change in the position between one iteration and the next is smaller than this number, the algorithm is stopped.
f_relative_tolerance Scalar Tensor of real dtype. If the relative change in the objective value between one iteration and the next is smaller than this value, the algorithm is stopped.
initial_inverse_hessian_estimate Optional Tensor of the same dtype as the components of the output of the value_and_gradients_function. If specified, the shape should broadcastable to shape [..., n, n]; e.g. if a single [n, n] matrix is provided, it will be automatically broadcasted to all batches. Alternatively, one can also specify a different hessian estimate for each batch member. For the correctness of the algorithm, it is required that this parameter be symmetric and positive definite. Specifies the starting estimate for the inverse of the Hessian at the initial point. If not specified, the identity matrix is used as the starting estimate for the inverse Hessian.
max_iterations Scalar positive int32 Tensor. The maximum number of iterations for BFGS updates.
parallel_iterations Positive integer. The number of iterations allowed to run in parallel.
stopping_condition (Optional) A Python function that takes as input two Boolean tensors of shape [...], and returns a Boolean scalar tensor. The input tensors are converged and failed, indicating the current status of each respective batch member; the return value states whether the algorithm should stop. The default is tfp.optimizer.converged_all which only stops when all batch members have either converged or failed. An alternative is tfp.optimizer.converged_any which stops as soon as one batch member has converged, or when all have failed.
validate_args Python bool, default True. When True optimizer parameters are checked for validity despite possibly degrading runtime performance. When False invalid inputs may silently render incorrect outputs.
max_line_search_iterations Python int. The maximum number of iterations for the hager_zhang line search algorithm.
name (Optional) Python str. The name prefixed to the ops created by this function. If not supplied, the default name 'minimize' is used.

optimizer_results A namedtuple containing the following items: converged: boolean tensor of shape [...] indicating for each batch member whether the minimum was found within tolerance. failed: boolean tensor of shape [...] indicating for each batch member whether a line search step failed to find a suitable step size satisfying Wolfe conditions. In the absence of any constraints on the number of objective evaluations permitted, this value will be the complement of converged. However, if there is a constraint and the search stopped due to available evaluations being exhausted, both failed and converged will be simultaneously False. num_objective_evaluations: The total number of objective evaluations performed. position: A tensor of shape [..., n] containing the last argument value found during the search from each starting point. If the search converged, then this value is the argmin of the objective function. objective_value: A tensor of shape [...] with the value of the objective function at the position. If the search converged, then this is the (local) minimum of the objective function. objective_gradient: A tensor of shape [..., n] containing the gradient of the objective function at the position. If the search converged the max-norm of this tensor should be below the tolerance. inverse_hessian_estimate: A tensor of shape [..., n, n] containing the inverse of the estimated Hessian.