diff --git a/tfjs-core/src/gradients/GreaterEqual_grad.ts b/tfjs-core/src/gradients/GreaterEqual_grad.ts new file mode 100644 index 00000000000..0ab5dca1bd6 --- /dev/null +++ b/tfjs-core/src/gradients/GreaterEqual_grad.ts @@ -0,0 +1,29 @@ +/** + * @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 {GreaterEqual} from '../kernel_names'; +import {GradConfig} from '../kernel_registry'; +import {zerosLike} from '../ops/tensor_ops'; +import {Tensor} from '../tensor'; + +export const greaterEqualGradConfig: GradConfig = { + kernelName: GreaterEqual, + inputsToSave: ['a', 'b'], + gradFunc: (dy: Tensor, saved: Tensor[]) => { + const [a, b] = saved; + return {a: () => zerosLike(a), b: () => zerosLike(b)}; + } +}; diff --git a/tfjs-core/src/kernel_names.ts b/tfjs-core/src/kernel_names.ts index deccb85147e..861bdce929e 100644 --- a/tfjs-core/src/kernel_names.ts +++ b/tfjs-core/src/kernel_names.ts @@ -58,6 +58,9 @@ export interface FusedBatchNormAttrs { export const Greater = 'Greater'; export type GreaterInputs = BinaryInputs; +export const GreaterEqual = 'GreaterEqual'; +export type GreaterEqualInputs = 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 c09f2ff7f21..58ed9ee201b 100644 --- a/tfjs-core/src/ops/compare.ts +++ b/tfjs-core/src/ops/compare.ts @@ -14,16 +14,11 @@ * limitations under the License. * ============================================================================= */ - -import {ENGINE} from '../engine'; import {Tensor} from '../tensor'; -import {makeTypesMatch} from '../tensor_util'; import {convertToTensor} from '../tensor_util_env'; import {TensorLike} from '../types'; import {assertShapesMatch} from '../util'; -import {assertAndGetBroadcastShape} from './broadcast_util'; import {op} from './operation'; -import {zerosLike} from './tensor_ops'; /** * Strict version of `tf.notEqual` that forces `a` and `b` to be of the same @@ -78,41 +73,6 @@ function greaterStrict_(a: T|TensorLike, b: T|TensorLike): T { return $a.greater($b); } -/** - * Returns the truth value of (a >= b) element-wise. Supports broadcasting. - * - * We also expose `tf.greaterEqualStrict` 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.greaterEqual(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 greaterEqual_( - a: Tensor|TensorLike, b: Tensor|TensorLike): T { - let $a = convertToTensor(a, 'a', 'greaterEqual'); - let $b = convertToTensor(b, 'b', 'greaterEqual'); - [$a, $b] = makeTypesMatch($a, $b); - assertAndGetBroadcastShape($a.shape, $b.shape); - - const grad = (dy: T, saved: Tensor[]) => { - const [$a, $b] = saved; - return {a: () => zerosLike($a), b: () => zerosLike($b)}; - }; - return ENGINE.runKernelFunc((backend, save) => { - const res = backend.greaterEqual($a, $b); - save([$a, $b]); - return res; - }, {a: $a, b: $b}, grad, 'GreaterEqual') as T; -} - function greaterEqualStrict_( a: T|TensorLike, b: T|TensorLike): T { const $a = convertToTensor(a, 'a', 'greaterEqualStrict'); @@ -122,7 +82,6 @@ function greaterEqualStrict_( } export const equalStrict = op({equalStrict_}); -export const greaterEqual = op({greaterEqual_}); export const greaterEqualStrict = op({greaterEqualStrict_}); export const greaterStrict = op({greaterStrict_}); export const lessEqualStrict = op({lessEqualStrict_}); diff --git a/tfjs-core/src/ops/greater_equal.ts b/tfjs-core/src/ops/greater_equal.ts new file mode 100644 index 00000000000..4f8090a4e82 --- /dev/null +++ b/tfjs-core/src/ops/greater_equal.ts @@ -0,0 +1,66 @@ +/** + * @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 {GreaterEqual, GreaterEqualInputs} 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.greaterEqualStrict` 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.greaterEqual(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 greaterEqual_( + a: Tensor|TensorLike, b: Tensor|TensorLike): T { + let $a = convertToTensor(a, 'a', 'greaterEqual'); + let $b = convertToTensor(b, 'b', 'greaterEqual'); + [$a, $b] = makeTypesMatch($a, $b); + + assertAndGetBroadcastShape($a.shape, $b.shape); + + const forward: ForwardFunc = (backend, save) => { + const res = backend.greaterEqual($a, $b); + save([$a, $b]); + return res; + }; + + const inputs: GreaterEqualInputs = {a: $a, b: $b}; + + return ENGINE.runKernelFunc( + forward, inputs as {} as NamedTensorMap, null /* grad */, + GreaterEqual) as T; +} + +export const greaterEqual = op({greaterEqual_}); diff --git a/tfjs-core/src/ops/greater_equal_test.ts b/tfjs-core/src/ops/greater_equal_test.ts new file mode 100644 index 00000000000..8284c299217 --- /dev/null +++ b/tfjs-core/src/ops/greater_equal_test.ts @@ -0,0 +1,356 @@ +/** + * @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('greaterEqual', ALL_ENVS, () => { + // Tensor1D: + it('Tensor1D - int32', async () => { + let a = tf.tensor1d([1, 4, 5], 'int32'); + let b = tf.tensor1d([2, 3, 5], 'int32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 1]); + + a = tf.tensor1d([2, 2, 2], 'int32'); + b = tf.tensor1d([2, 2, 2], 'int32'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1, 1]); + + a = tf.tensor1d([0, 0], 'int32'); + b = tf.tensor1d([3, 3], 'int32'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 1]); + + a = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + b = tf.tensor1d([2.31, 2.31, 2.31], 'float32'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1, 1]); + + a = tf.tensor1d([0.45, 0.123], 'float32'); + b = tf.tensor1d([3.123, 3.321], 'float32'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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.greaterEqual( + 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.greaterEqual(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, 1, 1]); + }); + + it('mismatched Tensor1D shapes - int32', () => { + const a = tf.tensor1d([1, 2], 'int32'); + const b = tf.tensor1d([1, 2, 3], 'int32'); + const f = () => { + tf.greaterEqual(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.greaterEqual(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.greaterEqual(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, 12]], [2, 3], 'int32'); + let b = tf.tensor2d([[2, 3, 6], [7, 10, 11]], [2, 3], 'int32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 1]); + + a = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + b = tf.tensor2d([[0, 0], [1, 1]], [2, 2], 'int32'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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, 3.1, 6.1], [7.1, 10.1, 11.1]], [2, 3], 'float32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 1]); + + 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.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + const res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 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'); + const res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 1, 0, 1, 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.greaterEqual(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], [12]]], [2, 3, 1], 'int32'); + let b = + tf.tensor3d([[[2], [3], [6]], [[7], [10], [11]]], [2, 3, 1], 'int32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 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'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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], [11.1]]], [2, 3, 1], 'float32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 0, 1, 0, 1]); + + 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.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + const res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 1, 1, 1, 1, 0, 1, 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.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 1, 1, 1, 1, 0, 1, 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.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 1, 0, 1, 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, 7], [2, 2, 1, 1], 'int32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 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'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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, 7.1], [2, 2, 1, 1], 'float32'); + let res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 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'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.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'); + const res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0, 1, 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.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [1, 0, 0, 0, 1, 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.greaterEqual(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.greaterEqual({} as tf.Tensor, tf.scalar(1))) + .toThrowError(/Argument 'a' passed to 'greaterEqual' must be a Tensor/); + }); + it('throws when passed b as a non-tensor', () => { + expect(() => tf.greaterEqual(tf.scalar(1), {} as tf.Tensor)) + .toThrowError(/Argument 'b' passed to 'greaterEqual' must be a Tensor/); + }); + + it('accepts a tensor-like object', async () => { + const a = [1, 4, 5]; + const b = [2, 3, 5]; + const res = tf.greaterEqual(a, b); + + expect(res.dtype).toBe('bool'); + expectArraysClose(await res.data(), [0, 1, 1]); + }); + + it('has gradient', async () => { + const a = tf.tensor1d([3, 2, 5]); + const b = tf.tensor1d([4, 1, 5]); + const dy = tf.ones([3], 'float32'); + const da = tf.grad((a: tf.Tensor1D) => tf.greaterEqual(a, b))(a, dy); + + expect(da.dtype).toBe('float32'); + expect(da.shape).toEqual([3]); + expectArraysClose(await da.data(), [0, 0, 0]); + }); + + it('gradient with clones', async () => { + const a = tf.tensor1d([3, 2, 5]); + const b = tf.tensor1d([4, 1, 5]); + const dy = tf.ones([3], 'float32'); + const da = tf.grad( + (a: tf.Tensor1D) => tf.greaterEqual(a.clone(), b.clone()).clone())( + a, dy); + + expect(da.dtype).toBe('float32'); + expect(da.shape).toEqual([3]); + expectArraysClose(await da.data(), [0, 0, 0]); + }); +}); diff --git a/tfjs-core/src/ops/ops.ts b/tfjs-core/src/ops/ops.ts index 96599087fc7..c25c620e082 100644 --- a/tfjs-core/src/ops/ops.ts +++ b/tfjs-core/src/ops/ops.ts @@ -34,6 +34,7 @@ export {divNoNan} from './div_no_nan'; export {equal} from './equal'; export {eye} from './eye'; export {greater} from './greater'; +export {greaterEqual} from './greater_equal'; export {less} from './less'; export {lessEqual} from './less_equal'; export {multinomial} from './multinomial'; diff --git a/tfjs-core/src/ops/segment_ops.ts b/tfjs-core/src/ops/segment_ops.ts index 1a00f6a7a3f..c75eac1492a 100644 --- a/tfjs-core/src/ops/segment_ops.ts +++ b/tfjs-core/src/ops/segment_ops.ts @@ -23,7 +23,7 @@ import {assert, isInt, parseAxisParam} from '../util'; import {expandDims} from './array_ops'; import {getUndoAxesPermutation} from './axis_util'; import {maximum} from './binary_ops'; -import {greaterEqual} from './compare'; +import {greaterEqual} from './greater_equal'; import {logicalAnd, where} from './logical_ops'; import {op} from './operation'; import {collectGatherOpShapeInfo} from './segment_util'; diff --git a/tfjs-core/src/public/chained_ops/greater_equal.ts b/tfjs-core/src/public/chained_ops/greater_equal.ts new file mode 100644 index 00000000000..d2e5dd8bae0 --- /dev/null +++ b/tfjs-core/src/public/chained_ops/greater_equal.ts @@ -0,0 +1,31 @@ +/** + * @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 {greaterEqual} from '../../ops/greater_equal'; +import {Tensor} from '../../tensor'; +import {Rank, TensorLike} from '../../types'; + +declare module '../../tensor' { + interface Tensor { + greaterEqual(b: Tensor|TensorLike): T; + } +} + +Tensor.prototype.greaterEqual = function(b: Tensor| + TensorLike): T { + this.throwIfDisposed(); + return greaterEqual(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 b50bd7e3ea7..edff9ab8a90 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 @@ -22,6 +22,7 @@ import './div'; import './div_no_nan'; import './equal'; import './greater'; +import './greater_equal'; import './less'; import './less_equal'; 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 42cec4bf901..817c520adae 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', 'equal', - 'greater', 'less', 'lessEqual', 'notEqual', 'oneHot', 'pad', 'split', - 'square', 'sub', 'tile', 'transpose' + 'greater', 'greaterEqual', 'less', 'lessEqual', 'notEqual', 'oneHot', 'pad', + 'split', 'square', 'sub', 'tile', 'transpose' ]; describeWithFlags('chained ops', ALL_ENVS, () => { diff --git a/tfjs-core/src/register_all_gradients.ts b/tfjs-core/src/register_all_gradients.ts index 3339be99279..6c74c80d9e7 100644 --- a/tfjs-core/src/register_all_gradients.ts +++ b/tfjs-core/src/register_all_gradients.ts @@ -20,6 +20,7 @@ import {broadcastToGradConfig} from './gradients/BroadcastTo_grad'; import {concatGradConfig} from './gradients/Concat_grad'; import {divGradConfig} from './gradients/Div_grad'; import {fusedBatchNormGradConfig} from './gradients/FusedBatchNorm_grad'; +import {greaterEqualGradConfig} from './gradients/GreaterEqual_grad'; import {identityGradConfig} from './gradients/Identity_grad'; import {oneHotGradConfig} from './gradients/OneHot_grad'; import {padV2GradConfig} from './gradients/PadV2_grad'; @@ -35,10 +36,10 @@ import {registerGradient} from './kernel_registry'; // Export all kernel configs here so that the package can auto register them const gradConfigs: GradConfig[] = [ addGradConfig, addNGradConfig, broadcastToGradConfig, concatGradConfig, - divGradConfig, fusedBatchNormGradConfig, identityGradConfig, oneHotGradConfig, - padV2GradConfig, splitVGradConfig, squareGradConfig, - squaredDifferenceGradConfig, tileGradConfig, transposeGradConfig, - subGradConfig + divGradConfig, fusedBatchNormGradConfig, greaterEqualGradConfig, + identityGradConfig, oneHotGradConfig, padV2GradConfig, splitVGradConfig, + squareGradConfig, squaredDifferenceGradConfig, tileGradConfig, + transposeGradConfig, subGradConfig ]; for (const gradientConfig of gradConfigs) { diff --git a/tfjs-core/src/tensor.ts b/tfjs-core/src/tensor.ts index 804040e9e9a..29152cccbbf 100644 --- a/tfjs-core/src/tensor.ts +++ b/tfjs-core/src/tensor.ts @@ -232,7 +232,6 @@ export interface OpHandler { equalStrict(a: T, b: T|TensorLike): T; lessEqualStrict(a: T, b: T|TensorLike): T; greaterStrict(a: T, b: T|TensorLike): T; - greaterEqual(a: Tensor, b: Tensor|TensorLike): T; greaterEqualStrict(a: T, b: T|TensorLike): T; neg(x: T): T; ceil(x: T): T; @@ -941,10 +940,6 @@ export class Tensor { this.throwIfDisposed(); return opHandler.greaterStrict(this, x); } - greaterEqual(x: Tensor|TensorLike): T { - this.throwIfDisposed(); - return opHandler.greaterEqual(this, x); - } greaterEqualStrict(this: T, x: T|TensorLike): T { this.throwIfDisposed(); return opHandler.greaterEqualStrict(this, x); diff --git a/tfjs-core/src/tests.ts b/tfjs-core/src/tests.ts index 23b3f4e1e69..c0f71240526 100644 --- a/tfjs-core/src/tests.ts +++ b/tfjs-core/src/tests.ts @@ -69,6 +69,7 @@ import './ops/equal_test'; import './ops/eye_test'; import './ops/fused_test'; import './ops/gather_nd_test'; +import './ops/greater_equal_test'; import './ops/greater_test'; import './ops/image_ops_test'; import './ops/in_top_k_test';