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

Function slice_function

tensorflow/contrib/labeled_tensor/python/ops/core.py:622–672  ·  view source on GitHub ↗

Slice out a subset of the tensor. This is an analog of tf.slice. For example: >>> tensor = tf.reshape(tf.range(0, 6), [3, 2]) >>> labeled_tensor = lt.LabeledTensor(tensor, ['a', ('b', ['foo', 'bar'])]) >>> lt.slice(labeled_tensor, {'a': slice(0, 2), 'b': 1}) <LabeledTensor 'lt_slice:...

(labeled_tensor, selection, name=None)

Source from the content-addressed store, hash-verified

620@tc.accepts(LabeledTensorLike, tc.Mapping(string_types, tc.Union(int, slice)),
621 tc.Optional(string_types))
622def slice_function(labeled_tensor, selection, name=None):
623 """Slice out a subset of the tensor.
624
625 This is an analog of tf.slice.
626 For example:
627 >>> tensor = tf.reshape(tf.range(0, 6), [3, 2])
628 >>> labeled_tensor = lt.LabeledTensor(tensor, ['a', ('b', ['foo', 'bar'])])
629 >>> lt.slice(labeled_tensor, {'a': slice(0, 2), 'b': 1})
630 <LabeledTensor 'lt_slice:...' shape=(2,) dtype=int32
631 axes=[('a', Dimension(2))]>
632
633 Args:
634 labeled_tensor: The input tensor.
635 selection: A dictionary of type str -> Union(int, slice of int) mapping axis
636 names to sub-selections.
637 name: Optional op name.
638
639 Returns:
640 The slice as a `LabeledTensor`.
641 """
642 with ops.name_scope(name, 'lt_slice', [labeled_tensor]) as scope:
643 labeled_tensor = convert_to_labeled_tensor(labeled_tensor)
644
645 slices = []
646
647 for axis_name in labeled_tensor.axes:
648 if axis_name not in selection:
649 # We're not sub-selecting this axis, so use the full slice.
650 slices.append(slice(None))
651 else:
652 slices.append(selection[axis_name])
653
654 sliced_tensor = labeled_tensor.tensor[tuple(slices)]
655
656 sliced_axes = []
657 for axis, s in zip(labeled_tensor.axes.values(), slices):
658 # We sub-select this axis's index with the slice s.
659
660 # `s` is either an int or a proper slice.
661 if isinstance(s, slice):
662 if axis.labels is None:
663 # We're not tracking coordinate names for this axis.
664 sliced_axes.append(axis.name)
665 else:
666 sliced_axes.append((axis.name, axis.labels[s]))
667 else:
668 # If the slice is an int this dimension now has size 1, so we remove it.
669 assert isinstance(s, int)
670
671 return LabeledTensor(
672 array_ops.identity(sliced_tensor, name=scope), sliced_axes)
673
674
675@tc.returns(LabeledTensor)

Callers 1

__getitem__Method · 0.85

Calls 8

tupleFunction · 0.85
LabeledTensorClass · 0.85
sliceFunction · 0.50
name_scopeMethod · 0.45
appendMethod · 0.45
valuesMethod · 0.45
identityMethod · 0.45

Tested by

no test coverage detected