Multiplies matrix `a` by matrix `b`, producing `a` * `b`. The inputs must, following any transpositions, be tensors of rank >= 2 where the inner 2 dimensions specify valid matrix multiplication arguments, and any further outer dimensions match. Both matrices must be of the same type. The s
(a,
b,
transpose_a=False,
transpose_b=False,
adjoint_a=False,
adjoint_b=False,
a_is_sparse=False,
b_is_sparse=False,
name=None)
| 2565 | @tf_export("linalg.matmul", "matmul") |
| 2566 | @dispatch.add_dispatch_support |
| 2567 | def matmul(a, |
| 2568 | b, |
| 2569 | transpose_a=False, |
| 2570 | transpose_b=False, |
| 2571 | adjoint_a=False, |
| 2572 | adjoint_b=False, |
| 2573 | a_is_sparse=False, |
| 2574 | b_is_sparse=False, |
| 2575 | name=None): |
| 2576 | """Multiplies matrix `a` by matrix `b`, producing `a` * `b`. |
| 2577 | |
| 2578 | The inputs must, following any transpositions, be tensors of rank >= 2 |
| 2579 | where the inner 2 dimensions specify valid matrix multiplication arguments, |
| 2580 | and any further outer dimensions match. |
| 2581 | |
| 2582 | Both matrices must be of the same type. The supported types are: |
| 2583 | `float16`, `float32`, `float64`, `int32`, `complex64`, `complex128`. |
| 2584 | |
| 2585 | Either matrix can be transposed or adjointed (conjugated and transposed) on |
| 2586 | the fly by setting one of the corresponding flag to `True`. These are `False` |
| 2587 | by default. |
| 2588 | |
| 2589 | If one or both of the matrices contain a lot of zeros, a more efficient |
| 2590 | multiplication algorithm can be used by setting the corresponding |
| 2591 | `a_is_sparse` or `b_is_sparse` flag to `True`. These are `False` by default. |
| 2592 | This optimization is only available for plain matrices (rank-2 tensors) with |
| 2593 | datatypes `bfloat16` or `float32`. |
| 2594 | |
| 2595 | For example: |
| 2596 | |
| 2597 | ```python |
| 2598 | # 2-D tensor `a` |
| 2599 | # [[1, 2, 3], |
| 2600 | # [4, 5, 6]] |
| 2601 | a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) |
| 2602 | |
| 2603 | # 2-D tensor `b` |
| 2604 | # [[ 7, 8], |
| 2605 | # [ 9, 10], |
| 2606 | # [11, 12]] |
| 2607 | b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) |
| 2608 | |
| 2609 | # `a` * `b` |
| 2610 | # [[ 58, 64], |
| 2611 | # [139, 154]] |
| 2612 | c = tf.matmul(a, b) |
| 2613 | |
| 2614 | |
| 2615 | # 3-D tensor `a` |
| 2616 | # [[[ 1, 2, 3], |
| 2617 | # [ 4, 5, 6]], |
| 2618 | # [[ 7, 8, 9], |
| 2619 | # [10, 11, 12]]] |
| 2620 | a = tf.constant(np.arange(1, 13, dtype=np.int32), |
| 2621 | shape=[2, 2, 3]) |
| 2622 | |
| 2623 | # 3-D tensor `b` |
| 2624 | # [[[13, 14], |