Return a strided slice from input.
tf.raw_ops.StridedSlice(
input,
begin,
end,
strides,
begin_mask=0,
end_mask=0,
ellipsis_mask=0,
new_axis_mask=0,
shrink_axis_mask=0,
name=None
)
Note, most python users will want to use the Python Tensor.getitem
or Variable.getitem rather than this op directly.
The goal of this op is to produce a new tensor with a subset of
the elements from the n dimensional input tensor. The subset is chosen using
a sequence of m sparse range specifications encoded into the arguments
of this function. Note, in some cases
m could be equal to n, but this need not be the case. Each
range specification entry can be one of the following:
An ellipsis (...). Ellipses are used to imply zero or more dimensions of full-dimension selection and are produced using
ellipsis_mask. For example,foo[...]is the identity slice.A new axis. This is used to insert a new shape=1 dimension and is produced using
new_axis_mask. For example,foo[:, ...]wherefoois shape(3, 4)produces a(1, 3, 4)tensor.A range
begin:end:stride. This is used to specify how much to choose from a given dimension.stridecan be any integer but 0.beginis an integer which represents the index of the first value to select whileendrepresents the index of the last value to select. The number of values selected in each dimension isend - beginifstride > 0andbegin - endifstride < 0.beginandendcan be negative where-1is the last element,-2is the second to last.begin_maskcontrols whether to replace the explicitly givenbeginwith an implicit effective value of0ifstride > 0and-1ifstride < 0.end_maskis analogous but produces the number required to create the largest open interval. For example, given a shape(3,)tensorfoo[:], the effectivebeginandendare0and3. Do not assume this is equivalent tofoo[0:-1]which has an effectivebeginandendof0and2. Another example isfoo[-2::-1]which reverses the first dimension of a tensor while dropping the last two (in the original order elements). For examplefoo = [1,2,3,4]; foo[-2::-1]is[4,3].A single index. This is used to keep only elements that have a given index. For example (
foo[2, :]on a shape(5,6)tensor produces a shape(6,)tensor. This is encoded inbeginandendandshrink_axis_mask.
Each conceptual range specification is encoded in the op's argument. This
encoding is best understand by considering a non-trivial example. In
particular,
foo[1, 2:4, None, ..., :-3:-1, :] will be encoded as
begin = [1, 2, x, x, 0, x] # x denotes don't care (usually 0)
end = [2, 4, x, x, -3, x]
strides = [1, 1, x, x, -1, 1]
begin_mask = 1<<4 | 1<<5 = 48
end_mask = 1<<5 = 32
ellipsis_mask = 1<<3 = 8
new_axis_mask = 1<<2 = 4
shrink_axis_mask = 1<<0 = 1
In this case if foo.shape is (5, 5, 5, 5, 5, 5) the final shape of
the slice becomes (2, 1, 5, 5, 2, 5).
Let us walk step by step through each argument specification.
The first argument in the example slice is turned into
begin = 1andend = begin + 1 = 2. To disambiguate from the original spec2:4we also set the appropriate bit inshrink_axis_mask.2:4is contributes 2, 4, 1 to begin, end, and stride. All masks have zero bits contributed.None is a synonym for
tf.newaxis. This means insert a dimension of size 1 dimension in the final shape. Dummy values are contributed to begin, end and stride, while the new_axis_mask bit is set....grab the full ranges from as many dimensions as needed to fully specify a slice for every dimension of the input shape.:-3:-1shows the use of negative indices. A negative indexiassociated with a dimension that has shapesis converted to a positive indexs + i. So-1becomess-1(i.e. the last element). This conversion is done internally so begin, end and strides receive x, -3, and -1. The appropriate begin_mask bit is set to indicate the start range is the full range (ignoring the x).:indicates that the entire contents of the corresponding dimension is selected. This is equivalent to::or0::1. begin, end, and strides receive 0, 0, and 1, respectively. The appropriate bits inbegin_maskandend_maskare also set.
Requirements:
0 != strides[i] for i in [0, m)
ellipsis_mask must be a power of two (only one ellipsis)
Returns | |
|---|---|
A Tensor. Has the same type as input.
|