diff --git a/tfjs-core/src/kernel_names.ts b/tfjs-core/src/kernel_names.ts index dfba6db5f6b..a58985e5028 100644 --- a/tfjs-core/src/kernel_names.ts +++ b/tfjs-core/src/kernel_names.ts @@ -45,6 +45,9 @@ export interface ConcatAttrs { export const Div = 'Div'; export type DivInputs = BinaryInputs; +export const Equal = 'Equal'; +export type EqualInputs = BinaryInputs; + export const FusedBatchNorm = 'FusedBatchNorm'; export type FusedBatchNormInputs = Pick; diff --git a/tfjs-core/src/ops/compare.ts b/tfjs-core/src/ops/compare.ts index 87f678756e6..ae927c22968 100644 --- a/tfjs-core/src/ops/compare.ts +++ b/tfjs-core/src/ops/compare.ts @@ -56,33 +56,6 @@ function lessStrict_(a: T|TensorLike, b: T|TensorLike): T { return $a.less($b); } -/** - * Returns the truth value of (a == b) element-wise. Supports broadcasting. - * - * We also expose `tf.equalStrict` which has the same signature as this op - * and asserts that `a` and `b` are the same shape (does not broadcast). - * - * ```js - * const a = tf.tensor1d([1, 2, 3]); - * const b = tf.tensor1d([2, 2, 2]); - * - * a.equal(b).print(); - * ``` - * - * @param a The first input tensor. - * @param b The second input tensor. Must have the same dtype as `a`. - */ -/** @doc {heading: 'Operations', subheading: 'Logical'} */ -function equal_( - a: Tensor|TensorLike, b: Tensor|TensorLike): T { - let $a = convertToTensor(a, 'a', 'equal'); - let $b = convertToTensor(b, 'b', 'equal'); - [$a, $b] = makeTypesMatch($a, $b); - assertAndGetBroadcastShape($a.shape, $b.shape); - - return ENGINE.runKernelFunc(backend => backend.equal($a, $b), {$a, $b}) as T; -} - function equalStrict_(a: T|TensorLike, b: T|TensorLike): T { const $a = convertToTensor(a, 'a', 'equalStrict'); const $b = convertToTensor(b, 'b', 'equalStrict'); @@ -179,7 +152,6 @@ function greaterEqualStrict_( return $a.greaterEqual($b); } -export const equal = op({equal_}); export const equalStrict = op({equalStrict_}); export const greaterEqual = op({greaterEqual_}); export const greaterEqualStrict = op({greaterEqualStrict_}); diff --git a/tfjs-core/src/ops/compare_ops_test.ts b/tfjs-core/src/ops/compare_ops_test.ts index 26676ed1e14..4f0b95973b3 100644 --- a/tfjs-core/src/ops/compare_ops_test.ts +++ b/tfjs-core/src/ops/compare_ops_test.ts @@ -17,273 +17,7 @@ import * as tf from '../index'; import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; -import {expectArraysClose, expectArraysEqual} from '../test_util'; - -describeWithFlags('equal', ALL_ENVS, () => { - it('Tensor1D - int32', async () => { - let a = tf.tensor1d([1, 4, 5], 'int32'); - let b = tf.tensor1d([2, 3, 5], 'int32'); - - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); - - a = tf.tensor1d([2, 2, 2], 'int32'); - b = tf.tensor1d([2, 2, 2], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1]); - - a = tf.tensor1d([0, 0], 'int32'); - b = tf.tensor1d([3, 3], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0]); - }); - it('Tensor1D - float32', async () => { - let a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); - let b = tf.tensor1d([2.2, 3.2, 5.1], 'float32'); - - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); - - a = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); - b = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1]); - - a = tf.tensor1d([0.45, 0.123], 'float32'); - b = tf.tensor1d([3.123, 3.321], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0]); - }); - - it('upcasts when dtypes dont match', async () => { - const a = [1.1, 4.1, 5]; - const b = [2.2, 3.2, 5]; - - let res = - tf.equal(tf.tensor(a, [3], 'float32'), tf.tensor(b, [3], 'int32')); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([3]); - expectArraysClose(await res.data(), [0, 0, 1]); - - res = tf.equal(tf.tensor(a, [3], 'int32'), tf.tensor(b, [3], 'bool')); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([3]); - expectArraysClose(await res.data(), [1, 0, 0]); - }); - - it('TensorLike', async () => { - const a = [1.1, 4.1, 5.1]; - const b = [2.2, 3.2, 5.1]; - - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); - }); - it('TensorLike chained', async () => { - const a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); - const b = [2.2, 3.2, 5.1]; - - expectArraysClose(await a.equal(b).data(), [0, 0, 1]); - }); - - it('mismatched Tensor1D shapes - int32', () => { - const a = tf.tensor1d([1, 2], 'int32'); - const b = tf.tensor1d([1, 2, 3], 'int32'); - const f = () => { - tf.equal(a, b); - }; - expect(f).toThrowError(); - }); - it('mismatched Tensor1D shapes - float32', () => { - const a = tf.tensor1d([1.1, 2.1], 'float32'); - const b = tf.tensor1d([1.1, 2.1, 3.1], 'float32'); - const f = () => { - tf.equal(a, b); - }; - expect(f).toThrowError(); - }); - it('NaNs in Tensor1D - float32', async () => { - const a = tf.tensor1d([1.1, NaN, 2.1], 'float32'); - const b = tf.tensor1d([2.1, 3.1, NaN], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0]); - }); - it('scalar and 1D broadcast', async () => { - const a = tf.scalar(2); - const b = tf.tensor1d([1, 2, 3, 4, 5, 2]); - const res = tf.equal(a, b); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([6]); - expectArraysEqual(await res.data(), [0, 1, 0, 0, 0, 1]); - }); - - // Tensor2D: - it('Tensor2D - int32', async () => { - let a = tf.tensor2d([[1, 4, 5], [8, 9, 12]], [2, 3], 'int32'); - let b = tf.tensor2d([[2, 3, 6], [7, 10, 11]], [2, 3], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 0]); - - a = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); - b = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); - }); - it('Tensor2D - float32', async () => { - let a = tf.tensor2d([[1.1, 4.1, 5.1], [8.1, 9.1, 12.1]], [2, 3], 'float32'); - let b = - tf.tensor2d([[2.1, 4.1, 5.1], [7.1, 10.1, 11.1]], [2, 3], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 1, 1, 0, 0, 0]); - - a = tf.tensor2d([[0.2, 0.2], [1.2, 1.2]], [2, 2], 'float32'); - b = tf.tensor2d([[0.2, 0.2], [1.2, 1.2]], [2, 2], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); - }); - it('broadcasting Tensor2D shapes - int32', async () => { - const a = tf.tensor2d([[3], [7]], [2, 1], 'int32'); - const b = tf.tensor2d([[2, 3, 4], [7, 8, 9]], [2, 3], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 1, 0, 1, 0, 0]); - }); - it('broadcasting Tensor2D shapes - float32', async () => { - const a = tf.tensor2d([[1.1], [7.1]], [2, 1], 'float32'); - const b = - tf.tensor2d([[0.1, 1.1, 2.1], [7.1, 8.1, 9.1]], [2, 3], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 1, 0, 1, 0, 0]); - }); - it('NaNs in Tensor2D - float32', async () => { - const a = tf.tensor2d([[1.1, NaN], [1.1, NaN]], [2, 2], 'float32'); - const b = tf.tensor2d([[0.1, NaN], [1.1, NaN]], [2, 2], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0]); - }); - it('2D and 2D broadcast each with 1 dim', async () => { - const a = tf.tensor2d([1, 2, 5], [1, 3]); - const b = tf.tensor2d([5, 1], [2, 1]); - const res = tf.equal(a, b); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([2, 3]); - expectArraysEqual(await res.data(), [0, 0, 1, 1, 0, 0]); - }); - it('2D and scalar broadcast', async () => { - const a = tf.tensor2d([1, 2, 3, 2, 5, 6], [2, 3]); - const b = tf.scalar(2); - const res = tf.equal(a, b); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([2, 3]); - expectArraysEqual(await res.data(), [0, 1, 0, 1, 0, 0]); - }); - - // Tensor3D: - it('Tensor3D - int32', async () => { - let a = - tf.tensor3d([[[1], [4], [5]], [[8], [9], [12]]], [2, 3, 1], 'int32'); - let b = - tf.tensor3d([[[2], [3], [6]], [[7], [10], [12]]], [2, 3, 1], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 1]); - - a = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'int32'); - b = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1, 1, 1]); - }); - it('Tensor3D - float32', async () => { - let a = tf.tensor3d( - [[[1.1], [4.1], [5.1]], [[8.1], [9.1], [12.1]]], [2, 3, 1], 'float32'); - let b = tf.tensor3d( - [[[2.1], [3.1], [6.1]], [[7.1], [10.1], [12.1]]], [2, 3, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 1]); - - a = tf.tensor3d( - [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); - b = tf.tensor3d( - [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1, 1, 1]); - }); - it('broadcasting Tensor3D shapes - int32', async () => { - const a = tf.tensor3d( - [[[1, 0], [2, 3], [4, 5]], [[6, 7], [9, 8], [10, 11]]], [2, 3, 2], - 'int32'); - const b = - tf.tensor3d([[[1], [2], [3]], [[7], [10], [9]]], [2, 3, 1], 'int32'); - expectArraysClose( - await tf.equal(a, b).data(), [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]); - }); - it('broadcasting Tensor3D shapes - float32', async () => { - const a = tf.tensor3d( - [ - [[1.1, 0.1], [2.1, 3.1], [4.1, 5.1]], - [[6.1, 7.1], [9.1, 8.1], [10.1, 11.1]] - ], - [2, 3, 2], 'float32'); - const b = tf.tensor3d( - [[[1.1], [2.1], [3.1]], [[7.1], [10.1], [9.1]]], [2, 3, 1], 'float32'); - expectArraysClose( - await tf.equal(a, b).data(), [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]); - }); - it('NaNs in Tensor3D - float32', async () => { - const a = tf.tensor3d( - [[[1.1], [NaN], [1.1]], [[0.1], [0.1], [0.1]]], [2, 3, 1], 'float32'); - const b = tf.tensor3d( - [[[0.1], [0.1], [1.1]], [[1.1], [0.1], [NaN]]], [2, 3, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0, 1, 0]); - }); - it('3D and scalar', async () => { - const a = tf.tensor3d([1, 2, 3, 4, 5, -1], [2, 3, 1]); - const b = tf.scalar(-1); - const res = tf.equal(a, b); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([2, 3, 1]); - expectArraysEqual(await res.data(), [0, 0, 0, 0, 0, 1]); - }); - - // Tensor4D: - it('Tensor4D - int32', async () => { - let a = tf.tensor4d([1, 4, 5, 8], [2, 2, 1, 1], 'int32'); - let b = tf.tensor4d([2, 3, 6, 8], [2, 2, 1, 1], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 1]); - - a = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); - b = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); - - a = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'int32'); - b = tf.tensor4d([2, 2, 2, 2], [2, 2, 1, 1], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0]); - }); - it('Tensor4D - float32', async () => { - let a = tf.tensor4d([1.1, 4.1, 5.1, 8.1], [2, 2, 1, 1], 'float32'); - let b = tf.tensor4d([2.1, 3.1, 6.1, 8.1], [2, 2, 1, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 1]); - - a = tf.tensor4d([0.1, 1.1, 2.2, 3.3], [2, 2, 1, 1], 'float32'); - b = tf.tensor4d([0.1, 1.1, 2.2, 3.3], [2, 2, 1, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); - - a = tf.tensor4d([0.1, 0.1, 0.1, 0.1], [2, 2, 1, 1], 'float32'); - b = tf.tensor4d([1.1, 1.1, 1.1, 1.1], [2, 2, 1, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0]); - }); - it('broadcasting Tensor4D shapes - int32', async () => { - const a = tf.tensor4d([1, 2, 5, 9], [2, 2, 1, 1], 'int32'); - const b = tf.tensor4d( - [[[[1, 2]], [[3, 4]]], [[[5, 6]], [[7, 8]]]], [2, 2, 1, 2], 'int32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 0, 0, 0, 1, 0, 0, 0]); - }); - it('broadcasting Tensor4D shapes - float32', async () => { - const a = tf.tensor4d([1.1, 2.1, 5.1, 9.1], [2, 2, 1, 1], 'float32'); - const b = tf.tensor4d( - [[[[1.1, 2.1]], [[3.1, 4.1]]], [[[5.1, 6.1]], [[7.1, 8.1]]]], - [2, 2, 1, 2], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [1, 0, 0, 0, 1, 0, 0, 0]); - }); - it('NaNs in Tensor4D - float32', async () => { - const a = tf.tensor4d([1.1, NaN, 1.1, 0.1], [2, 2, 1, 1], 'float32'); - const b = tf.tensor4d([0.1, 1.1, 1.1, NaN], [2, 2, 1, 1], 'float32'); - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0]); - }); - - it('throws when passed a as a non-tensor', () => { - expect(() => tf.equal({} as tf.Tensor, tf.scalar(1))) - .toThrowError(/Argument 'a' passed to 'equal' must be a Tensor/); - }); - it('throws when passed b as a non-tensor', () => { - expect(() => tf.equal(tf.scalar(1), {} as tf.Tensor)) - .toThrowError(/Argument 'b' passed to 'equal' must be a Tensor/); - }); - - it('accepts a tensor-like object', async () => { - const a = [1, 4, 5]; - const b = [2, 3, 5]; - expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); - }); -}); +import {expectArraysClose} from '../test_util'; describeWithFlags('equalStrict', ALL_ENVS, () => { it('Tensor1D - int32', async () => { diff --git a/tfjs-core/src/ops/equal.ts b/tfjs-core/src/ops/equal.ts new file mode 100644 index 00000000000..dcf0c54a4cd --- /dev/null +++ b/tfjs-core/src/ops/equal.ts @@ -0,0 +1,61 @@ +/** + * @license + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +import {ENGINE, ForwardFunc} from '../engine'; +import {Equal, EqualInputs} from '../kernel_names'; +import {Tensor} from '../tensor'; +import {NamedTensorMap} from '../tensor_types'; +import {makeTypesMatch} from '../tensor_util'; +import {convertToTensor} from '../tensor_util_env'; +import {TensorLike} from '../types'; + +import {assertAndGetBroadcastShape} from './broadcast_util'; +import {op} from './operation'; + +/** + * Returns the truth value of (a == b) element-wise. Supports broadcasting. + * + * We also expose `tf.equalStrict` which has the same signature as this op + * and asserts that `a` and `b` are the same shape (does not broadcast). + * + * ```js + * const a = tf.tensor1d([1, 2, 3]); + * const b = tf.tensor1d([2, 2, 2]); + * + * a.equal(b).print(); + * ``` + * + * @param a The first input tensor. + * @param b The second input tensor. Must have the same dtype as `a`. + */ +/** @doc {heading: 'Operations', subheading: 'Logical'} */ +function equal_( + a: Tensor|TensorLike, b: Tensor|TensorLike): T { + let $a = convertToTensor(a, 'a', 'equal'); + let $b = convertToTensor(b, 'b', 'equal'); + [$a, $b] = makeTypesMatch($a, $b); + + assertAndGetBroadcastShape($a.shape, $b.shape); + + const forward: ForwardFunc = backend => backend.equal($a, $b); + + const inputs: EqualInputs = {a: $a, b: $b}; + + return ENGINE.runKernelFunc( + forward, inputs as {} as NamedTensorMap, null, Equal) as T; +} + +export const equal = op({equal_}); diff --git a/tfjs-core/src/ops/equal_test.ts b/tfjs-core/src/ops/equal_test.ts new file mode 100644 index 00000000000..7ffac5d2396 --- /dev/null +++ b/tfjs-core/src/ops/equal_test.ts @@ -0,0 +1,285 @@ +/** + * @license + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +import * as tf from '../index'; +import {ALL_ENVS, describeWithFlags} from '../jasmine_util'; +import {expectArraysClose, expectArraysEqual} from '../test_util'; + +describeWithFlags('equal', ALL_ENVS, () => { + it('Tensor1D - int32', async () => { + let a = tf.tensor1d([1, 4, 5], 'int32'); + let b = tf.tensor1d([2, 3, 5], 'int32'); + + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); + + a = tf.tensor1d([2, 2, 2], 'int32'); + b = tf.tensor1d([2, 2, 2], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1]); + + a = tf.tensor1d([0, 0], 'int32'); + b = tf.tensor1d([3, 3], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0]); + }); + it('Tensor1D - float32', async () => { + let a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); + let b = tf.tensor1d([2.2, 3.2, 5.1], 'float32'); + + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); + + a = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + b = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1]); + + a = tf.tensor1d([0.45, 0.123], 'float32'); + b = tf.tensor1d([3.123, 3.321], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0]); + }); + + it('upcasts when dtypes dont match', async () => { + const a = [1.1, 4.1, 5]; + const b = [2.2, 3.2, 5]; + + let res = + tf.equal(tf.tensor(a, [3], 'float32'), tf.tensor(b, [3], 'int32')); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([3]); + expectArraysClose(await res.data(), [0, 0, 1]); + + res = tf.equal(tf.tensor(a, [3], 'int32'), tf.tensor(b, [3], 'bool')); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([3]); + expectArraysClose(await res.data(), [1, 0, 0]); + }); + + it('TensorLike', async () => { + const a = [1.1, 4.1, 5.1]; + const b = [2.2, 3.2, 5.1]; + + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); + }); + it('TensorLike chained', async () => { + const a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); + const b = [2.2, 3.2, 5.1]; + + expectArraysClose(await a.equal(b).data(), [0, 0, 1]); + }); + + it('mismatched Tensor1D shapes - int32', () => { + const a = tf.tensor1d([1, 2], 'int32'); + const b = tf.tensor1d([1, 2, 3], 'int32'); + const f = () => { + tf.equal(a, b); + }; + expect(f).toThrowError(); + }); + it('mismatched Tensor1D shapes - float32', () => { + const a = tf.tensor1d([1.1, 2.1], 'float32'); + const b = tf.tensor1d([1.1, 2.1, 3.1], 'float32'); + const f = () => { + tf.equal(a, b); + }; + expect(f).toThrowError(); + }); + it('NaNs in Tensor1D - float32', async () => { + const a = tf.tensor1d([1.1, NaN, 2.1], 'float32'); + const b = tf.tensor1d([2.1, 3.1, NaN], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0]); + }); + it('scalar and 1D broadcast', async () => { + const a = tf.scalar(2); + const b = tf.tensor1d([1, 2, 3, 4, 5, 2]); + const res = tf.equal(a, b); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([6]); + expectArraysEqual(await res.data(), [0, 1, 0, 0, 0, 1]); + }); + + // Tensor2D: + it('Tensor2D - int32', async () => { + let a = tf.tensor2d([[1, 4, 5], [8, 9, 12]], [2, 3], 'int32'); + let b = tf.tensor2d([[2, 3, 6], [7, 10, 11]], [2, 3], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 0]); + + a = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + b = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); + }); + it('Tensor2D - float32', async () => { + let a = tf.tensor2d([[1.1, 4.1, 5.1], [8.1, 9.1, 12.1]], [2, 3], 'float32'); + let b = + tf.tensor2d([[2.1, 4.1, 5.1], [7.1, 10.1, 11.1]], [2, 3], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 1, 1, 0, 0, 0]); + + a = tf.tensor2d([[0.2, 0.2], [1.2, 1.2]], [2, 2], 'float32'); + b = tf.tensor2d([[0.2, 0.2], [1.2, 1.2]], [2, 2], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); + }); + it('broadcasting Tensor2D shapes - int32', async () => { + const a = tf.tensor2d([[3], [7]], [2, 1], 'int32'); + const b = tf.tensor2d([[2, 3, 4], [7, 8, 9]], [2, 3], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 1, 0, 1, 0, 0]); + }); + it('broadcasting Tensor2D shapes - float32', async () => { + const a = tf.tensor2d([[1.1], [7.1]], [2, 1], 'float32'); + const b = + tf.tensor2d([[0.1, 1.1, 2.1], [7.1, 8.1, 9.1]], [2, 3], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 1, 0, 1, 0, 0]); + }); + it('NaNs in Tensor2D - float32', async () => { + const a = tf.tensor2d([[1.1, NaN], [1.1, NaN]], [2, 2], 'float32'); + const b = tf.tensor2d([[0.1, NaN], [1.1, NaN]], [2, 2], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0]); + }); + it('2D and 2D broadcast each with 1 dim', async () => { + const a = tf.tensor2d([1, 2, 5], [1, 3]); + const b = tf.tensor2d([5, 1], [2, 1]); + const res = tf.equal(a, b); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([2, 3]); + expectArraysEqual(await res.data(), [0, 0, 1, 1, 0, 0]); + }); + it('2D and scalar broadcast', async () => { + const a = tf.tensor2d([1, 2, 3, 2, 5, 6], [2, 3]); + const b = tf.scalar(2); + const res = tf.equal(a, b); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([2, 3]); + expectArraysEqual(await res.data(), [0, 1, 0, 1, 0, 0]); + }); + + // Tensor3D: + it('Tensor3D - int32', async () => { + let a = + tf.tensor3d([[[1], [4], [5]], [[8], [9], [12]]], [2, 3, 1], 'int32'); + let b = + tf.tensor3d([[[2], [3], [6]], [[7], [10], [12]]], [2, 3, 1], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 1]); + + a = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'int32'); + b = tf.tensor3d([[[0], [0], [0]], [[1], [1], [1]]], [2, 3, 1], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1, 1, 1]); + }); + it('Tensor3D - float32', async () => { + let a = tf.tensor3d( + [[[1.1], [4.1], [5.1]], [[8.1], [9.1], [12.1]]], [2, 3, 1], 'float32'); + let b = tf.tensor3d( + [[[2.1], [3.1], [6.1]], [[7.1], [10.1], [12.1]]], [2, 3, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0, 0, 1]); + + a = tf.tensor3d( + [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); + b = tf.tensor3d( + [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1, 1, 1]); + }); + it('broadcasting Tensor3D shapes - int32', async () => { + const a = tf.tensor3d( + [[[1, 0], [2, 3], [4, 5]], [[6, 7], [9, 8], [10, 11]]], [2, 3, 2], + 'int32'); + const b = + tf.tensor3d([[[1], [2], [3]], [[7], [10], [9]]], [2, 3, 1], 'int32'); + expectArraysClose( + await tf.equal(a, b).data(), [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]); + }); + it('broadcasting Tensor3D shapes - float32', async () => { + const a = tf.tensor3d( + [ + [[1.1, 0.1], [2.1, 3.1], [4.1, 5.1]], + [[6.1, 7.1], [9.1, 8.1], [10.1, 11.1]] + ], + [2, 3, 2], 'float32'); + const b = tf.tensor3d( + [[[1.1], [2.1], [3.1]], [[7.1], [10.1], [9.1]]], [2, 3, 1], 'float32'); + expectArraysClose( + await tf.equal(a, b).data(), [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0]); + }); + it('NaNs in Tensor3D - float32', async () => { + const a = tf.tensor3d( + [[[1.1], [NaN], [1.1]], [[0.1], [0.1], [0.1]]], [2, 3, 1], 'float32'); + const b = tf.tensor3d( + [[[0.1], [0.1], [1.1]], [[1.1], [0.1], [NaN]]], [2, 3, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0, 1, 0]); + }); + it('3D and scalar', async () => { + const a = tf.tensor3d([1, 2, 3, 4, 5, -1], [2, 3, 1]); + const b = tf.scalar(-1); + const res = tf.equal(a, b); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([2, 3, 1]); + expectArraysEqual(await res.data(), [0, 0, 0, 0, 0, 1]); + }); + + // Tensor4D: + it('Tensor4D - int32', async () => { + let a = tf.tensor4d([1, 4, 5, 8], [2, 2, 1, 1], 'int32'); + let b = tf.tensor4d([2, 3, 6, 8], [2, 2, 1, 1], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 1]); + + a = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); + b = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); + + a = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'int32'); + b = tf.tensor4d([2, 2, 2, 2], [2, 2, 1, 1], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0]); + }); + it('Tensor4D - float32', async () => { + let a = tf.tensor4d([1.1, 4.1, 5.1, 8.1], [2, 2, 1, 1], 'float32'); + let b = tf.tensor4d([2.1, 3.1, 6.1, 8.1], [2, 2, 1, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 1]); + + a = tf.tensor4d([0.1, 1.1, 2.2, 3.3], [2, 2, 1, 1], 'float32'); + b = tf.tensor4d([0.1, 1.1, 2.2, 3.3], [2, 2, 1, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 1, 1, 1]); + + a = tf.tensor4d([0.1, 0.1, 0.1, 0.1], [2, 2, 1, 1], 'float32'); + b = tf.tensor4d([1.1, 1.1, 1.1, 1.1], [2, 2, 1, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 0, 0]); + }); + it('broadcasting Tensor4D shapes - int32', async () => { + const a = tf.tensor4d([1, 2, 5, 9], [2, 2, 1, 1], 'int32'); + const b = tf.tensor4d( + [[[[1, 2]], [[3, 4]]], [[[5, 6]], [[7, 8]]]], [2, 2, 1, 2], 'int32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 0, 0, 0, 1, 0, 0, 0]); + }); + it('broadcasting Tensor4D shapes - float32', async () => { + const a = tf.tensor4d([1.1, 2.1, 5.1, 9.1], [2, 2, 1, 1], 'float32'); + const b = tf.tensor4d( + [[[[1.1, 2.1]], [[3.1, 4.1]]], [[[5.1, 6.1]], [[7.1, 8.1]]]], + [2, 2, 1, 2], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [1, 0, 0, 0, 1, 0, 0, 0]); + }); + it('NaNs in Tensor4D - float32', async () => { + const a = tf.tensor4d([1.1, NaN, 1.1, 0.1], [2, 2, 1, 1], 'float32'); + const b = tf.tensor4d([0.1, 1.1, 1.1, NaN], [2, 2, 1, 1], 'float32'); + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1, 0]); + }); + + it('throws when passed a as a non-tensor', () => { + expect(() => tf.equal({} as tf.Tensor, tf.scalar(1))) + .toThrowError(/Argument 'a' passed to 'equal' must be a Tensor/); + }); + it('throws when passed b as a non-tensor', () => { + expect(() => tf.equal(tf.scalar(1), {} as tf.Tensor)) + .toThrowError(/Argument 'b' passed to 'equal' must be a Tensor/); + }); + + it('accepts a tensor-like object', async () => { + const a = [1, 4, 5]; + const b = [2, 3, 5]; + expectArraysClose(await tf.equal(a, b).data(), [0, 0, 1]); + }); +}); diff --git a/tfjs-core/src/ops/ops.ts b/tfjs-core/src/ops/ops.ts index a1f150c2c2d..282a8c17fd6 100644 --- a/tfjs-core/src/ops/ops.ts +++ b/tfjs-core/src/ops/ops.ts @@ -31,6 +31,7 @@ export {concat3d} from './concat_3d'; export {concat4d} from './concat_4d'; export {div} from './div'; export {divNoNan} from './div_no_nan'; +export {equal} from './equal'; export {eye} from './eye'; export {greater} from './greater'; export {less} from './less'; diff --git a/tfjs-core/src/public/chained_ops/equal.ts b/tfjs-core/src/public/chained_ops/equal.ts new file mode 100644 index 00000000000..1534c1c3f26 --- /dev/null +++ b/tfjs-core/src/public/chained_ops/equal.ts @@ -0,0 +1,30 @@ +/** + * @license + * Copyright 2020 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +import {equal} from '../../ops/equal'; +import {Tensor} from '../../tensor'; +import {Rank, TensorLike} from '../../types'; + +declare module '../../tensor' { + interface Tensor { + equal(b: Tensor|TensorLike): T; + } +} + +Tensor.prototype.equal = function(b: Tensor|TensorLike): T { + this.throwIfDisposed(); + return equal(this, b); +}; diff --git a/tfjs-core/src/public/chained_ops/register_all_chained_ops.ts b/tfjs-core/src/public/chained_ops/register_all_chained_ops.ts index ff6852b6b82..ba3d0691b1d 100644 --- a/tfjs-core/src/public/chained_ops/register_all_chained_ops.ts +++ b/tfjs-core/src/public/chained_ops/register_all_chained_ops.ts @@ -20,6 +20,7 @@ import './broadcast_to'; import './concat'; import './div'; import './div_no_nan'; +import './equal'; import './greater'; import './less'; import './one_hot'; diff --git a/tfjs-core/src/public/chained_ops/register_all_chained_ops_test.ts b/tfjs-core/src/public/chained_ops/register_all_chained_ops_test.ts index 597b052fd1a..1aadb870969 100644 --- a/tfjs-core/src/public/chained_ops/register_all_chained_ops_test.ts +++ b/tfjs-core/src/public/chained_ops/register_all_chained_ops_test.ts @@ -25,8 +25,8 @@ import {ALL_ENVS, describeWithFlags} from '../../jasmine_util'; const CHAINED_OPS = [ 'add', 'batchNorm', 'broadcastTo', 'concat', 'div', 'divNoNan', 'greater', - 'less', 'notEqual', 'oneHot', 'pad', 'split', 'square', 'sub', 'tile', - 'transpose' + 'less', 'equal', 'notEqual', 'oneHot', 'pad', 'split', 'square', 'sub', + 'tile', 'transpose' ]; describeWithFlags('chained ops', ALL_ENVS, () => { diff --git a/tfjs-core/src/tensor.ts b/tfjs-core/src/tensor.ts index 422f9699cab..4ee16a46fd7 100644 --- a/tfjs-core/src/tensor.ts +++ b/tfjs-core/src/tensor.ts @@ -229,7 +229,6 @@ export interface OpHandler { T; notEqualStrict(a: T, b: T|TensorLike): T; lessStrict(a: T, b: T|TensorLike): T; - equal(a: Tensor, b: Tensor|TensorLike): T; equalStrict(a: T, b: T|TensorLike): T; lessEqual(a: Tensor, b: Tensor|TensorLike): T; lessEqualStrict(a: T, b: T|TensorLike): T; @@ -931,10 +930,6 @@ export class Tensor { this.throwIfDisposed(); return opHandler.lessStrict(this, x); } - equal(x: Tensor|TensorLike): T { - this.throwIfDisposed(); - return opHandler.equal(this, x); - } equalStrict(this: T, x: T|TensorLike): T { this.throwIfDisposed(); return opHandler.equalStrict(this, x); diff --git a/tfjs-core/src/tests.ts b/tfjs-core/src/tests.ts index 7bbb6e54cd5..222b8687bd1 100644 --- a/tfjs-core/src/tests.ts +++ b/tfjs-core/src/tests.ts @@ -65,6 +65,7 @@ import './ops/conv_util_test'; import './ops/diag_test'; import './ops/dropout_test'; import './ops/dropout_util_test'; +import './ops/equal_test'; import './ops/eye_test'; import './ops/fused_test'; import './ops/gather_nd_test';