def _value_and_gradients(fn, fn_arg_list, result=None, grads=None, name=None):
with tf.compat.v1.name_scope(name, '<STR_LIT>',<EOL>[fn_arg_list, result, grads]):<EOL><INDENT>def _convert_to_tensor(x, name):<EOL><INDENT>ctt = lambda x_: x_ if x_ is None else tf.convert_to_tensor(<EOL>value=x_, name=name)<EOL>return [ctt(x_) for x_ in x] if is_list_like(x) else ctt(x)<EOL><DEDENT>fn_arg_list = (list(fn_arg_list) if is_list_like(fn_arg_list)<EOL>else [fn_arg_list])<EOL>fn_arg_list = _convert_to_tensor(fn_arg_list, '<STR_LIT>')<EOL>if result is None:<EOL><INDENT>result = fn(*fn_arg_list)<EOL>if grads is None and tf.executing_eagerly():<EOL><INDENT>fn_arg_list = [<NUM_LIT:0> + x for x in fn_arg_list]<EOL><DEDENT><DEDENT>result = _convert_to_tensor(result, '<STR_LIT>')<EOL>if grads is not None:<EOL><INDENT>grads = _convert_to_tensor(grads, '<STR_LIT>')<EOL>return result, grads<EOL><DEDENT>if is_list_like(result) and len(result) == len(fn_arg_list):<EOL><INDENT>def fn_slice(i):<EOL><INDENT>"""<STR_LIT>"""<EOL>return lambda x: fn(*(fn_arg_list[:i] + [x] + fn_arg_list[i+<NUM_LIT:1>:]))<EOL><DEDENT>grads = [<EOL>tfp_math_value_and_gradients(fn_slice(i), fn_arg_list[i])[<NUM_LIT:1>]<EOL>for i in range(len(result))<EOL>]<EOL><DEDENT>else:<EOL><INDENT>_, grads = tfp_math_value_and_gradients(fn, fn_arg_list)<EOL><DEDENT>return result, grads<EOL><DEDENT>
Helper to `maybe_call_fn_and_grads`.