UNPKG

1.95 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 { Cumsum } from '../kernel_names';
19import { convertToTensor } from '../tensor_util_env';
20import { op } from './operation';
21/**
22 * Computes the cumulative sum of a `tf.Tensor` along `axis`.
23 *
24 * ```js
25 * const x = tf.tensor([1, 2, 3, 4]);
26 * x.cumsum().print();
27 * ```
28 * ```js
29 * const x = tf.tensor([[1, 2], [3, 4]]);
30 * x.cumsum().print();
31 * ```
32 *
33 * @param x The input tensor to be summed.
34 * @param axis The axis along which to sum. Optional. Defaults to 0.
35 * @param exclusive Whether to perform exclusive cumulative sum. Optional.
36 * Defaults to false. If set to true then the sum of each tensor entry
37 * does not include its own value, but only the values previous to it
38 * along the specified axis.
39 * @param reverse Whether to sum in the opposite direction. Optional.
40 * Defaults to false.
41 *
42 * @doc {heading: 'Operations', subheading: 'Scan'}
43 */
44function cumsum_(x, axis = 0, exclusive = false, reverse = false) {
45 const $x = convertToTensor(x, 'x', 'cumsum');
46 const inputs = { x: $x };
47 const attrs = { axis, exclusive, reverse };
48 return ENGINE.runKernel(Cumsum, inputs, attrs);
49}
50export const cumsum = op({ cumsum_ });
51//# sourceMappingURL=cumsum.js.map
\No newline at end of file