tf.linalg.diag
Stay organized with collections
Save and categorize content based on your preferences.
Returns a batched diagonal tensor with given batched diagonal values.
tf.linalg.diag(
diagonal, name='diag', k=0, num_rows=-1, num_cols=-1, padding_value=0
)
Returns a tensor with the contents in diagonal
as k[0]
-th to k[1]
-th
diagonals of a matrix, with everything else padded with padding
. num_rows
and num_cols
specify the dimension of the innermost matrix of the output. If
both are not specified, the op assumes the innermost matrix is square and
infers its size from k
and the innermost dimension of diagonal
. If only
one of them is specified, the op assumes the unspecified value is the smallest
possible based on other criteria.
Let diagonal
have r
dimensions [I, J, ..., L, M, N]
. The output tensor
has rank r+1
with shape [I, J, ..., L, M, num_rows, num_cols]
when only
one diagonal is given (k
is an integer or k[0] == k[1]
). Otherwise, it has
rank r
with shape [I, J, ..., L, num_rows, num_cols]
.
The second innermost dimension of diagonal
has double meaning. When k
is
scalar or k[0] == k[1]
, M
is part of the batch size [I, J, ..., M], and
the output tensor is:
output[i, j, ..., l, m, n]
= diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper
output[i, j, ..., l, m, n] ; otherwise
Otherwise, M
is treated as the number of diagonals for the matrix in the
same batch (M = k[1]-k[0]+1
), and the output tensor is:
output[i, j, ..., l, m, n]
= diagonal[i, j, ..., l, k[1]-d, n-max(d, 0)] ; if d_lower <= d <= d_upper
input[i, j, ..., l, m, n] ; otherwise
where d = n - m
For example:
# The main diagonal.
diagonal = np.array([[1, 2, 3, 4], # Input shape: (2, 4)
[5, 6, 7, 8]])
tf.matrix_diag(diagonal) ==> [[[1, 0, 0, 0], # Output shape: (2, 4, 4)
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]],
[[5, 0, 0, 0],
[0, 6, 0, 0],
[0, 0, 7, 0],
[0, 0, 0, 8]]]
# A superdiagonal (per batch).
diagonal = np.array([[1, 2, 3], # Input shape: (2, 3)
[4, 5, 6]])
tf.matrix_diag(diagonal, k = 1)
==> [[[0, 1, 0, 0], # Output shape: (2, 4, 4)
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]],
[[0, 4, 0, 0],
[0, 0, 5, 0],
[0, 0, 0, 6],
[0, 0, 0, 0]]]
# A band of diagonals.
diagonals = np.array([[[1, 2, 3], # Input shape: (2, 2, 3)
[4, 5, 0]],
[[6, 7, 9],
[9, 1, 0]]])
tf.matrix_diag(diagonals, k = (-1, 0))
==> [[[1, 0, 0], # Output shape: (2, 3, 3)
[4, 2, 0],
[0, 5, 3]],
[[6, 0, 0],
[9, 7, 0],
[0, 1, 9]]]
# Rectangular matrix.
diagonal = np.array([1, 2]) # Input shape: (2)
tf.matrix_diag(diagonal, k = -1, num_rows = 3, num_cols = 4)
==> [[0, 0, 0, 0], # Output shape: (3, 4)
[1, 0, 0, 0],
[0, 2, 0, 0]]
# Rectangular matrix with inferred num_cols and padding = 9.
tf.matrix_diag(diagonal, k = -1, num_rows = 3, padding = 9)
==> [[9, 9], # Output shape: (3, 2)
[1, 9],
[9, 2]]
Args |
diagonal
|
A Tensor with rank k >= 1 .
|
name
|
A name for the operation (optional).
|
k
|
Diagonal offset(s). Positive value means superdiagonal, 0 refers to the
main diagonal, and negative value means subdiagonals. k can be a single
integer (for a single diagonal) or a pair of integers specifying the low
and high ends of a matrix band. k[0] must not be larger than k[1] .
|
num_rows
|
The number of rows of the output matrix. If it is not provided,
the op assumes the output matrix is a square matrix and infers the matrix
size from d_lower , d_upper , and the innermost dimension of diagonal .
|
num_cols
|
The number of columns of the output matrix. If it is not provided,
the op assumes the output matrix is a square matrix and infers the matrix
size from d_lower , d_upper , and the innermost dimension of diagonal .
|
padding_value
|
The value to fill the area outside the specified diagonal
band with. Default is 0.
|
Returns |
A Tensor. Has the same type as diagonal .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.linalg.diag\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/linalg/diag) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/array_ops.py#L1955-L2089) |\n\nReturns a batched diagonal tensor with given batched diagonal values.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.linalg.diag`](/api_docs/python/tf/linalg/diag), [`tf.compat.v1.matrix_diag`](/api_docs/python/tf/linalg/diag)\n\n\u003cbr /\u003e\n\n tf.linalg.diag(\n diagonal, name='diag', k=0, num_rows=-1, num_cols=-1, padding_value=0\n )\n\nReturns a tensor with the contents in `diagonal` as `k[0]`-th to `k[1]`-th\ndiagonals of a matrix, with everything else padded with `padding`. `num_rows`\nand `num_cols` specify the dimension of the innermost matrix of the output. If\nboth are not specified, the op assumes the innermost matrix is square and\ninfers its size from `k` and the innermost dimension of `diagonal`. If only\none of them is specified, the op assumes the unspecified value is the smallest\npossible based on other criteria.\n\nLet `diagonal` have `r` dimensions `[I, J, ..., L, M, N]`. The output tensor\nhas rank `r+1` with shape `[I, J, ..., L, M, num_rows, num_cols]` when only\none diagonal is given (`k` is an integer or `k[0] == k[1]`). Otherwise, it has\nrank `r` with shape `[I, J, ..., L, num_rows, num_cols]`.\n\nThe second innermost dimension of `diagonal` has double meaning. When `k` is\nscalar or `k[0] == k[1]`, `M` is part of the batch size \\[I, J, ..., M\\], and\nthe output tensor is: \n\n output[i, j, ..., l, m, n]\n = diagonal[i, j, ..., l, n-max(d_upper, 0)] ; if n - m == d_upper\n output[i, j, ..., l, m, n] ; otherwise\n\nOtherwise, `M` is treated as the number of diagonals for the matrix in the\nsame batch (`M = k[1]-k[0]+1`), and the output tensor is: \n\n output[i, j, ..., l, m, n]\n = diagonal[i, j, ..., l, k[1]-d, n-max(d, 0)] ; if d_lower \u003c= d \u003c= d_upper\n input[i, j, ..., l, m, n] ; otherwise\n\nwhere `d = n - m`\n\n#### For example:\n\n # The main diagonal.\n diagonal = np.array([[1, 2, 3, 4], # Input shape: (2, 4)\n [5, 6, 7, 8]])\n tf.matrix_diag(diagonal) ==\u003e [[[1, 0, 0, 0], # Output shape: (2, 4, 4)\n [0, 2, 0, 0],\n [0, 0, 3, 0],\n [0, 0, 0, 4]],\n [[5, 0, 0, 0],\n [0, 6, 0, 0],\n [0, 0, 7, 0],\n [0, 0, 0, 8]]]\n\n # A superdiagonal (per batch).\n diagonal = np.array([[1, 2, 3], # Input shape: (2, 3)\n [4, 5, 6]])\n tf.matrix_diag(diagonal, k = 1)\n ==\u003e [[[0, 1, 0, 0], # Output shape: (2, 4, 4)\n [0, 0, 2, 0],\n [0, 0, 0, 3],\n [0, 0, 0, 0]],\n [[0, 4, 0, 0],\n [0, 0, 5, 0],\n [0, 0, 0, 6],\n [0, 0, 0, 0]]]\n\n # A band of diagonals.\n diagonals = np.array([[[1, 2, 3], # Input shape: (2, 2, 3)\n [4, 5, 0]],\n [[6, 7, 9],\n [9, 1, 0]]])\n tf.matrix_diag(diagonals, k = (-1, 0))\n ==\u003e [[[1, 0, 0], # Output shape: (2, 3, 3)\n [4, 2, 0],\n [0, 5, 3]],\n [[6, 0, 0],\n [9, 7, 0],\n [0, 1, 9]]]\n\n # Rectangular matrix.\n diagonal = np.array([1, 2]) # Input shape: (2)\n tf.matrix_diag(diagonal, k = -1, num_rows = 3, num_cols = 4)\n ==\u003e [[0, 0, 0, 0], # Output shape: (3, 4)\n [1, 0, 0, 0],\n [0, 2, 0, 0]]\n\n # Rectangular matrix with inferred num_cols and padding = 9.\n tf.matrix_diag(diagonal, k = -1, num_rows = 3, padding = 9)\n ==\u003e [[9, 9], # Output shape: (3, 2)\n [1, 9],\n [9, 2]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `diagonal` | A `Tensor` with `rank k \u003e= 1`. |\n| `name` | A name for the operation (optional). |\n| `k` | Diagonal offset(s). Positive value means superdiagonal, 0 refers to the main diagonal, and negative value means subdiagonals. `k` can be a single integer (for a single diagonal) or a pair of integers specifying the low and high ends of a matrix band. `k[0]` must not be larger than `k[1]`. |\n| `num_rows` | The number of rows of the output matrix. If it is not provided, the op assumes the output matrix is a square matrix and infers the matrix size from `d_lower`, `d_upper`, and the innermost dimension of `diagonal`. |\n| `num_cols` | The number of columns of the output matrix. If it is not provided, the op assumes the output matrix is a square matrix and infers the matrix size from `d_lower`, `d_upper`, and the innermost dimension of `diagonal`. |\n| `padding_value` | The value to fill the area outside the specified diagonal band with. Default is 0. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A Tensor. Has the same type as `diagonal`. ||\n\n\u003cbr /\u003e"]]