0
# Math Operations
1
2
Comprehensive mathematical operations including arithmetic, trigonometric, linear algebra, and statistical functions. These operations provide the mathematical foundation for machine learning computations and numerical analysis.
3
4
## Capabilities
5
6
### Basic Arithmetic
7
8
Fundamental arithmetic operations for tensor computations.
9
10
```python { .api }
11
def add(x, y, name=None):
12
"""
13
Returns x + y element-wise.
14
15
Parameters:
16
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string
17
- y: A Tensor. Must have the same type as x
18
- name: A name for the operation
19
20
Returns:
21
A Tensor. Has the same type as x
22
"""
23
24
def subtract(x, y, name=None):
25
"""
26
Returns x - y element-wise.
27
28
Parameters:
29
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
30
- y: A Tensor. Must have the same type as x
31
- name: A name for the operation
32
33
Returns:
34
A Tensor. Has the same type as x
35
"""
36
37
def multiply(x, y, name=None):
38
"""
39
Returns x * y element-wise.
40
41
Parameters:
42
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128
43
- y: A Tensor. Must have the same type as x
44
- name: A name for the operation
45
46
Returns:
47
A Tensor. Has the same type as x
48
"""
49
50
def divide(x, y, name=None):
51
"""
52
Computes Python style division of x by y.
53
54
Parameters:
55
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64, complex64, complex128
56
- y: A Tensor. Must have the same type as x
57
- name: A name for the operation
58
59
Returns:
60
A Tensor. Has the same type as x
61
"""
62
63
def floordiv(x, y, name=None):
64
"""
65
Divides x / y elementwise, rounding toward the most negative integer.
66
67
Parameters:
68
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, uint8, int8, uint16, int16, int32, int64
69
- y: A Tensor. Must have the same type as x
70
- name: A name for the operation
71
72
Returns:
73
A Tensor. Has the same type as x
74
"""
75
76
def mod(x, y, name=None):
77
"""
78
Returns element-wise remainder of division.
79
80
Parameters:
81
- x: A Tensor. Must be one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64, bfloat16, half, float32, float64
82
- y: A Tensor. Must have the same type as x
83
- name: A name for the operation
84
85
Returns:
86
A Tensor. Has the same type as x
87
"""
88
89
def pow(x, y, name=None):
90
"""
91
Computes the power of one value to another.
92
93
Parameters:
94
- x: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128
95
- y: A Tensor of type float16, float32, float64, int32, int64, complex64, or complex128
96
- name: A name for the operation
97
98
Returns:
99
A Tensor
100
"""
101
```
102
103
### Mathematical Functions
104
105
Common mathematical functions and operations.
106
107
```python { .api }
108
def abs(x, name=None):
109
"""
110
Computes the absolute value of a tensor.
111
112
Parameters:
113
- x: A Tensor or SparseTensor of type float16, float32, float64, int8, int16, int32, int64, complex64 or complex128
114
- name: A name for the operation
115
116
Returns:
117
A Tensor or SparseTensor the same size, type, and sparsity as x with absolute values
118
"""
119
120
def sign(x, name=None):
121
"""
122
Returns an element-wise indication of the sign of a number.
123
124
Parameters:
125
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
126
- name: A name for the operation
127
128
Returns:
129
A Tensor. Has the same type as x
130
"""
131
132
def sqrt(x, name=None):
133
"""
134
Computes element-wise square root of the input tensor.
135
136
Parameters:
137
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
138
- name: A name for the operation
139
140
Returns:
141
A Tensor. Has the same type as x
142
"""
143
144
def square(x, name=None):
145
"""
146
Computes square of x element-wise.
147
148
Parameters:
149
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
150
- name: A name for the operation
151
152
Returns:
153
A Tensor. Has the same type as x
154
"""
155
156
def exp(x, name=None):
157
"""
158
Computes exponential of x element-wise.
159
160
Parameters:
161
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
162
- name: A name for the operation
163
164
Returns:
165
A Tensor. Has the same type as x
166
"""
167
168
def log(x, name=None):
169
"""
170
Computes natural logarithm of x element-wise.
171
172
Parameters:
173
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
174
- name: A name for the operation
175
176
Returns:
177
A Tensor. Has the same type as x
178
"""
179
180
def log10(x, name=None):
181
"""
182
Computes element-wise log base 10 of x.
183
184
Parameters:
185
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
186
- name: A name for the operation
187
188
Returns:
189
A Tensor. Has the same type as x
190
"""
191
```
192
193
### Trigonometric Functions
194
195
Trigonometric and hyperbolic functions for mathematical computations.
196
197
```python { .api }
198
def sin(x, name=None):
199
"""
200
Computes sine of x element-wise.
201
202
Parameters:
203
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
204
- name: A name for the operation
205
206
Returns:
207
A Tensor. Has the same type as x
208
"""
209
210
def cos(x, name=None):
211
"""
212
Computes cosine of x element-wise.
213
214
Parameters:
215
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
216
- name: A name for the operation
217
218
Returns:
219
A Tensor. Has the same type as x
220
"""
221
222
def tan(x, name=None):
223
"""
224
Computes tan of x element-wise.
225
226
Parameters:
227
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
228
- name: A name for the operation
229
230
Returns:
231
A Tensor. Has the same type as x
232
"""
233
234
def asin(x, name=None):
235
"""
236
Computes the trignometric inverse sine of x element-wise.
237
238
Parameters:
239
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
240
- name: A name for the operation
241
242
Returns:
243
A Tensor. Has the same type as x
244
"""
245
246
def acos(x, name=None):
247
"""
248
Computes acos of x element-wise.
249
250
Parameters:
251
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
252
- name: A name for the operation
253
254
Returns:
255
A Tensor. Has the same type as x
256
"""
257
258
def atan(x, name=None):
259
"""
260
Computes the trignometric inverse tangent of x element-wise.
261
262
Parameters:
263
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int8, int16, int32, int64, complex64, complex128
264
- name: A name for the operation
265
266
Returns:
267
A Tensor. Has the same type as x
268
"""
269
270
def sinh(x, name=None):
271
"""
272
Computes hyperbolic sine of x element-wise.
273
274
Parameters:
275
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
276
- name: A name for the operation
277
278
Returns:
279
A Tensor. Has the same type as x
280
"""
281
282
def cosh(x, name=None):
283
"""
284
Computes hyperbolic cosine of x element-wise.
285
286
Parameters:
287
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
288
- name: A name for the operation
289
290
Returns:
291
A Tensor. Has the same type as x
292
"""
293
294
def tanh(x, name=None):
295
"""
296
Computes hyperbolic tangent of x element-wise.
297
298
Parameters:
299
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128
300
- name: A name for the operation
301
302
Returns:
303
A Tensor. Has the same type as x
304
"""
305
```
306
307
### Linear Algebra
308
309
Matrix operations and linear algebra functions.
310
311
```python { .api }
312
def matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False,
313
adjoint_b=False, a_is_sparse=False, b_is_sparse=False, output_type=None,
314
grad_a=False, grad_b=False, name=None):
315
"""
316
Multiplies matrix a by matrix b, producing a * b.
317
318
Parameters:
319
- a: A Tensor of type float16, float32, float64, int32, int64, complex64, complex128 and rank > 1
320
- b: A Tensor with same type and rank as a
321
- transpose_a: If True, a is transposed before multiplication
322
- transpose_b: If True, b is transposed before multiplication
323
- adjoint_a: If True, a is conjugated and transposed before multiplication
324
- adjoint_b: If True, b is conjugated and transposed before multiplication
325
- a_is_sparse: If True, a is treated as a sparse matrix
326
- b_is_sparse: If True, b is treated as a sparse matrix
327
- output_type: The output type of the operation (float16, float32, etc.)
328
- grad_a: Whether to use gradient optimized version for matrix a
329
- grad_b: Whether to use gradient optimized version for matrix b
330
- name: Name for the operation
331
332
Returns:
333
A Tensor of the same type as a and b where each inner-most matrix is the product of the corresponding matrices in a and b
334
"""
335
336
def transpose(a, perm=None, conjugate=False, name="transpose"):
337
"""
338
Transposes a.
339
340
Parameters:
341
- a: A Tensor
342
- perm: A permutation of the dimensions of a
343
- conjugate: Setting it to True is mathematically equivalent to tf.math.conj(tf.transpose(input))
344
- name: A name for the operation
345
346
Returns:
347
A transposed Tensor
348
"""
349
350
def trace(x, name=None):
351
"""
352
Compute the trace of a tensor x.
353
354
Parameters:
355
- x: A Tensor. Must be one of the following types: bfloat16, half, float32, float64, int32, int64, complex64, complex128
356
- name: A name for the operation
357
358
Returns:
359
A Tensor. Has the same type as x
360
"""
361
362
def det(input, name=None):
363
"""
364
Computes the determinant of one or more square matrices.
365
366
Parameters:
367
- input: A Tensor of type float32, float64, complex64 or complex128 of shape [..., M, M]
368
- name: A name for the operation
369
370
Returns:
371
A Tensor of the same type as input with shape [...]
372
"""
373
374
def inv(input, adjoint=False, name=None):
375
"""
376
Computes the inverse of one or more square invertible matrices or their adjoints (conjugate transposes).
377
378
Parameters:
379
- input: A Tensor. Must be one of the following types: float64, float32, half, complex64, complex128
380
- adjoint: An optional bool. Defaults to False
381
- name: A name for the operation
382
383
Returns:
384
A Tensor. Has the same type as input
385
"""
386
387
def norm(tensor, ord='euclidean', axis=None, keepdims=None, name=None):
388
"""
389
Computes the norm of vectors, matrices, and tensors.
390
391
Parameters:
392
- tensor: A Tensor
393
- ord: Order of the norm. Supported values are 'fro', 'euclidean', 1, 2, np.inf and any positive real number yielding the corresponding p-norm
394
- axis: If axis is None (the default), the input is considered a vector and a single vector norm is computed over the entire set of values in the tensor
395
- keepdims: If True, the axis indicated in axis are kept with size 1
396
- name: The name of the op
397
398
Returns:
399
A Tensor with the same type as tensor, containing the vector or matrix norms
400
"""
401
```
402
403
### Reduction Operations
404
405
Operations that reduce tensor dimensions through aggregation.
406
407
```python { .api }
408
def reduce_sum(input_tensor, axis=None, keepdims=None, name=None):
409
"""
410
Computes the sum of elements across dimensions of a tensor.
411
412
Parameters:
413
- input_tensor: The tensor to reduce. Should have numeric type
414
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
415
- keepdims: If true, retains reduced dimensions with length 1
416
- name: A name for the operation
417
418
Returns:
419
The reduced tensor
420
"""
421
422
def reduce_mean(input_tensor, axis=None, keepdims=None, name=None):
423
"""
424
Computes the mean of elements across dimensions of a tensor.
425
426
Parameters:
427
- input_tensor: The tensor to reduce. Should have numeric type
428
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
429
- keepdims: If true, retains reduced dimensions with length 1
430
- name: A name for the operation
431
432
Returns:
433
The reduced tensor
434
"""
435
436
def reduce_max(input_tensor, axis=None, keepdims=None, name=None):
437
"""
438
Computes the maximum of elements across dimensions of a tensor.
439
440
Parameters:
441
- input_tensor: The tensor to reduce. Should have numeric type
442
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
443
- keepdims: If true, retains reduced dimensions with length 1
444
- name: A name for the operation
445
446
Returns:
447
The reduced tensor
448
"""
449
450
def reduce_min(input_tensor, axis=None, keepdims=None, name=None):
451
"""
452
Computes the minimum of elements across dimensions of a tensor.
453
454
Parameters:
455
- input_tensor: The tensor to reduce. Should have numeric type
456
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
457
- keepdims: If true, retains reduced dimensions with length 1
458
- name: A name for the operation
459
460
Returns:
461
The reduced tensor
462
"""
463
464
def reduce_prod(input_tensor, axis=None, keepdims=None, name=None):
465
"""
466
Computes the product of elements across dimensions of a tensor.
467
468
Parameters:
469
- input_tensor: The tensor to reduce. Should have numeric type
470
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
471
- keepdims: If true, retains reduced dimensions with length 1
472
- name: A name for the operation
473
474
Returns:
475
The reduced tensor
476
"""
477
478
def reduce_all(input_tensor, axis=None, keepdims=None, name=None):
479
"""
480
Computes the "logical and" of elements across dimensions of a tensor.
481
482
Parameters:
483
- input_tensor: The boolean tensor to reduce
484
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
485
- keepdims: If true, retains reduced dimensions with length 1
486
- name: A name for the operation
487
488
Returns:
489
The reduced tensor
490
"""
491
492
def reduce_any(input_tensor, axis=None, keepdims=None, name=None):
493
"""
494
Computes the "logical or" of elements across dimensions of a tensor.
495
496
Parameters:
497
- input_tensor: The boolean tensor to reduce
498
- axis: The dimensions to reduce. If None (the default), reduces all dimensions
499
- keepdims: If true, retains reduced dimensions with length 1
500
- name: A name for the operation
501
502
Returns:
503
The reduced tensor
504
"""
505
```
506
507
### Element-wise Comparisons
508
509
Operations for comparing tensor elements.
510
511
```python { .api }
512
def equal(x, y, name=None):
513
"""
514
Returns the truth value of (x == y) element-wise.
515
516
Parameters:
517
- x: A Tensor
518
- y: A Tensor. Must have the same type as x
519
- name: A name for the operation
520
521
Returns:
522
A Tensor of type bool
523
"""
524
525
def not_equal(x, y, name=None):
526
"""
527
Returns the truth value of (x != y) element-wise.
528
529
Parameters:
530
- x: A Tensor
531
- y: A Tensor. Must have the same type as x
532
- name: A name for the operation
533
534
Returns:
535
A Tensor of type bool
536
"""
537
538
def less(x, y, name=None):
539
"""
540
Returns the truth value of (x < y) element-wise.
541
542
Parameters:
543
- x: A Tensor
544
- y: A Tensor. Must have the same type as x
545
- name: A name for the operation
546
547
Returns:
548
A Tensor of type bool
549
"""
550
551
def less_equal(x, y, name=None):
552
"""
553
Returns the truth value of (x <= y) element-wise.
554
555
Parameters:
556
- x: A Tensor
557
- y: A Tensor. Must have the same type as x
558
- name: A name for the operation
559
560
Returns:
561
A Tensor of type bool
562
"""
563
564
def greater(x, y, name=None):
565
"""
566
Returns the truth value of (x > y) element-wise.
567
568
Parameters:
569
- x: A Tensor
570
- y: A Tensor. Must have the same type as x
571
- name: A name for the operation
572
573
Returns:
574
A Tensor of type bool
575
"""
576
577
def greater_equal(x, y, name=None):
578
"""
579
Returns the truth value of (x >= y) element-wise.
580
581
Parameters:
582
- x: A Tensor
583
- y: A Tensor. Must have the same type as x
584
- name: A name for the operation
585
586
Returns:
587
A Tensor of type bool
588
"""
589
590
def maximum(x, y, name=None):
591
"""
592
Returns the max of x and y (i.e. x > y ? x : y) element-wise.
593
594
Parameters:
595
- x: A Tensor
596
- y: A Tensor. Must have the same type as x
597
- name: A name for the operation
598
599
Returns:
600
A Tensor. Has the same type as x
601
"""
602
603
def minimum(x, y, name=None):
604
"""
605
Returns the min of x and y (i.e. x < y ? x : y) element-wise.
606
607
Parameters:
608
- x: A Tensor
609
- y: A Tensor. Must have the same type as x
610
- name: A name for the operation
611
612
Returns:
613
A Tensor. Has the same type as x
614
"""
615
```
616
617
## Usage Examples
618
619
```python
620
import tensorflow as tf
621
622
# Basic arithmetic
623
a = tf.constant([1.0, 2.0, 3.0])
624
b = tf.constant([4.0, 5.0, 6.0])
625
626
sum_result = tf.add(a, b) # [5.0, 7.0, 9.0]
627
diff_result = tf.subtract(b, a) # [3.0, 3.0, 3.0]
628
prod_result = tf.multiply(a, b) # [4.0, 10.0, 18.0]
629
div_result = tf.divide(b, a) # [4.0, 2.5, 2.0]
630
631
# Mathematical functions
632
sqrt_result = tf.sqrt(a) # [1.0, 1.414, 1.732]
633
exp_result = tf.exp(a) # [2.718, 7.389, 20.086]
634
log_result = tf.log(b) # [1.386, 1.609, 1.792]
635
636
# Trigonometric functions
637
sin_result = tf.sin(a) # [0.841, 0.909, 0.141]
638
cos_result = tf.cos(a) # [0.540, -0.416, -0.990]
639
640
# Matrix operations
641
matrix_a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
642
matrix_b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
643
644
matmul_result = tf.matmul(matrix_a, matrix_b) # [[19.0, 22.0], [43.0, 50.0]]
645
transpose_result = tf.transpose(matrix_a) # [[1.0, 3.0], [2.0, 4.0]]
646
647
# Reduction operations
648
tensor = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
649
sum_all = tf.reduce_sum(tensor) # 21.0
650
sum_axis0 = tf.reduce_sum(tensor, axis=0) # [5.0, 7.0, 9.0]
651
mean_result = tf.reduce_mean(tensor) # 3.5
652
max_result = tf.reduce_max(tensor) # 6.0
653
654
# Comparisons
655
x = tf.constant([1, 2, 3])
656
y = tf.constant([1, 4, 2])
657
658
eq_result = tf.equal(x, y) # [True, False, False]
659
gt_result = tf.greater(x, y) # [False, False, True]
660
max_xy = tf.maximum(x, y) # [1, 4, 3]
661
```