1 | /**
|
2 | * @license
|
3 | * Copyright 2020 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 | */
|
17 | import { ENGINE } from '../engine';
|
18 | import { Select } from '../kernel_names';
|
19 | import { convertToTensor } from '../tensor_util_env';
|
20 | import { broadcastTo } from './broadcast_to';
|
21 | import { assertAndGetBroadcastShape } from './broadcast_util';
|
22 | import { op } from './operation';
|
23 | /**
|
24 | * Returns the elements, either `a` or `b` depending on the `condition`.
|
25 | *
|
26 | * If the condition is true, select from `a`, otherwise select from `b`.
|
27 | *
|
28 | * ```js
|
29 | * const cond = tf.tensor1d([false, false, true], 'bool');
|
30 | * const a = tf.tensor1d([1 , 2, 3]);
|
31 | * const b = tf.tensor1d([-1, -2, -3]);
|
32 | *
|
33 | * a.where(cond, b).print();
|
34 | * ```
|
35 | *
|
36 | * @param condition The input condition. Must be of dtype bool.
|
37 | * @param a If `condition` is rank 1, `a` may have a higher rank but
|
38 | * its first dimension must match the size of `condition`.
|
39 | * @param b A tensor with the same dtype as `a` and with shape that is
|
40 | * compatible with `a`.
|
41 | * @return A tensor with same dtype as `a` and `b`, and shape that is
|
42 | * broadcastable from `a` and `b`.
|
43 | *
|
44 | * @doc {heading: 'Operations', subheading: 'Logical'}
|
45 | */
|
46 | function where_(condition, a, b) {
|
47 | const $a = convertToTensor(a, 'a', 'where');
|
48 | const $b = convertToTensor(b, 'b', 'where');
|
49 | const $condition = convertToTensor(condition, 'condition', 'where', 'bool');
|
50 | // TODO: move this logic to forward function when the broadcastTo op is
|
51 | // implemented in WASM.
|
52 | // Find the broadcastable shape for $condition, $a, and $b.
|
53 | const broadcastShape = assertAndGetBroadcastShape(assertAndGetBroadcastShape($condition.shape, $a.shape), $b.shape);
|
54 | const $broadcastedCondition = broadcastTo($condition, broadcastShape);
|
55 | const $broadcastedA = broadcastTo($a, broadcastShape);
|
56 | const $broadcastedB = broadcastTo($b, broadcastShape);
|
57 | const inputs = {
|
58 | condition: $broadcastedCondition,
|
59 | t: $broadcastedA,
|
60 | e: $broadcastedB
|
61 | };
|
62 | return ENGINE.runKernel(Select, inputs);
|
63 | }
|
64 | export const where = op({ where_ });
|
65 | //# sourceMappingURL=where.js.map |
\ | No newline at end of file |