MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / __getitem__

Method __getitem__

tensorflow/python/framework/tensor_shape.py:847–895  ·  view source on GitHub ↗

Returns the value of a dimension or a shape, depending on the key. Args: key: If `key` is an integer, returns the dimension at that index; otherwise if `key` is a slice, returns a TensorShape whose dimensions are those selected by the slice from `self`. Returns:

(self, key)

Source from the content-addressed store, hash-verified

845 return iter(d for d in self._dims)
846
847 def __getitem__(self, key):
848 """Returns the value of a dimension or a shape, depending on the key.
849
850 Args:
851 key: If `key` is an integer, returns the dimension at that index;
852 otherwise if `key` is a slice, returns a TensorShape whose dimensions
853 are those selected by the slice from `self`.
854
855 Returns:
856 An integer if `key` is an integer, or a `TensorShape` if `key` is a
857 slice.
858
859 Raises:
860 ValueError: If `key` is a slice and `self` is completely unknown and
861 the step is set.
862 """
863 if self._dims is not None:
864 if isinstance(key, slice):
865 return TensorShape(self._dims[key])
866 else:
867 if self._v2_behavior:
868 return self._dims[key].value
869 else:
870 return self._dims[key]
871 else:
872 if isinstance(key, slice):
873 start = key.start if key.start is not None else 0
874 stop = key.stop
875
876 if key.step is not None:
877 # TODO(mrry): Handle these maybe.
878 raise ValueError("Steps are not yet handled")
879 if stop is None:
880 # NOTE(mrry): This implies that TensorShape(None) is compatible with
881 # TensorShape(None)[1:], which is obviously not true. It would be
882 # possible to track the number of dimensions symbolically,
883 # and perhaps we should do that.
884 return unknown_shape()
885 elif start < 0 or stop < 0:
886 # TODO(mrry): Handle this better, as it will be useful for handling
887 # suffixes of otherwise unknown shapes.
888 return unknown_shape()
889 else:
890 return unknown_shape(rank=stop - start)
891 else:
892 if self._v2_behavior:
893 return None
894 else:
895 return Dimension(None)
896
897 def num_elements(self):
898 """Returns the total number of elements, or none for incomplete shapes."""

Callers

nothing calls this directly

Calls 3

TensorShapeClass · 0.70
unknown_shapeFunction · 0.70
DimensionClass · 0.70

Tested by

no test coverage detected