Like conv_v2, except emits a Case op instead of an If.
(branch_index, branch_fns, name="indexed_case")
| 868 | |
| 869 | |
| 870 | def indexed_case(branch_index, branch_fns, name="indexed_case"): |
| 871 | """Like conv_v2, except emits a Case op instead of an If.""" |
| 872 | if isinstance(branch_index, int): |
| 873 | raise TypeError("branch_index must not be a Python int", branch_index) |
| 874 | |
| 875 | with ops.name_scope(name) as scope: |
| 876 | branch_names = [ |
| 877 | util.unique_fn_name(scope, "branch{}".format(b)) |
| 878 | for b in range(len(branch_fns)) |
| 879 | ] |
| 880 | |
| 881 | # Automatic control dependencies are added in defuns, but not in v1 |
| 882 | # graphs. Propagate that behavior here. |
| 883 | add_control_dependencies = ops.get_default_graph()._add_control_dependencies |
| 884 | branch_index = ops.convert_to_tensor(branch_index, name="branch_index") |
| 885 | |
| 886 | branch_graphs = [] |
| 887 | for branch_name, branch_fn in zip(branch_names, branch_fns): |
| 888 | branch_graphs.append( |
| 889 | func_graph_module.func_graph_from_py_func( |
| 890 | branch_name, |
| 891 | branch_fn, |
| 892 | [], |
| 893 | {}, |
| 894 | func_graph=util.CondBranchFuncGraph( |
| 895 | branch_name, |
| 896 | collections=ops.get_default_graph()._collections), # pylint: disable=protected-access |
| 897 | add_control_dependencies=add_control_dependencies, |
| 898 | op_return_value=branch_index)) |
| 899 | |
| 900 | verify_captures(_CASE, branch_graphs) |
| 901 | return _build_case( |
| 902 | branch_index, |
| 903 | branch_graphs, [g.external_captures for g in branch_graphs], |
| 904 | name=scope) |
| 905 | |
| 906 | |
| 907 | @ops.RegisterGradient("Case") |
nothing calls this directly
no test coverage detected