This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 950
/
binary_ops.ts
678 lines (643 loc) · 21 KB
/
binary_ops.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/**
* @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 {doc} from '../doc';
import {ENV} from '../environment';
import {Tensor} from '../tensor';
import {upcastType} from '../types';
import * as util from '../util';
import * as broadcast_util from './broadcast_util';
import {operation} from './operation';
import {neg, scalar, square} from './ops';
export class BinaryOps {
/**
* Adds two `Tensor`s element-wise, A + B. Supports broadcasting.
*
* We also expose `addStrict` 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, 4]);
* const b = tf.tensor1d([10, 20, 30, 40]);
*
* a.add(b).print(); // or tf.add(a, b)
* ```
*
* ```js
* // Broadcast add a with b.
* const a = tf.scalar(5);
* const b = tf.tensor1d([10, 20, 30, 40]);
*
* a.add(b).print(); // or tf.add(a, b)
* ```
* @param a The first `Tensor` to add.
* @param b The second `Tensor` to add. Must have the same type as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static add<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'add');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
let res = dy;
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(a.shape);
};
const derB = () => {
let res = dy;
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(b.shape);
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(backend => backend.add(a, b), {a, b}, der) as T;
}
/**
* Adds two `Tensor`s element-wise, A + B.
*
* Inputs must be the same shape. For broadcasting support, use add() instead.
*
* @param a The first Tensor to add element-wise.
* @param b The second Tensor to add element-wise.
*/
@operation
static addStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in addStrict: ');
return a.add(b);
}
/**
* Subtracts two `Tensor`s element-wise, A - B. Supports broadcasting.
*
* We also expose `subStrict` 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([10, 20, 30, 40]);
* const b = tf.tensor1d([1, 2, 3, 4]);
*
* a.sub(b).print(); // or tf.sub(a, b)
* ```
*
* ```js
* // Broadcast subtract a with b.
* const a = tf.tensor1d([10, 20, 30, 40]);
* const b = tf.scalar(5);
*
* a.sub(b).print(); // or tf.sub(a, b)
* ```
* @param a The first `Tensor` to subtract from.
* @param b The second `Tensor` to be subtracted. Must have the same dtype as
* `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static sub<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'sub');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
let res = dy;
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(a.shape);
};
const derB = () => {
let res = dy;
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.neg().reshape(b.shape);
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(
backend => backend.subtract(a, b), {a, b}, der) as T;
}
/**
* Subtracts two `Tensor`s element-wise, A - B. Inputs must
* be the same shape.
*
* For broadcasting support, use sub() instead.
*
* @param a The first Tensor to subtract element-wise.
* @param b The second Tensor to subtract element-wise.
*/
@operation
static subStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in subStrict: ');
return a.sub(b);
}
/**
* Computes the power of one `Tensor` to another. Supports broadcasting.
*
* Given a `Tensor` x and a `Tensor` y, this operation computes x^y for
* corresponding elements in x and y. The result's dtype will be the upcasted
* type of the `base` and `exp` dtypes.
*
* ```js
* const a = tf.tensor([[2, 3], [4, 5]])
* const b = tf.tensor([[1, 2], [3, 0]]).toInt();
*
* a.pow(b).print(); // or tf.pow(a, b)
* ```
*
* ```js
* const a = tf.tensor([[1, 2], [3, 4]])
* const b = tf.tensor(2).toInt();
*
* a.pow(b).print(); // or tf.pow(a, b)
* ```
* We also expose `powStrict` which has the same signature as this op and
* asserts that `base` and `exp` are the same shape (does not broadcast).
*
* @param base The base `Tensor` to pow element-wise.
* @param exp The exponent `Tensor` to pow element-wise.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static pow<T extends Tensor>(base: T, exp: Tensor): T {
util.assertArgumentsAreTensors({base, exp}, 'pow');
const outShape =
broadcast_util.assertAndGetBroadcastShape(base.shape, exp.shape);
base = base.cast(upcastType(base.dtype, exp.dtype));
exp = exp.cast(upcastType(base.dtype, exp.dtype));
const grad = (dy: Tensor, saved: Tensor[]) => {
const [y] = saved;
const derBase = () => {
let res = dy.mul(exp.toFloat().mul(y.div(base)));
const reduceAxes =
broadcast_util.getReductionAxes(base.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(base.shape) as T;
};
const derExp = () => {
let res = dy.mul(y.mul(base.log()).toFloat());
const reduceAxes = broadcast_util.getReductionAxes(exp.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(exp.shape);
};
return {base: derBase, exp: derExp};
};
return ENV.engine.runKernel(
(backend, save) => save(backend.pow(base, exp)), {base, exp},
grad) as T;
}
/**
* Computes the power of one `Tensor` to another. Inputs must
* be the same shape.
*
* For broadcasting support, use pow() instead.
*
* @param base The base tensor to pow element-wise.
* @param exp The exponent tensor to pow element-wise.
*/
@operation
static powStrict<T extends Tensor>(base: T, exp: Tensor): T {
util.assertShapesMatch(base.shape, exp.shape, 'Error in powStrict: ');
return base.pow(exp);
}
/**
* Multiplies two `Tensor`s element-wise, A * B. Supports broadcasting.
*
* We also expose `mulStrict` 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, 4]);
* const b = tf.tensor1d([2, 3, 4, 5]);
*
* a.mul(b).print(); // or tf.mul(a, b)
* ```
*
* ```js
* // Broadcast mul a with b.
* const a = tf.tensor1d([1, 2, 3, 4]);
* const b = tf.scalar(5);
*
* a.mul(b).print(); // or tf.mul(a, b)
* ```
* @param a The first tensor to multiply.
* @param b The second tensor to multiply. Must have the same dtype as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static mul<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'mul');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
const res = dy.mul(b.toFloat());
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
return res.sum(reduceAxes).reshape(a.shape);
}
return res;
};
const derB = () => {
const res = dy.mul(a.toFloat());
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
return res.sum(reduceAxes).reshape(b.shape);
}
return res;
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(
backend => backend.multiply(a, b), {a, b}, der) as T;
}
/**
* Multiplies two `Tensor`s element-wise, A * B.
*
* Inputs must be the same shape. For broadcasting support, use mul().
*
* @param a The first tensor to multiply.
* @param b The first tensor to multiply. Must have the same
* dtype as `a`.
*/
@operation
static mulStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in multiplyStrict: ');
return a.mul(b) as T;
}
/**
* Divides two `Tensor`s element-wise, A / B. Supports broadcasting.
*
* We also expose `divStrict` 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, 4, 9, 16]);
* const b = tf.tensor1d([1, 2, 3, 4]);
*
* a.div(b).print(); // or tf.div(a, b)
* ```
*
* ```js
* // Broadcast div a with b.
* const a = tf.tensor1d([2, 4, 6, 8]);
* const b = tf.scalar(2);
*
* a.div(b).print(); // or tf.div(a, b)
* ```
*
* @param a The first tensor as the numerator.
* @param b The second tensor as the denominator. Must have the same dtype as
* `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static div<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'div');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
const res = dy.div(b.toFloat());
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
return res.sum(reduceAxes).reshape(a.shape);
}
return res;
};
const derB = () => {
let res = dy.mul(a.toFloat());
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes).reshape(b.shape);
}
const tmp = b.square() as Tensor;
return res.div(tmp.toFloat()).neg() as Tensor;
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(backend => backend.divide(a, b), {a, b}, der) as
T;
}
/**
* Divides two `Tensor`s element-wise, A / B. Inputs must
* be the same shape.
*
* @param a The first tensor as the numerator for element-wise division.
* @param b The second tensor as the denominator for element-wise division.
*/
@operation
static divStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in divideStrict: ');
return a.div(b) as T;
}
/**
* Returns the mod of a and b element-wise.
* `floor(x / y) * y + mod(x, y) = x`
* Supports broadcasting.
*
* We also expose `modStrict` 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, 4, 3, 16]);
* const b = tf.tensor1d([1, 2, 9, 4]);
*
* a.mod(b).print(); // or tf.mod(a, b)
* ```
*
* ```js
* // Broadcast a mod b.
* const a = tf.tensor1d([2, 4, 6, 8]);
* const b = tf.scalar(5);
*
* a.mod(b).print(); // or tf.mod(a, b)
* ```
*
* @param a The first tensor.
* @param b The second tensor. Must have the same type as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static mod<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'mod');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
return dy.sum(reduceAxes).reshape(a.shape);
}
return dy;
};
const derB = () => {
const res = dy.mul(a.div(b).floor().neg());
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
return res.sum(reduceAxes).reshape(b.shape);
}
return res;
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(backend => backend.mod(a, b), {a, b}, der) as T;
}
/**
* Returns the mod of a and b (`a < b ? a : b`) element-wise. Inputs must
* be the same shape. For broadcasting support, use mod().
*
* @param a The first tensor.
* @param b The second tensor. Must have the same dtype as `a`.
*/
@operation
static modStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in modStrict: ');
return a.mod(b);
}
/**
* Returns the min of a and b (`a < b ? a : b`) element-wise.
* Supports broadcasting.
*
* We also expose `minimumStrict` 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, 4, 3, 16]);
* const b = tf.tensor1d([1, 2, 9, 4]);
*
* a.minimum(b).print(); // or tf.minimum(a, b)
* ```
*
* ```js
* // Broadcast minimum a with b.
* const a = tf.tensor1d([2, 4, 6, 8]);
* const b = tf.scalar(5);
*
* a.minimum(b).print(); // or tf.minimum(a, b)
* ```
*
* @param a The first tensor.
* @param b The second tensor. Must have the same type as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static minimum<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'minimum');
util.assertTypesMatch(a, b);
if (a.dtype === 'bool') {
a = a.toInt();
}
if (b.dtype === 'bool') {
b = b.toInt();
}
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => dy.mul(a.lessEqual(b).toFloat());
const derB = () => dy.mul(a.greater(b).toFloat());
return {a: derA, b: derB};
};
return ENV.engine.runKernel(
backend => backend.minimum(a, b), {a, b}, der) as T;
}
/**
* Returns the min of a and b (`a < b ? a : b`) element-wise. Inputs must
* be the same shape. For broadcasting support, use minimum().
*
* @param a The first tensor.
* @param b The second tensor. Must have the same dtype as `a`.
*/
@operation
static minimumStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in minimumStrict: ');
return a.minimum(b);
}
/**
* Returns the max of a and b (`a > b ? a : b`) element-wise.
* Supports broadcasting.
*
* We also expose `maximumStrict` 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, 4, 3, 16]);
* const b = tf.tensor1d([1, 2, 9, 4]);
*
* a.maximum(b).print(); // or tf.maximum(a, b)
* ```
*
* ```js
* // Broadcast maximum a with b.
* const a = tf.tensor1d([2, 4, 6, 8]);
* const b = tf.scalar(5);
*
* a.maximum(b).print(); // or tf.maximum(a, b)
* ```
*
* @param a The first tensor.
* @param b The second tensor. Must have the same type as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static maximum<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'maximum');
util.assertTypesMatch(a, b);
if (a.dtype === 'bool') {
a = a.toInt();
}
if (b.dtype === 'bool') {
b = b.toInt();
}
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => dy.mul(a.greaterEqual(b).toFloat());
const derB = () => dy.mul(a.less(b).toFloat());
return {a: derA, b: derB};
};
return ENV.engine.runKernel(
backend => backend.maximum(a, b), {a, b}, der) as T;
}
/**
* Returns the max of a and b (`a > b ? a : b`) element-wise. Inputs must
* be the same shape. For broadcasting support, use maximum().
*
* @param a The first tensor.
* @param b The second tensor. Must have the same dtype as `a`.
*/
@operation
static maximumStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(a.shape, b.shape, 'Error in minimumStrict: ');
return a.maximum(b);
}
/**
* Returns (a - b) * (a - b) element-wise.
* Supports broadcasting.
*
* We also expose `squaredDifferenceStrict` 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, 4, 3, 16]);
* const b = tf.tensor1d([1, 2, 9, 4]);
*
* a.squaredDifference(b).print(); // or tf.squaredDifference(a, b)
* ```
*
* ```js
* // Broadcast squared difference a with b.
* const a = tf.tensor1d([2, 4, 6, 8]);
* const b = tf.scalar(5);
*
* a.squaredDifference(b).print(); // or tf.squaredDifference(a, b)
* ```
*
* @param a The first tensor.
* @param b The second tensor. Must have the same type as `a`.
*/
@doc({heading: 'Operations', subheading: 'Arithmetic'})
@operation
static squaredDifference<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'squaredDifference');
util.assertTypesMatch(a, b);
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const two = scalar(2);
const derA = () => dy.mul(a.sub(b).mul(two));
const derB = () => dy.mul(b.sub(a).mul(two));
return {a: derA, b: derB};
};
return ENV.engine.runKernel(
backend => backend.squaredDifference(a, b), {a, b}, der) as T;
}
/**
* Returns (a - b) * (a - b) element-wise.
*
* Inputs must be the same shape. For broadcasting support, use
* squaredDifference() instead.
*
* @param a The first tensor.
* @param b The second tensor. Must have the same type as `a`.
*/
@operation
static squaredDifferenceStrict<T extends Tensor>(a: T, b: T): T {
util.assertShapesMatch(
a.shape, b.shape, 'Error in squaredDifferenceStrict: ');
return a.squaredDifference(b);
}
/*
* Computes arctangent of `Tensor`s a / b element-wise: `atan2(a, b)`.
* Supports broadcasting.
*
* ```js
* const a = tf.tensor1d([1.0, 1.0, -1.0, .7]);
* const b = tf.tensor1d([2.0, 13.0, 3.5, .21]);
*
* tf.atan2(x, y).print()
*```
*
* @param a The first tensor.
* @param b The second tensor. Must have the same dtype as `a`.
*
*/
@operation
static atan2<T extends Tensor>(a: Tensor, b: Tensor): T {
util.assertArgumentsAreTensors({a, b}, 'atan2');
util.assertTypesMatch(a, b);
const outShape =
broadcast_util.assertAndGetBroadcastShape(a.shape, b.shape);
const der = (dy: Tensor) => {
const derA = () => {
const d = BinaryOps.add(square(a), square(b));
let res = dy.mul(b.div(d));
const reduceAxes = broadcast_util.getReductionAxes(a.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(a.shape);
};
const derB = () => {
const d = BinaryOps.add(square(a), square(b)) as T;
let res = neg(dy.mul(a.div(d)));
const reduceAxes = broadcast_util.getReductionAxes(b.shape, outShape);
if (reduceAxes.length > 0) {
res = res.sum(reduceAxes);
}
return res.reshape(b.shape);
};
return {a: derA, b: derB};
};
return ENV.engine.runKernel(backend => backend.atan2(a, b), {a, b}, der) as
T;
}
}