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

Function _slice_helper

tensorflow/python/ops/array_ops.py:658–802  ·  view source on GitHub ↗

Overload for Tensor.__getitem__. This operation extracts the specified region from the tensor. The notation is similar to NumPy with the restriction that currently only support basic indexing. That means that using a non-scalar tensor as input is not currently allowed. Some useful exampl

(tensor, slice_spec, var=None)

Source from the content-addressed store, hash-verified

656
657
658def _slice_helper(tensor, slice_spec, var=None):
659 """Overload for Tensor.__getitem__.
660
661 This operation extracts the specified region from the tensor.
662 The notation is similar to NumPy with the restriction that
663 currently only support basic indexing. That means that
664 using a non-scalar tensor as input is not currently allowed.
665
666 Some useful examples:
667
668 ```python
669 # Strip leading and trailing 2 elements
670 foo = tf.constant([1,2,3,4,5,6])
671 print(foo[2:-2].eval()) # => [3,4]
672
673 # Skip every other row and reverse the order of the columns
674 foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
675 print(foo[::2,::-1].eval()) # => [[3,2,1], [9,8,7]]
676
677 # Use scalar tensors as indices on both dimensions
678 print(foo[tf.constant(0), tf.constant(2)].eval()) # => 3
679
680 # Insert another dimension
681 foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
682 print(foo[tf.newaxis, :, :].eval()) # => [[[1,2,3], [4,5,6], [7,8,9]]]
683 print(foo[:, tf.newaxis, :].eval()) # => [[[1,2,3]], [[4,5,6]], [[7,8,9]]]
684 print(foo[:, :, tf.newaxis].eval()) # => [[[1],[2],[3]], [[4],[5],[6]],
685 [[7],[8],[9]]]
686
687 # Ellipses (3 equivalent operations)
688 foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
689 print(foo[tf.newaxis, :, :].eval()) # => [[[1,2,3], [4,5,6], [7,8,9]]]
690 print(foo[tf.newaxis, ...].eval()) # => [[[1,2,3], [4,5,6], [7,8,9]]]
691 print(foo[tf.newaxis].eval()) # => [[[1,2,3], [4,5,6], [7,8,9]]]
692
693 # Masks
694 foo = tf.constant([[1,2,3], [4,5,6], [7,8,9]])
695 print(foo[foo > 2].eval()) # => [3, 4, 5, 6, 7, 8, 9]
696 ```
697
698 Notes:
699 - `tf.newaxis` is `None` as in NumPy.
700 - An implicit ellipsis is placed at the end of the `slice_spec`
701 - NumPy advanced indexing is currently not supported.
702
703 Args:
704 tensor: An ops.Tensor object.
705 slice_spec: The arguments to Tensor.__getitem__.
706 var: In the case of variable slice assignment, the Variable object to slice
707 (i.e. tensor is the read-only view of this variable).
708
709 Returns:
710 The appropriate slice of "tensor", based on "slice_spec".
711
712 Raises:
713 ValueError: If a slice range is negative size.
714 TypeError: If the slice indices aren't int, slice, ellipsis,
715 tf.newaxis or scalar int32/int64 tensors.

Callers 1

_SliceHelperVarFunction · 0.85

Calls 8

constantFunction · 0.90
_check_indexFunction · 0.85
strided_sliceFunction · 0.85
boolean_maskFunction · 0.70
stackFunction · 0.70
appendMethod · 0.45
name_scopeMethod · 0.45
castMethod · 0.45

Tested by

no test coverage detected