Check if a given value is a valid index into a tensor.
(idx)
| 640 | |
| 641 | |
| 642 | def _check_index(idx): |
| 643 | """Check if a given value is a valid index into a tensor.""" |
| 644 | if isinstance(idx, (six.integer_types, tensor_shape.Dimension)): |
| 645 | return |
| 646 | |
| 647 | # Optimistic check. Assumptions: |
| 648 | # * any object with a dtype is supported |
| 649 | # * any object with a dtype has a sizeable shape attribute. |
| 650 | dtype = getattr(idx, "dtype", None) |
| 651 | if (dtype is None or dtypes.as_dtype(dtype) not in _SUPPORTED_SLICE_DTYPES or |
| 652 | idx.shape and len(idx.shape) == 1): |
| 653 | # TODO(slebedev): IndexError seems more appropriate here, but it |
| 654 | # will break `_slice_helper` contract. |
| 655 | raise TypeError(_SLICE_TYPE_ERROR + ", got {!r}".format(idx)) |
| 656 | |
| 657 | |
| 658 | def _slice_helper(tensor, slice_spec, var=None): |