UNPKG

3.05 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17import { ENGINE } from '../engine';
18import { StridedSlice } from '../kernel_names';
19import { convertToTensor } from '../tensor_util_env';
20import { op } from './operation';
21/**
22 * Extracts a strided slice of a tensor.
23 *
24 * Roughly speaking, this op extracts a slice of size (end-begin)/stride from
25 * the given input tensor (x). Starting at the location specified by begin the
26 * slice continues by adding stride to the index until all dimensions are not
27 * less than end. Note that a stride can be negative, which causes a reverse
28 * slice.
29 *
30 * ```js
31 * const t = tf.tensor3d([1, 1, 1 ,2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6],
32 * [3, 2, 3]);
33 * t.stridedSlice([1, 0, 0], [2, 1, 3], [1, 1, 1]).print() // [[[3, 3, 3]]]
34 * t.stridedSlice([1, 0, 0], [2, 2, 3], [1, 1, 1]).print() // [[[3, 3, 3],
35 * // [4, 4, 4]]]
36 * t.stridedSlice([1, -1, 0], [2, -3, 3], [1, -1, 1]).print() // [[[4, 4, 4],
37 * // [3, 3, 3]]]
38 * ```
39 *
40 * @param x The tensor to stride slice.
41 * @param begin The coordinates to start the slice from.
42 * @param end: The coordinates to end the slice at.
43 * @param strides: The size of the slice.
44 * @param beginMask: If the ith bit of beginMask is set, begin[i] is ignored
45 * and the fullest possible range in that dimension is used instead.
46 * @param endMask: If the ith bit of endMask is set, end[i] is ignored
47 * and the fullest possible range in that dimension is used instead.
48 * @param shrinkAxisMask: a bitmask where bit i implies that
49 * the ith specification should shrink the dimensionality. begin and end must
50 * imply a slice of size 1 in the dimension.
51 *
52 * @doc {heading: 'Operations', subheading: 'Slicing and Joining'}
53 */
54function stridedSlice_(x, begin, end, strides, beginMask = 0, endMask = 0, ellipsisMask = 0, newAxisMask = 0, shrinkAxisMask = 0) {
55 const $x = convertToTensor(x, 'x', 'stridedSlice', 'string_or_numeric');
56 const inputs = { x: $x };
57 const attrs = {
58 begin,
59 end,
60 strides,
61 beginMask,
62 endMask,
63 ellipsisMask,
64 newAxisMask,
65 shrinkAxisMask
66 };
67 return ENGINE.runKernel(StridedSlice, inputs, attrs);
68}
69export const stridedSlice = op({ stridedSlice_ });
70//# sourceMappingURL=strided_slice.js.map
\No newline at end of file