tensorflow / probability

Probabilistic reasoning and statistical analysis in TensorFlow
https://www.tensorflow.org/probability/
Apache License 2.0
4.26k stars 1.1k forks source link

LBFGS documentation example error in TensorFlow 2.0 #624

Closed a-z-e-r-i-l-a closed 4 years ago

a-z-e-r-i-l-a commented 5 years ago

I am trying this example with tensorflow 2.0:

# A high-dimensional quadratic bowl.
ndims = 60
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims, dtype='float64') + 1.0

# The objective function and the gradient.
def quadratic(x):
    value = tf.reduce_sum(scales * (x - minimum) ** 2)
    return value, tf.gradients(value, x)[0]

start = np.arange(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(
  quadratic, initial_position=start, num_correction_pairs=10,
  tolerance=1e-8)

with tf.Session() as session:
    results = session.run(optim_results)
# Check that the search converged
assert(results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(results.position, minimum)

With which I get the following error:


RuntimeError Traceback (most recent call last)

in 12 optim_results = tfp.optimizer.lbfgs_minimize( 13 quadratic, initial_position=start, num_correction_pairs=10, ---> 14 tolerance=1e-8) 15 16 with tf.Session() as session: ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/lbfgs.py in minimize(value_and_gradients_function, initial_position, num_correction_pairs, tolerance, x_tolerance, f_relative_tolerance, initial_inverse_hessian_estimate, max_iterations, parallel_iterations, stopping_condition, name) 253 initial_position, 254 num_correction_pairs, --> 255 tolerance) 256 return tf.while_loop( 257 cond=_cond, ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/lbfgs.py in _get_initial_state(value_and_gradients_function, initial_position, num_correction_pairs, tolerance) 269 value_and_gradients_function, 270 initial_position, --> 271 tolerance) 272 empty_queue = _make_empty_queue_for(num_correction_pairs, initial_position) 273 init_args.update(position_deltas=empty_queue, gradient_deltas=empty_queue) ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/bfgs_utils.py in get_initial_state_args(value_and_gradients_function, initial_position, grad_tolerance, control_inputs) 80 f0, df0 = value_and_gradients_function(initial_position) 81 else: ---> 82 f0, df0 = value_and_gradients_function(initial_position) 83 converged = norm(df0, dims=1) < grad_tolerance 84 return dict( in quadratic(x) 7 def quadratic(x): 8 value = tf.reduce_sum(scales * (x - minimum) ** 2) ----> 9 return value, tf.gradients(value, x)[0] 10 11 start = np.arange(ndims, 0, -1, dtype='float64') ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_impl.py in gradients_v2(ys, xs, grad_ys, name, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients) 272 ys, xs, grad_ys, name, True, gate_gradients, 273 aggregation_method, stop_gradients, --> 274 unconnected_gradients) 275 # pylint: enable=protected-access 276 ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python/ops/gradients_util.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients, unconnected_gradients, src_graph) 503 """Implementation of gradients().""" 504 if context.executing_eagerly(): --> 505 raise RuntimeError("tf.gradients is not supported when eager execution " 506 "is enabled. Use tf.GradientTape instead.") 507 if src_graph is None: RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.

Then I use the gradient tape instead:

# A high-dimensional quadratic bowl.
ndims = 60
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims,  dtype='float64') + 1.0

# The objective function and the gradient.
def quadratic(x):

    x = tf.Variable(x, dtype='float64')

    with tf.GradientTape() as t:
        value = tf.reduce_sum(scales * (x - minimum) ** 2)

    grad = t.gradient(value, x)
    print(grad)

    return value, grad

start = np.arange(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(
  quadratic, initial_position=start, num_correction_pairs=10,
  tolerance=1e-8)

with tf.Session() as session:
    results = session.run(optim_results)
# Check that the search converged
assert(results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(results.position, minimum)

This also returns the following error:


TypeError Traceback (most recent call last)

in 20 optim_results = tfp.optimizer.lbfgs_minimize( 21 quadratic, initial_position=start, num_correction_pairs=10, ---> 22 tolerance=1e-8) 23 24 with tf.Session() as session: ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/lbfgs.py in minimize(value_and_gradients_function, initial_position, num_correction_pairs, tolerance, x_tolerance, f_relative_tolerance, initial_inverse_hessian_estimate, max_iterations, parallel_iterations, stopping_condition, name) 258 body=_body, 259 loop_vars=[initial_state], --> 260 parallel_iterations=parallel_iterations)[0] 261 262 ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py in while_loop_v2(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, maximum_iterations, name) 2476 name=name, 2477 maximum_iterations=maximum_iterations, -> 2478 return_same_structure=True) 2479 2480 ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations, return_same_structure) 2712 list(loop_vars)) 2713 while cond(*loop_vars): -> 2714 loop_vars = body(*loop_vars) 2715 if try_to_pack and not isinstance(loop_vars, (list, _basetuple)): 2716 packed = True ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/lbfgs.py in _body(current_state) 227 def _body(current_state): 228 """Main optimization loop.""" --> 229 search_direction = _get_search_direction(current_state) 230 231 # TODO(b/120134934): Check if the derivative at the start point is not ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/optimizer/lbfgs.py in _get_search_direction(state) 364 return prefer_static.cond(tf.equal(num_elements, 0), 365 (lambda: -state.objective_gradient), --> 366 _two_loop_algorithm) 367 368 ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/internal/prefer_static.py in cond(pred, true_fn, false_fn, name) 170 raise TypeError('`false_fn` must be callable.') 171 --> 172 pred_value = _get_static_predicate(pred) 173 if pred_value is not None: 174 if pred_value: ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_probability/python/internal/prefer_static.py in _get_static_predicate(pred) 81 def _get_static_predicate(pred): 82 """Helper function for statically evaluating predicates in `cond`.""" ---> 83 if pred in {0, 1}: # Accept 1/0 as valid boolean values 84 pred_value = bool(pred) 85 elif isinstance(pred, bool): ~/.local/share/virtualenvs/tf-tRAPLeXL/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __hash__(self) 711 if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and 712 (g is None or g._building_function)): # pylint: disable=protected-access --> 713 raise TypeError("Tensor is unhashable if Tensor equality is enabled. " 714 "Instead, use tensor.experimental_ref() as the key.") 715 else: TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.

Anyone knows how I can use lbfgs in tensorflow 2.0?

a-z-e-r-i-l-a commented 5 years ago

this stackoverflow answer helped solving the problem. I had to also install tensorflow-probability although I hadn't got any error that it is not installed.

dynamicwebpaige commented 4 years ago

Closing out this issue, as TFP was not installed.

Please feel free to reopen if you hit any other snags! 🙂