From 98d6ba7e2f4c148a5e43daab7712437af528bea4 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Thu, 18 Feb 2021 14:56:14 +0800 Subject: [PATCH] refactor(mge/functional): move matinv from nn to math GitOrigin-RevId: 75c48ce32729ea82fbc86e2f7dc0427709c2e978 --- imperative/python/megengine/functional/math.py | 33 ++++++++++++++++++++++++++ imperative/python/megengine/functional/nn.py | 33 -------------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/imperative/python/megengine/functional/math.py b/imperative/python/megengine/functional/math.py index 0053a382..f93e5cee 100644 --- a/imperative/python/megengine/functional/math.py +++ b/imperative/python/megengine/functional/math.py @@ -29,6 +29,7 @@ __all__ = [ "dot", "isinf", "isnan", + "matinv", "matmul", "max", "mean", @@ -729,6 +730,38 @@ def topk( return tns, ind +def matinv(inp: Tensor) -> Tensor: + """ + Computes the inverse of a batch of matrices; input must has shape [..., n, n]. + + :param inp: input tensor. + :return: output tensor. + + Examples: + + .. testcode:: + + import numpy as np + from megengine import tensor + import megengine.functional as F + + data = tensor([[1.0, 0.0], [1.0, 1.0]]) + out = F.matinv(data) + print(out.numpy()) + + Outputs: + + .. testoutput:: + + [[ 1. 0.] + [-1. 1.]] + + """ + + (result,) = apply(builtin.MatrixInverse(), inp) + return result + + def matmul( inp1: Tensor, inp2: Tensor, diff --git a/imperative/python/megengine/functional/nn.py b/imperative/python/megengine/functional/nn.py index fb6d4ae5..91f484ce 100644 --- a/imperative/python/megengine/functional/nn.py +++ b/imperative/python/megengine/functional/nn.py @@ -53,7 +53,6 @@ __all__ = [ "logsigmoid", "logsumexp", "logsoftmax", - "matinv", "max_pool2d", "one_hot", "prelu", @@ -1183,38 +1182,6 @@ def remap( return result -def matinv(inp: Tensor) -> Tensor: - """ - Computes the inverse of a batch of matrices; input must has shape [..., n, n]. - - :param inp: input tensor. - :return: output tensor. - - Examples: - - .. testcode:: - - import numpy as np - from megengine import tensor - import megengine.functional as F - - data = tensor([[1.0, 0.0], [1.0, 1.0]]) - out = F.matinv(data) - print(out.numpy()) - - Outputs: - - .. testoutput:: - - [[ 1. 0.] - [-1. 1.]] - - """ - - (result,) = apply(builtin.MatrixInverse(), inp) - return result - - def interpolate( inp: Tensor, size: Optional[Union[int, Tuple[int, int]]] = None,