See `Graph.as_graph_element()` for details.
(self, obj, allow_tensor, allow_operation)
| 3618 | return self._as_graph_element_locked(obj, allow_tensor, allow_operation) |
| 3619 | |
| 3620 | def _as_graph_element_locked(self, obj, allow_tensor, allow_operation): |
| 3621 | """See `Graph.as_graph_element()` for details.""" |
| 3622 | # The vast majority of this function is figuring |
| 3623 | # out what an API user might be doing wrong, so |
| 3624 | # that we can give helpful error messages. |
| 3625 | # |
| 3626 | # Ideally, it would be nice to split it up, but we |
| 3627 | # need context to generate nice error messages. |
| 3628 | |
| 3629 | if allow_tensor and allow_operation: |
| 3630 | types_str = "Tensor or Operation" |
| 3631 | elif allow_tensor: |
| 3632 | types_str = "Tensor" |
| 3633 | elif allow_operation: |
| 3634 | types_str = "Operation" |
| 3635 | else: |
| 3636 | raise ValueError("allow_tensor and allow_operation can't both be False.") |
| 3637 | |
| 3638 | temp_obj = _as_graph_element(obj) |
| 3639 | if temp_obj is not None: |
| 3640 | obj = temp_obj |
| 3641 | |
| 3642 | # If obj appears to be a name... |
| 3643 | if isinstance(obj, compat.bytes_or_text_types): |
| 3644 | name = compat.as_str(obj) |
| 3645 | |
| 3646 | if ":" in name and allow_tensor: |
| 3647 | # Looks like a Tensor name and can be a Tensor. |
| 3648 | try: |
| 3649 | op_name, out_n = name.split(":") |
| 3650 | out_n = int(out_n) |
| 3651 | except: |
| 3652 | raise ValueError("The name %s looks a like a Tensor name, but is " |
| 3653 | "not a valid one. Tensor names must be of the " |
| 3654 | "form \"<op_name>:<output_index>\"." % repr(name)) |
| 3655 | if op_name in self._nodes_by_name: |
| 3656 | op = self._nodes_by_name[op_name] |
| 3657 | else: |
| 3658 | raise KeyError("The name %s refers to a Tensor which does not " |
| 3659 | "exist. The operation, %s, does not exist in the " |
| 3660 | "graph." % (repr(name), repr(op_name))) |
| 3661 | try: |
| 3662 | return op.outputs[out_n] |
| 3663 | except: |
| 3664 | raise KeyError("The name %s refers to a Tensor which does not " |
| 3665 | "exist. The operation, %s, exists but only has " |
| 3666 | "%s outputs." % |
| 3667 | (repr(name), repr(op_name), len(op.outputs))) |
| 3668 | |
| 3669 | elif ":" in name and not allow_tensor: |
| 3670 | # Looks like a Tensor name but can't be a Tensor. |
| 3671 | raise ValueError("Name %s appears to refer to a Tensor, not a %s." % |
| 3672 | (repr(name), types_str)) |
| 3673 | |
| 3674 | elif ":" not in name and allow_operation: |
| 3675 | # Looks like an Operation name and can be an Operation. |
| 3676 | if name not in self._nodes_by_name: |
| 3677 | raise KeyError("The name %s refers to an Operation not in the " |
no test coverage detected