From e193b9d65d77905dc6bb7d1a0659060ba9db7391 Mon Sep 17 00:00:00 2001 From: Na Li Date: Thu, 30 Apr 2020 14:24:01 -0700 Subject: [PATCH] Modularize greater. --- tfjs-core/src/kernel_names.ts | 3 + tfjs-core/src/ops/compare.ts | 30 -- tfjs-core/src/ops/compare_ops_test.ts | 335 ----------------- tfjs-core/src/ops/greater.ts | 62 +++ tfjs-core/src/ops/greater_test.ts | 355 ++++++++++++++++++ tfjs-core/src/ops/ops.ts | 1 + tfjs-core/src/public/chained_ops/greater.ts | 30 ++ .../chained_ops/register_all_chained_ops.ts | 1 + .../register_all_chained_ops_test.ts | 4 +- tfjs-core/src/tensor.ts | 5 - tfjs-core/src/tests.ts | 1 + 11 files changed, 455 insertions(+), 372 deletions(-) create mode 100644 tfjs-core/src/ops/greater.ts create mode 100644 tfjs-core/src/ops/greater_test.ts create mode 100644 tfjs-core/src/public/chained_ops/greater.ts diff --git a/tfjs-core/src/kernel_names.ts b/tfjs-core/src/kernel_names.ts index 656eca8b085..f08886a0b95 100644 --- a/tfjs-core/src/kernel_names.ts +++ b/tfjs-core/src/kernel_names.ts @@ -52,6 +52,9 @@ export interface FusedBatchNormAttrs { varianceEpsilon: number; } +export const Greater = 'Greater'; +export type GreaterInputs = BinaryInputs; + export const Identity = 'Identity'; export type IdentityInputs = Pick; diff --git a/tfjs-core/src/ops/compare.ts b/tfjs-core/src/ops/compare.ts index 974609234ed..6a89bee3f39 100644 --- a/tfjs-core/src/ops/compare.ts +++ b/tfjs-core/src/ops/compare.ts @@ -157,35 +157,6 @@ function lessEqualStrict_( return $a.lessEqual($b); } -/** - * Returns the truth value of (a > b) element-wise. Supports broadcasting. - * - * We also expose `tf.greaterStrict` 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.greater(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 greater_( - a: Tensor|TensorLike, b: Tensor|TensorLike): T { - let $a = convertToTensor(a, 'a', 'greater'); - let $b = convertToTensor(b, 'b', 'greater'); - [$a, $b] = makeTypesMatch($a, $b); - assertAndGetBroadcastShape($a.shape, $b.shape); - - return ENGINE.runKernelFunc( - backend => backend.greater($a, $b), {a: $a, b: $b}, - null /* grad */, 'Greater') as T; -} - function greaterStrict_(a: T|TensorLike, b: T|TensorLike): T { const $a = convertToTensor(a, 'a', 'greaterStrict'); const $b = convertToTensor(b, 'b', 'greaterStrict'); @@ -238,7 +209,6 @@ function greaterEqualStrict_( export const equal = op({equal_}); export const equalStrict = op({equalStrict_}); -export const greater = op({greater_}); export const greaterEqual = op({greaterEqual_}); export const greaterEqualStrict = op({greaterEqualStrict_}); export const greaterStrict = op({greaterStrict_}); diff --git a/tfjs-core/src/ops/compare_ops_test.ts b/tfjs-core/src/ops/compare_ops_test.ts index 35678c90e4b..7a82c774ce1 100644 --- a/tfjs-core/src/ops/compare_ops_test.ts +++ b/tfjs-core/src/ops/compare_ops_test.ts @@ -1491,341 +1491,6 @@ describeWithFlags('lessEqualStrict', ALL_ENVS, () => { }); }); -describeWithFlags('greater', ALL_ENVS, () => { - it('Tensor1D - int32', async () => { - let a = tf.tensor1d([1, 4, 5], 'int32'); - let b = tf.tensor1d([2, 3, 5], 'int32'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0]); - - a = tf.tensor1d([2, 2, 2], 'int32'); - b = tf.tensor1d([2, 2, 2], 'int32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0]); - - a = tf.tensor1d([3, 3], 'int32'); - b = tf.tensor1d([0, 0], 'int32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 1]); - }); - 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'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0]); - - a = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); - b = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0]); - - a = tf.tensor1d([3.123, 3.321], 'float32'); - b = tf.tensor1d([0.45, 0.123], 'float32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 1]); - }); - - it('upcasts when dtypes dont match', async () => { - const a = [1.1, 4.1, 5.2]; - const b = [2.2, 3.2, 5.1]; - - let res = - tf.greater(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, 1, 1]); - - res = tf.greater(tf.tensor(a, [3], 'int32'), tf.tensor(b, [3], 'bool')); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([3]); - expectArraysClose(await res.data(), [0, 1, 1]); - }); - - it('TensorLike', async () => { - const a = [1.1, 4.1, 5.1]; - const b = [2.2, 3.2, 5.1]; - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0]); - }); - it('TensorLike Chained', async () => { - const a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); - const b = [2.2, 3.2, 5.1]; - const res = a.greater(b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0]); - }); - it('mismatched Tensor1D shapes - int32', () => { - const a = tf.tensor1d([1, 2], 'int32'); - const b = tf.tensor1d([1, 2, 3], 'int32'); - const f = () => { - tf.greater(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.greater(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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0]); - }); - - // Tensor2D: - it('Tensor2D - int32', async () => { - let a = tf.tensor2d([[1, 4, 5], [8, 9, 11]], [2, 3], 'int32'); - let b = tf.tensor2d([[2, 3, 6], [7, 10, 11]], [2, 3], 'int32'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); - - a = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); - b = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0]); - }); - it('Tensor2D - float32', async () => { - let a = tf.tensor2d([[1.1, 4.1, 5.1], [8.1, 9.1, 11.1]], [2, 3], 'float32'); - let b = - tf.tensor2d([[2.1, 3.1, 6.1], [7.1, 10.1, 11.1]], [2, 3], 'float32'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 1, 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'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0]); - }); - 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 0, 0, 0, 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 0, 0, 0, 0, 0]); - }); - it('NaNs in Tensor2D - float32', async () => { - const a = tf.tensor2d([[1.1, NaN], [0.1, NaN]], [2, 2], 'float32'); - const b = tf.tensor2d([[0.1, NaN], [1.1, NaN]], [2, 2], 'float32'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 0, 0, 0]); - }); - - // Tensor3D: - it('Tensor3D - int32', async () => { - let a = - tf.tensor3d([[[1], [4], [5]], [[8], [9], [11]]], [2, 3, 1], 'int32'); - let b = - tf.tensor3d([[[2], [3], [6]], [[7], [10], [11]]], [2, 3, 1], 'int32'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); - - 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'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0]); - }); - it('Tensor3D - float32', async () => { - let a = tf.tensor3d( - [[[1.1], [4.1], [5.1]], [[8.1], [9.1], [11.1]]], [2, 3, 1], 'float32'); - let b = tf.tensor3d( - [[[2.1], [3.1], [6.1]], [[7.1], [10.1], [11.1]]], [2, 3, 1], 'float32'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); - - a = tf.tensor3d( - [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.2]]], [2, 3, 1], 'float32'); - b = tf.tensor3d( - [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1]); - }); - 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1]); - }); - 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 0, 0, 0, 0, 0]); - }); - - // 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'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 0]); - - a = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); - b = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0]); - - a = tf.tensor4d([2, 2, 2, 2], [2, 2, 1, 1], 'int32'); - b = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'int32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 1, 1, 1]); - }); - 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'); - let res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0, 0]); - - 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'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0]); - - a = tf.tensor4d([1.1, 1.1, 1.1, 1.1], [2, 2, 1, 1], 'float32'); - b = tf.tensor4d([0.1, 0.1, 0.1, 0.1], [2, 2, 1, 1], 'float32'); - res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 1, 1, 1]); - }); - 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0, 1, 1]); - }); - 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'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0, 1, 1]); - }); - it('NaNs in Tensor4D - float32', async () => { - const a = tf.tensor4d([1.1, NaN, 0.1, 0.1], [2, 2, 1, 1], 'float32'); - const b = tf.tensor4d([0.1, 1.1, 1.1, NaN], [2, 2, 1, 1], 'float32'); - const res = tf.greater(a, b); - - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [1, 0, 0, 0]); - }); - - it('throws when passed a as a non-tensor', () => { - expect(() => tf.greater({} as tf.Tensor, tf.scalar(1))) - .toThrowError(/Argument 'a' passed to 'greater' must be a Tensor/); - }); - it('throws when passed b as a non-tensor', () => { - expect(() => tf.greater(tf.scalar(1), {} as tf.Tensor)) - .toThrowError(/Argument 'b' passed to 'greater' must be a Tensor/); - }); - - it('accepts a tensor-like object', async () => { - const a = [1, 4, 5]; - const b = [2, 3, 5]; - - const res = tf.greater(a, b); - expect(res.dtype).toBe('bool'); - expectArraysClose(await res.data(), [0, 1, 0]); - }); - - it('works with 0 sized tensors', async () => { - const a = tf.tensor2d([], [0, 5]); - const b = tf.tensor1d([1, 2, 3, 4, 5]); - const res = tf.greater(a, b); - expect(res.dtype).toBe('bool'); - expect(res.shape).toEqual([0, 5]); - expectArraysClose(await res.data(), []); - }); -}); - describeWithFlags('greaterStrict', ALL_ENVS, () => { it('Tensor1D - strict version throws when a and b are different shape', () => { diff --git a/tfjs-core/src/ops/greater.ts b/tfjs-core/src/ops/greater.ts new file mode 100644 index 00000000000..c6e19ba5287 --- /dev/null +++ b/tfjs-core/src/ops/greater.ts @@ -0,0 +1,62 @@ +/** + * @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 {Greater, GreaterInputs} 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.greaterStrict` 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.greater(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 greater_( + a: Tensor|TensorLike, b: Tensor|TensorLike): T { + let $a = convertToTensor(a, 'a', 'greater'); + let $b = convertToTensor(b, 'b', 'greater'); + [$a, $b] = makeTypesMatch($a, $b); + + assertAndGetBroadcastShape($a.shape, $b.shape); + + const forward: ForwardFunc = backend => backend.greater($a, $b); + + const inputs: GreaterInputs = {a: $a, b: $b}; + + return ENGINE.runKernelFunc( + forward, inputs as {} as NamedTensorMap, null /* grad */, + Greater) as T; +} + +export const greater = op({greater_}); diff --git a/tfjs-core/src/ops/greater_test.ts b/tfjs-core/src/ops/greater_test.ts new file mode 100644 index 00000000000..090ba9730e6 --- /dev/null +++ b/tfjs-core/src/ops/greater_test.ts @@ -0,0 +1,355 @@ +/** + * @license + * Copyright 2018 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} from '../test_util'; + +describeWithFlags('greater', ALL_ENVS, () => { + it('Tensor1D - int32', async () => { + let a = tf.tensor1d([1, 4, 5], 'int32'); + let b = tf.tensor1d([2, 3, 5], 'int32'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0]); + + a = tf.tensor1d([2, 2, 2], 'int32'); + b = tf.tensor1d([2, 2, 2], 'int32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0]); + + a = tf.tensor1d([3, 3], 'int32'); + b = tf.tensor1d([0, 0], 'int32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1]); + }); + 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'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0]); + + a = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + b = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0]); + + a = tf.tensor1d([3.123, 3.321], 'float32'); + b = tf.tensor1d([0.45, 0.123], 'float32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1]); + }); + + it('upcasts when dtypes dont match', async () => { + const a = [1.1, 4.1, 5.2]; + const b = [2.2, 3.2, 5.1]; + + let res = + tf.greater(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, 1, 1]); + + res = tf.greater(tf.tensor(a, [3], 'int32'), tf.tensor(b, [3], 'bool')); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([3]); + expectArraysClose(await res.data(), [0, 1, 1]); + }); + + it('TensorLike', async () => { + const a = [1.1, 4.1, 5.1]; + const b = [2.2, 3.2, 5.1]; + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0]); + }); + it('TensorLike Chained', async () => { + const a = tf.tensor1d([1.1, 4.1, 5.1], 'float32'); + const b = [2.2, 3.2, 5.1]; + const res = a.greater(b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0]); + }); + it('mismatched Tensor1D shapes - int32', () => { + const a = tf.tensor1d([1, 2], 'int32'); + const b = tf.tensor1d([1, 2, 3], 'int32'); + const f = () => { + tf.greater(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.greater(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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0]); + }); + + // Tensor2D: + it('Tensor2D - int32', async () => { + let a = tf.tensor2d([[1, 4, 5], [8, 9, 11]], [2, 3], 'int32'); + let b = tf.tensor2d([[2, 3, 6], [7, 10, 11]], [2, 3], 'int32'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); + + a = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + b = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0]); + }); + it('Tensor2D - float32', async () => { + let a = tf.tensor2d([[1.1, 4.1, 5.1], [8.1, 9.1, 11.1]], [2, 3], 'float32'); + let b = + tf.tensor2d([[2.1, 3.1, 6.1], [7.1, 10.1, 11.1]], [2, 3], 'float32'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 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'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0]); + }); + 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0, 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0, 0, 0]); + }); + it('NaNs in Tensor2D - float32', async () => { + const a = tf.tensor2d([[1.1, NaN], [0.1, NaN]], [2, 2], 'float32'); + const b = tf.tensor2d([[0.1, NaN], [1.1, NaN]], [2, 2], 'float32'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0]); + }); + + // Tensor3D: + it('Tensor3D - int32', async () => { + let a = + tf.tensor3d([[[1], [4], [5]], [[8], [9], [11]]], [2, 3, 1], 'int32'); + let b = + tf.tensor3d([[[2], [3], [6]], [[7], [10], [11]]], [2, 3, 1], 'int32'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); + + 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'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0]); + }); + it('Tensor3D - float32', async () => { + let a = tf.tensor3d( + [[[1.1], [4.1], [5.1]], [[8.1], [9.1], [11.1]]], [2, 3, 1], 'float32'); + let b = tf.tensor3d( + [[[2.1], [3.1], [6.1]], [[7.1], [10.1], [11.1]]], [2, 3, 1], 'float32'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 0]); + + a = tf.tensor3d( + [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.2]]], [2, 3, 1], 'float32'); + b = tf.tensor3d( + [[[0.1], [0.1], [0.1]], [[1.1], [1.1], [1.1]]], [2, 3, 1], 'float32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1]); + }); + 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1]); + }); + 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0, 0, 0]); + }); + + // 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'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 0]); + + a = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); + b = tf.tensor4d([0, 1, 2, 3], [2, 2, 1, 1], 'int32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0]); + + a = tf.tensor4d([2, 2, 2, 2], [2, 2, 1, 1], 'int32'); + b = tf.tensor4d([1, 1, 1, 1], [2, 2, 1, 1], 'int32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1, 1, 1]); + }); + 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'); + let res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 0]); + + 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'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0]); + + a = tf.tensor4d([1.1, 1.1, 1.1, 1.1], [2, 2, 1, 1], 'float32'); + b = tf.tensor4d([0.1, 0.1, 0.1, 0.1], [2, 2, 1, 1], 'float32'); + res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1, 1, 1]); + }); + 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0, 1, 1]); + }); + 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'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 0, 0, 0, 0, 0, 1, 1]); + }); + it('NaNs in Tensor4D - float32', async () => { + const a = tf.tensor4d([1.1, NaN, 0.1, 0.1], [2, 2, 1, 1], 'float32'); + const b = tf.tensor4d([0.1, 1.1, 1.1, NaN], [2, 2, 1, 1], 'float32'); + const res = tf.greater(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0]); + }); + + it('throws when passed a as a non-tensor', () => { + expect(() => tf.greater({} as tf.Tensor, tf.scalar(1))) + .toThrowError(/Argument 'a' passed to 'greater' must be a Tensor/); + }); + it('throws when passed b as a non-tensor', () => { + expect(() => tf.greater(tf.scalar(1), {} as tf.Tensor)) + .toThrowError(/Argument 'b' passed to 'greater' must be a Tensor/); + }); + + it('accepts a tensor-like object', async () => { + const a = [1, 4, 5]; + const b = [2, 3, 5]; + + const res = tf.greater(a, b); + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0]); + }); + + it('works with 0 sized tensors', async () => { + const a = tf.tensor2d([], [0, 5]); + const b = tf.tensor1d([1, 2, 3, 4, 5]); + const res = tf.greater(a, b); + expect(res.dtype).toBe('bool'); + expect(res.shape).toEqual([0, 5]); + expectArraysClose(await res.data(), []); + }); +}); diff --git a/tfjs-core/src/ops/ops.ts b/tfjs-core/src/ops/ops.ts index 4dd9d8b7115..c4fb67d288e 100644 --- a/tfjs-core/src/ops/ops.ts +++ b/tfjs-core/src/ops/ops.ts @@ -32,6 +32,7 @@ export {concat4d} from './concat_4d'; export {div} from './div'; export {divNoNan} from './div_no_nan'; export {eye} from './eye'; +export {greater} from './greater'; export {multinomial} from './multinomial'; export {notEqual} from './not_equal'; export {oneHot} from './one_hot'; diff --git a/tfjs-core/src/public/chained_ops/greater.ts b/tfjs-core/src/public/chained_ops/greater.ts new file mode 100644 index 00000000000..87ca4261563 --- /dev/null +++ b/tfjs-core/src/public/chained_ops/greater.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 {greater} from '../../ops/greater'; +import {Tensor} from '../../tensor'; +import {Rank, TensorLike} from '../../types'; + +declare module '../../tensor' { + interface Tensor { + greater(b: Tensor|TensorLike): T; + } +} + +Tensor.prototype.greater = function(b: Tensor|TensorLike): T { + this.throwIfDisposed(); + return greater(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 2516bae57db..95434ad831c 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 './greater'; import './one_hot'; import './not_equal'; import './pad'; 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 541100ca35c..e72e11e6f31 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 @@ -24,8 +24,8 @@ import {ALL_ENVS, describeWithFlags} from '../../jasmine_util'; // flexibility to change in future. const CHAINED_OPS = [ - 'add', 'batchNorm', 'broadcastTo', 'concat', 'div', 'divNoNan', 'notEqual', - 'oneHot', 'pad', 'split', 'square', 'sub', 'tile', 'transpose' + 'add', 'batchNorm', 'broadcastTo', 'concat', 'div', 'divNoNan', 'greater', + '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 f5ece96217a..ffc3183d500 100644 --- a/tfjs-core/src/tensor.ts +++ b/tfjs-core/src/tensor.ts @@ -234,7 +234,6 @@ export interface OpHandler { equalStrict(a: T, b: T|TensorLike): T; lessEqual(a: Tensor, b: Tensor|TensorLike): T; lessEqualStrict(a: T, b: T|TensorLike): T; - greater(a: Tensor, b: Tensor|TensorLike): T; greaterStrict(a: T, b: T|TensorLike): T; greaterEqual(a: Tensor, b: Tensor|TensorLike): T; greaterEqualStrict(a: T, b: T|TensorLike): T; @@ -953,10 +952,6 @@ export class Tensor { this.throwIfDisposed(); return opHandler.lessEqualStrict(this, x); } - greater(x: Tensor|TensorLike): T { - this.throwIfDisposed(); - return opHandler.greater(this, x); - } greaterStrict(this: T, x: T|TensorLike): T { this.throwIfDisposed(); return opHandler.greaterStrict(this, x); diff --git a/tfjs-core/src/tests.ts b/tfjs-core/src/tests.ts index 4aa3b5d4c87..b7907d653b9 100644 --- a/tfjs-core/src/tests.ts +++ b/tfjs-core/src/tests.ts @@ -68,6 +68,7 @@ import './ops/dropout_util_test'; import './ops/eye_test'; import './ops/fused_test'; import './ops/gather_nd_test'; +import './ops/greater_test'; import './ops/image_ops_test'; import './ops/in_top_k_test'; import './ops/linalg_ops_test';