UNPKG

5.32 kBJavaScriptView Raw
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 */
17import * as tf from '../index';
18import { ALL_ENVS, describeWithFlags } from '../jasmine_util';
19import { expectArraysClose } from '../test_util';
20describeWithFlags('logicalAnd', ALL_ENVS, () => {
21 it('Tensor1D.', async () => {
22 let a = tf.tensor1d([1, 0, 0], 'bool');
23 let b = tf.tensor1d([0, 1, 0], 'bool');
24 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0]);
25 a = tf.tensor1d([0, 0, 0], 'bool');
26 b = tf.tensor1d([0, 0, 0], 'bool');
27 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0]);
28 a = tf.tensor1d([1, 1], 'bool');
29 b = tf.tensor1d([1, 1], 'bool');
30 expectArraysClose(await tf.logicalAnd(a, b).data(), [1, 1]);
31 });
32 it('mismatched Tensor1D shapes', () => {
33 const a = tf.tensor1d([1, 0], 'bool');
34 const b = tf.tensor1d([0, 1, 0], 'bool');
35 const f = () => {
36 tf.logicalAnd(a, b);
37 };
38 expect(f).toThrowError();
39 });
40 it('Tensor2D', async () => {
41 let a = tf.tensor2d([[1, 0, 1], [0, 0, 0]], [2, 3], 'bool');
42 let b = tf.tensor2d([[0, 0, 0], [0, 1, 0]], [2, 3], 'bool');
43 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 0, 0, 0]);
44 a = tf.tensor2d([[0, 0, 0], [1, 1, 1]], [2, 3], 'bool');
45 b = tf.tensor2d([[0, 0, 0], [1, 1, 1]], [2, 3], 'bool');
46 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 1, 1, 1]);
47 });
48 it('broadcasting Tensor2D shapes', async () => {
49 const a = tf.tensor2d([[1], [0]], [2, 1], 'bool');
50 const b = tf.tensor2d([[0, 1, 0], [0, 1, 0]], [2, 3], 'bool');
51 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 1, 0, 0, 0, 0]);
52 });
53 it('Tensor3D', async () => {
54 let a = tf.tensor3d([[[1], [0], [1]], [[0], [0], [1]]], [2, 3, 1], 'bool');
55 let b = tf.tensor3d([[[0], [0], [1]], [[1], [0], [0]]], [2, 3, 1], 'bool');
56 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 1, 0, 0, 0]);
57 a = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'bool');
58 b = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'bool');
59 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 1, 1, 1]);
60 });
61 it('broadcasting Tensor3D shapes', async () => {
62 const a = tf.tensor3d([[[1, 0], [0, 0], [1, 1]], [[0, 0], [0, 1], [0, 0]]], [2, 3, 2], 'bool');
63 const b = tf.tensor3d([[[0], [0], [1]], [[1], [0], [0]]], [2, 3, 1], 'bool');
64 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]);
65 });
66 it('Tensor4D', async () => {
67 let a = tf.tensor4d([1, 0, 1, 0], [2, 2, 1, 1], 'bool');
68 let b = tf.tensor4d([0, 1, 1, 0], [2, 2, 1, 1], 'bool');
69 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 1, 0]);
70 a = tf.tensor4d([0, 0, 0, 0], [2, 2, 1, 1], 'bool');
71 b = tf.tensor4d([0, 0, 0, 0], [2, 2, 1, 1], 'bool');
72 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 0]);
73 a = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'bool');
74 b = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'bool');
75 expectArraysClose(await tf.logicalAnd(a, b).data(), [1, 1, 1, 1]);
76 });
77 it('broadcasting Tensor4D shapes', async () => {
78 const a = tf.tensor4d([1, 0, 1, 0], [2, 2, 1, 1], 'bool');
79 const b = tf.tensor4d([[[[1, 0]], [[0, 0]]], [[[0, 0]], [[1, 1]]]], [2, 2, 1, 2], 'bool');
80 expectArraysClose(await tf.logicalAnd(a, b).data(), [1, 0, 0, 0, 0, 0, 0, 0]);
81 });
82 it('TensorLike', async () => {
83 const a = [true, false, false];
84 const b = [false, true, false];
85 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0]);
86 });
87 it('TensorLike Chained', async () => {
88 const a = tf.tensor1d([1, 0, 0], 'bool');
89 const b = [false, true, false];
90 expectArraysClose(await a.logicalAnd(b).data(), [0, 0, 0]);
91 });
92 it('throws when passed a as a non-tensor', () => {
93 expect(() => tf.logicalAnd({}, tf.scalar(1, 'bool')))
94 .toThrowError(/Argument 'a' passed to 'logicalAnd' must be a Tensor/);
95 });
96 it('throws when passed b as a non-tensor', () => {
97 expect(() => tf.logicalAnd(tf.scalar(1, 'bool'), {}))
98 .toThrowError(/Argument 'b' passed to 'logicalAnd' must be a Tensor/);
99 });
100 it('accepts a tensor-like object', async () => {
101 const a = [1, 0, 0, 1];
102 const b = [0, 1, 0, 1];
103 expectArraysClose(await tf.logicalAnd(a, b).data(), [0, 0, 0, 1]);
104 });
105});
106//# sourceMappingURL=logical_and_test.js.map
\No newline at end of file