UNPKG

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