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

Method Slice

tensorflow/core/util/sparse/sparse_tensor.h:663–735  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

661
662template <typename T>
663StatusOr<SparseTensor> SparseTensor::Slice(const SparseTensor& input_tensor,
664 const gtl::ArraySlice<int64>& start,
665 const gtl::ArraySlice<int64>& size) {
666 TensorShape output_shape(input_tensor.shape());
667
668 const int dims = input_tensor.dims();
669 for (int dim = 0; dim < dims; dim++) {
670 int64 dim_size = start[dim] + size[dim] < output_shape.dim_size(dim)
671 ? size[dim]
672 : output_shape.dim_size(dim) - start[dim];
673 TF_RETURN_IF_ERROR(output_shape.SetDimWithStatus(dim, dim_size));
674 }
675
676 auto input_indices_t = input_tensor.indices().matrix<int64>();
677 auto input_values_t = input_tensor.values().vec<T>();
678
679 // Find the number of indices that fall inside start and size.
680 int count = 0;
681 for (int i = 0; i < input_tensor.indices().dim_size(0); i++) {
682 // The following will check to see if an input is within the
683 // range specified by start and size.
684 // The for loop below iterates through all dimensions. In case
685 // the index falls outside of the start and size at any dimension,
686 // it will be considered as a "no hit" (hit = false). In this
687 // case, it will not be counted as the index that fall inside
688 // the range specified by start and size.
689 bool hit = true;
690 for (int dim = 0; dim < dims; dim++) {
691 if (!(start[dim] <= input_indices_t(i, dim) &&
692 input_indices_t(i, dim) < start[dim] + size[dim])) {
693 hit = false;
694 break;
695 }
696 }
697 if (!hit) {
698 continue;
699 }
700 count++;
701 }
702
703 Tensor output_values(DataTypeToEnum<T>::v(), TensorShape({count}));
704 Tensor output_indices(DT_INT64, TensorShape({count, dims}));
705
706 auto output_values_t = output_values.vec<T>();
707 auto output_indices_t = output_indices.matrix<int64>();
708
709 // Obtain the output indices that fall inside start and size.
710 int index = 0;
711 for (int i = 0; i < input_tensor.indices().dim_size(0) && index < count;
712 i++) {
713 // The logic here is similar as the above except that the above
714 // only count the number of indices while here we actually generate
715 // the output.
716 bool hit = true;
717 for (int dim = 0; dim < dims; dim++) {
718 if (!(start[dim] <= input_indices_t(i, dim) &&
719 input_indices_t(i, dim) < start[dim] + size[dim])) {
720 hit = false;

Callers

nothing calls this directly

Calls 8

SetDimWithStatusMethod · 0.80
SparseTensorFunction · 0.70
TensorShapeClass · 0.50
shapeMethod · 0.45
dimsMethod · 0.45
dim_sizeMethod · 0.45
indicesMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected