You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

math.py 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. from typing import Optional
  10. import megengine._internal as mgb
  11. from ..core import Tensor, wrap_io_tensor
  12. @wrap_io_tensor
  13. def sum(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  14. r"""Returns the sum of each row of the ``inp`` tensor in the given ``axis``.
  15. :param inp: The input tensor.
  16. :param axis: The dimension to reduce. If None, all the dimensions will be reduced.
  17. Default: None
  18. :param keepdims: Whether the output tensor has ``axis`` retained or not.
  19. Default: False
  20. :return: The output tensor
  21. Examples:
  22. .. testcode::
  23. import numpy as np
  24. from megengine import tensor
  25. import megengine.functional as F
  26. data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  27. out = F.sum(data)
  28. print(out.numpy())
  29. .. testoutput::
  30. [21]
  31. """
  32. return mgb.opr.reduce_(inp, "SUM", axis, keepdims)
  33. @wrap_io_tensor
  34. def prod(inp: Tensor, axis: Optional[int] = None, keepdims=False) -> Tensor:
  35. r"""
  36. Returns prod of input tensor along given *axis*.
  37. :param inp: The input tensor
  38. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: ``None``
  39. :param keepdims: Whether the output tensor has *axis* retained or not. Default: ``False``
  40. :return: The output tensor
  41. Examples:
  42. .. testcode::
  43. import numpy as np
  44. from megengine import tensor
  45. import megengine.functional as F
  46. data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  47. out = F.prod(data)
  48. print(out.numpy())
  49. Outputs:
  50. .. testoutput::
  51. [720]
  52. """
  53. return mgb.opr.reduce_(inp, "PRODUCT", axis, keepdims)
  54. @wrap_io_tensor
  55. def mean(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  56. """Returns the mean value of each row of the ``inp`` tensor in
  57. the given ``axis``. If axis is a list of dimensions,
  58. reduce over all of them.
  59. :param inp: The input tensor
  60. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  61. :param keepdims: Whether the output tensor has ``axis`` retained or not. Default: False
  62. Examples:
  63. .. testcode::
  64. import numpy as np
  65. from megengine import tensor
  66. import megengine.functional as F
  67. data = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3))
  68. out = F.mean(data)
  69. print(out.numpy())
  70. .. testoutput::
  71. [3.5]
  72. """
  73. return mgb.opr.mean(inp, axis, keepdims)
  74. @wrap_io_tensor
  75. def min(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  76. r"""
  77. Returns the min value of input tensor along given *axis*.
  78. :param inp: The input tensor
  79. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  80. :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
  81. :return: The output tensor
  82. Examples:
  83. .. testcode::
  84. import numpy as np
  85. from megengine import tensor
  86. import megengine.functional as F
  87. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  88. y = F.min(x)
  89. print(y.numpy())
  90. Outputs:
  91. .. testoutput::
  92. [1]
  93. """
  94. return mgb.opr.reduce_(inp, "MIN", axis, keepdims)
  95. @wrap_io_tensor
  96. def max(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  97. r"""Returns the max value of the input tensor along given *axis*.
  98. :param inp: The input tensor
  99. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  100. :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
  101. :return: The output tensor
  102. Examples:
  103. .. testcode::
  104. import numpy as np
  105. from megengine import tensor
  106. import megengine.functional as F
  107. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  108. y = F.max(x)
  109. print(y.numpy())
  110. .. testoutput::
  111. [6]
  112. """
  113. return mgb.opr.reduce_(inp, "MAX", axis, keepdims)
  114. @wrap_io_tensor
  115. def sqrt(inp: Tensor) -> Tensor:
  116. """
  117. Return a new tensor with the square-root of the elements of ``inp``
  118. :param inp: The input tensor
  119. :return: The computed tensor
  120. Examples:
  121. .. testcode::
  122. import numpy as np
  123. import megengine as mge
  124. import megengine.functional as F
  125. data = mge.tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3))
  126. out = F.sqrt(data)
  127. print(out.numpy())
  128. Outputs:
  129. .. testoutput::
  130. [[0. 1. 1.4142]
  131. [1.7321 2. 2.2361 ]]
  132. """
  133. return mgb.opr.sqrt(inp)
  134. @wrap_io_tensor
  135. def norm(inp: Tensor, p=2, axis: Optional[int] = None, keepdims=False):
  136. """Calculate ``p``-norm of input tensor along certain axis.
  137. :param inp: The input tensor
  138. :param p: power of value ``p`` applied to ``inp``. Default: 2
  139. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  140. :param keepdims: Whether the output tensor has ``axis`` retained or not. Default: False
  141. :return: The output tensor
  142. """
  143. if axis is None:
  144. inp = inp.reshape(-1)
  145. return (inp ** p).sum(axis=axis, keepdims=keepdims) ** (1.0 / p)
  146. @wrap_io_tensor
  147. def argmin(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  148. r"""Returns the indices of the minimum values along an axis
  149. :param inp: The input tensor
  150. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  151. :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
  152. :return: The output tensor
  153. Examples:
  154. .. testcode::
  155. import numpy as np
  156. from megengine import tensor
  157. import megengine.functional as F
  158. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  159. y = F.argmin(x)
  160. print(y.numpy())
  161. .. testoutput::
  162. [0]
  163. """
  164. return mgb.opr.argmin(inp, axis, keepdims)
  165. @wrap_io_tensor
  166. def argmax(inp: Tensor, axis: Optional[int] = None, keepdims: bool = False) -> Tensor:
  167. r"""Returns the indices of the maximum values along an axis
  168. :param inp: The input tensor
  169. :param axis: The dimension to reduce. If None, all the dimensions will be reduced. Default: None
  170. :param keepdims: Whether the output tensor has *axis* retained or not. Default: False
  171. :return: The output tensor
  172. Examples:
  173. .. testcode::
  174. import numpy as np
  175. from megengine import tensor
  176. import megengine.functional as F
  177. x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3))
  178. y = F.argmax(x)
  179. print(y.numpy())
  180. .. testoutput::
  181. [5]
  182. """
  183. return mgb.opr.argmax(inp, axis, keepdims)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台

Contributors (1)