tensorflow icon indicating copy to clipboard operation
tensorflow copied to clipboard

Gradient for self_adjoint_eig

Open black-puppydog opened this issue 10 years ago • 3 comments

Well, what to say. This would be cool to have. According to the theano docs and sources it is not all that complicated. Their implementation uses the triangle functions mentioned in #1825 though.

black-puppydog avatar Apr 13 '16 12:04 black-puppydog

Hello - Thank you for your work on this project. I really appreciate everything that has gone into it.

I've noticed that gradients are now provided for the 'self_adjoint_eig' function but they are not for the 'self_adjoint_eigvals'. Using the 'self_adjoint_eig' function and just using the eigenvalues is, of course, a sufficient work-around, but perhaps you would like to resolve this for completeness.

For example, running:

import tensorflow as tf
a = tf.random_normal((10000,3))
covar = tf.matmul(tf.transpose(a), a)
eigvals, eigvect_holder = tf.self_adjoint_eig(covar)
eigvects = tf.transpose(eigvect_holder)
pure_eigvals = tf.self_adjoint_eigvals(covar)

with tf.Session() as sess:
       print(sess.run(eigvals))
       print(sess.run(eigvects))
       print(sess.run(pure_eigvals))
       print(sess.run(tf.gradients(eigvals, a)))
       print(sess.run(tf.gradients(pure_eigvals, a)))

Results in:

`[ 9719.09765625 10004.32910156 10070.64941406]

[[-0.27976531 -0.12752065 -0.95156187] [-0.9541291 0.14700767 0.26081926] [ 0.10662714 0.98088086 -0.16279887]]

[ 9808.58398438 10092.38964844 10352.38378906]

[array([[-2.07045388, 1.91732657, 0.77774417], [-0.6452961 , -3.11206341, -1.97885656], [-1.94984984, -0.21588665, -1.53561687], ..., [ 2.76743388, 2.91140604, -1.23593211], [-2.70180821, -3.767308 , 0.29746097], [-2.36184692, 0.6249733 , 0.13678969]], dtype=float32)]


InvalidArgumentError Traceback (most recent call last) /home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn) 670 graph_def_version, node_def_str, input_shapes, input_tensors, --> 671 input_tensors_as_shapes, status) 672 except errors.InvalidArgumentError as err:

/home/ubuntu/anaconda3/lib/python3.6/contextlib.py in exit(self, type, value, traceback) 88 try: ---> 89 next(self.gen) 90 except StopIteration:

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status() 465 compat.as_text(pywrap_tensorflow.TF_Message(status)), --> 466 pywrap_tensorflow.TF_GetCode(status)) 467 finally:

InvalidArgumentError: Shape must be rank 2 but is rank 1 for 'gradients_3/SelfAdjointEigV2_3_grad/MatMul' (op: 'MatMul') with input shapes: [0], [0].

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) in () 12 print(sess.run(pure_eigvals)) 13 print(sess.run(tf.gradients(eigvals, a))) ---> 14 print(sess.run(tf.gradients(pure_eigvals, a)))

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method) 480 # If grad_fn was found, do not use SymbolicGradient even for 481 # functions. --> 482 in_grads = grad_fn(op, *out_grads) 483 else: 484 # For function call ops, we add a 'SymbolicGradient'

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/linalg_grad.py in _SelfAdjointEigV2Grad(op, grad_e, grad_v) 203 math_ops.matmul( 204 array_ops.matrix_diag(grad_e) + f * math_ops.matmul( --> 205 v, grad_v, adjoint_a=True), 206 v, 207 adjoint_b=True))

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py in matmul(a, b, transpose_a, transpose_b, adjoint_a, adjoint_b, a_is_sparse, b_is_sparse, name) 1763 else: 1764 return gen_math_ops._mat_mul( -> 1765 a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name) 1766 1767

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py in _mat_mul(a, b, transpose_a, transpose_b, name) 1452 """ 1453 result = _op_def_lib.apply_op("MatMul", a=a, b=b, transpose_a=transpose_a, -> 1454 transpose_b=transpose_b, name=name) 1455 return result 1456

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords) 761 op = g.create_op(op_type_name, inputs, output_types, name=scope, 762 input_types=input_types, attrs=attr_protos, --> 763 op_def=op_def) 764 if output_structure: 765 outputs = op.outputs

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device) 2327 original_op=self._default_original_op, op_def=op_def) 2328 if compute_shapes: -> 2329 set_shapes_for_outputs(ret) 2330 self._add_op(ret) 2331 self._record_op_seen_by_control_dependencies(ret)

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in set_shapes_for_outputs(op) 1715 shape_func = _call_cpp_shape_fn_and_require_op 1716 -> 1717 shapes = shape_func(op) 1718 if shapes is None: 1719 raise RuntimeError(

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in call_with_requiring(op) 1665 1666 def call_with_requiring(op): -> 1667 return call_cpp_shape_fn(op, require_shape_fn=True) 1668 1669 _call_cpp_shape_fn_and_require_op = call_with_requiring

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in call_cpp_shape_fn(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn) 608 res = _call_cpp_shape_fn_impl(op, input_tensors_needed, 609 input_tensors_as_shapes_needed, --> 610 debug_python_shape_fn, require_shape_fn) 611 if not isinstance(res, dict): 612 # Handles the case where _call_cpp_shape_fn_impl calls unknown_shape(op).

/home/ubuntu/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py in _call_cpp_shape_fn_impl(op, input_tensors_needed, input_tensors_as_shapes_needed, debug_python_shape_fn, require_shape_fn) 674 missing_shape_fn = True 675 else: --> 676 raise ValueError(err.message) 677 678 if missing_shape_fn:

ValueError: Shape must be rank 2 but is rank 1 for 'gradients_3/SelfAdjointEigV2_3_grad/MatMul' (op: 'MatMul') with input shapes: [0], [0]. `

VinceMarron avatar Apr 03 '17 16:04 VinceMarron

Automatically closing due to lack of recent activity. Since this issue is old at this point, please reopen the issue if it still occurs when tried with the latest version of Tensorflow. Thank you.

itsmeolivia avatar Jun 16 '17 20:06 itsmeolivia

Got the same problem as above, there must be something wrong with the implementation of gradients in the latest release.

minogame avatar Jul 11 '17 11:07 minogame