r"""Computes the norm of vectors, matrices, and tensors. This function can compute several different vector norms (the 1-norm, the Euclidean or 2-norm, the inf-norm, and in general the p-norm for p > 0) and matrix norms (Frobenius, 1-norm, 2-norm and inf-norm). Args: tensor: `Tensor` o
(tensor,
ord='euclidean',
axis=None,
keepdims=None,
name=None)
| 425 | # pylint: disable=redefined-builtin |
| 426 | @tf_export('norm', 'linalg.norm', v1=[]) |
| 427 | def norm_v2(tensor, |
| 428 | ord='euclidean', |
| 429 | axis=None, |
| 430 | keepdims=None, |
| 431 | name=None): |
| 432 | r"""Computes the norm of vectors, matrices, and tensors. |
| 433 | |
| 434 | This function can compute several different vector norms (the 1-norm, the |
| 435 | Euclidean or 2-norm, the inf-norm, and in general the p-norm for p > 0) and |
| 436 | matrix norms (Frobenius, 1-norm, 2-norm and inf-norm). |
| 437 | |
| 438 | Args: |
| 439 | tensor: `Tensor` of types `float32`, `float64`, `complex64`, `complex128` |
| 440 | ord: Order of the norm. Supported values are `'fro'`, `'euclidean'`, |
| 441 | `1`, `2`, `np.inf` and any positive real number yielding the corresponding |
| 442 | p-norm. Default is `'euclidean'` which is equivalent to Frobenius norm if |
| 443 | `tensor` is a matrix and equivalent to 2-norm for vectors. |
| 444 | Some restrictions apply: |
| 445 | a) The Frobenius norm `'fro'` is not defined for vectors, |
| 446 | b) If axis is a 2-tuple (matrix norm), only `'euclidean'`, '`fro'`, `1`, |
| 447 | `2`, `np.inf` are supported. |
| 448 | See the description of `axis` on how to compute norms for a batch of |
| 449 | vectors or matrices stored in a tensor. |
| 450 | axis: If `axis` is `None` (the default), the input is considered a vector |
| 451 | and a single vector norm is computed over the entire set of values in the |
| 452 | tensor, i.e. `norm(tensor, ord=ord)` is equivalent to |
| 453 | `norm(reshape(tensor, [-1]), ord=ord)`. |
| 454 | If `axis` is a Python integer, the input is considered a batch of vectors, |
| 455 | and `axis` determines the axis in `tensor` over which to compute vector |
| 456 | norms. |
| 457 | If `axis` is a 2-tuple of Python integers it is considered a batch of |
| 458 | matrices and `axis` determines the axes in `tensor` over which to compute |
| 459 | a matrix norm. |
| 460 | Negative indices are supported. Example: If you are passing a tensor that |
| 461 | can be either a matrix or a batch of matrices at runtime, pass |
| 462 | `axis=[-2,-1]` instead of `axis=None` to make sure that matrix norms are |
| 463 | computed. |
| 464 | keepdims: If True, the axis indicated in `axis` are kept with size 1. |
| 465 | Otherwise, the dimensions in `axis` are removed from the output shape. |
| 466 | name: The name of the op. |
| 467 | |
| 468 | Returns: |
| 469 | output: A `Tensor` of the same type as tensor, containing the vector or |
| 470 | matrix norms. If `keepdims` is True then the rank of output is equal to |
| 471 | the rank of `tensor`. Otherwise, if `axis` is none the output is a scalar, |
| 472 | if `axis` is an integer, the rank of `output` is one less than the rank |
| 473 | of `tensor`, if `axis` is a 2-tuple the rank of `output` is two less |
| 474 | than the rank of `tensor`. |
| 475 | |
| 476 | Raises: |
| 477 | ValueError: If `ord` or `axis` is invalid. |
| 478 | |
| 479 | @compatibility(numpy) |
| 480 | Mostly equivalent to numpy.linalg.norm. |
| 481 | Not supported: ord <= 0, 2-norm for matrices, nuclear norm. |
| 482 | Other differences: |
| 483 | a) If axis is `None`, treats the flattened `tensor` as a vector |
| 484 | regardless of rank. |