reduce.py 15.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17 18 19 20
"""Reduce operators."""
# pylint: disable=redefined-builtin

from . import _make
21 22 23
from .tensor import sqrt
from .transform import squeeze
from ..expr import Tuple, TupleWrapper
24 25 26 27 28 29 30 31 32 33

def argmax(data, axis=None, keepdims=False, exclude=False):
    """Returns the indices of the maximum values along an axis.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
34 35
        Axis or axes along which a argmax operation is performed.
        The default, axis=None, will find the indices of the maximum element of the elements of
36 37 38 39 40 41 42 43 44
        the input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
45
        NOT in axis instead.
46 47 48 49 50 51

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
52
    axis = [axis] if isinstance(axis, int) else axis
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    return _make.argmax(data, axis, keepdims, exclude)

def argmin(data, axis=None, keepdims=False, exclude=False):
    """Returns the indices of the minimum values along an axis.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
        Axis or axes along which a argmin operation is performed.
        The default, axis=None, will find the indices of minimum element all of the elements of
        the input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
75
        NOT in axis instead.
76 77 78 79 80 81

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
82
    axis = [axis] if isinstance(axis, int) else axis
83
    return _make.argmin(data, axis, keepdims, exclude)
84 85 86 87 88 89 90 91 92 93 94


def sum(data, axis=None, keepdims=False, exclude=False):
    """Computes the sum of array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
95 96 97
        Axis or axes along which a sum is performed. The default, axis=None,
        will sum all of the elements of the input array. If axis is
        negative it counts from the last to the first axis.
98 99

    keepdims : bool
100 101 102
        If this is set to True, the axes which are reduced are left in the result as
        dimensions with size one. With this option, the result will broadcast
        correctly against the input array.
103 104 105

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
106
        NOT in axis instead.
107 108 109 110 111 112

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
113
    axis = [axis] if isinstance(axis, int) else axis
114 115 116
    return _make.sum(data, axis, keepdims, exclude)


117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
def all(data, axis=None, keepdims=False, exclude=False):
    """Computes the logical AND of boolean array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input boolean tensor

    axis : None or int or tuple of int
        Axis or axes along which a sum is performed. The default, axis=None,
        will sum all of the elements of the input array. If axis is
        negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as
        dimensions with size one. With this option, the result will broadcast
        correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.

    Examples
    --------
    .. code-block:: python

    data = relay.Constant(tvm.nd.array([[[ True,  True,  True],
                                         [ True,  True,  True],
                                         [False,  True, False]],
                                        [[ True, False, False],
                                         [ True,  True, False],
                                         [False,  True,  True]]]))

    relay.all(data, axis=1)
    # [[False,  True, False],
    # [False, False, False]]

    relay.all(data, axis=0)
    # [[ True, False, False],
    # [ True,  True, False],
    # [False,  True, False]]

    """
165
    axis = [axis] if isinstance(axis, int) else axis
166 167 168
    return _make.all(data, axis, keepdims, exclude)


169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
def any(data, axis=None, keepdims=False, exclude=False):
    """Computes the logical OR of boolean array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input boolean tensor

    axis : None or int or tuple of int
        Axis or axes along which a sum is performed. The default, axis=None,
        will sum all of the elements of the input array. If axis is
        negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as
        dimensions with size one. With this option, the result will broadcast
        correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.

    Examples
    --------
    .. code-block:: python

    data = relay.Constant(tvm.nd.array([[[ True,  True,  True],
                                         [ True,  True,  True],
                                         [False,  True, False]],
                                        [[ True, False, False],
                                         [ True,  True, False],
                                         [False,  True,  True]]]))

    relay.any(data, axis=1)
    # [[True, True, True],
    # [True,  True, True]]

    relay.any(data, axis=0)
    # [[ True, True, True],
    # [ True,  True, True],
    # [False,  True, True]]

    """
    axis = [axis] if isinstance(axis, int) else axis
    return _make.any(data, axis, keepdims, exclude)


221 222 223 224 225 226 227 228 229
def max(data, axis=None, keepdims=False, exclude=False):
    """ Computes the max of array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
230 231 232
        Axis or axes along which the max operation is performed.
        The default, axis=None, will find the max element from all of the elements of the input
        array. If axis is negative it counts from the last to the first axis.
233 234 235 236 237 238 239 240

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
241
        NOT in axis instead.
242 243 244 245 246 247

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
248
    axis = [axis] if isinstance(axis, int) else axis
249 250 251 252 253 254 255 256 257 258 259 260
    return _make.max(data, axis, keepdims, exclude)


def min(data, axis=None, keepdims=False, exclude=False):
    """Computes the min of array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
261 262 263 264
        Axis or axes along which a minimum operation is performed.
        The default, axis=None, will find the minimum element from all
        of the elements of the input array. If axis is negative it counts from
        the last to the first axis.
265 266 267 268 269 270 271 272

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
273
        NOT in axis instead.
274 275 276 277 278 279

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
280
    axis = [axis] if isinstance(axis, int) else axis
281 282 283 284 285 286 287 288 289 290 291 292
    return _make.min(data, axis, keepdims, exclude)


def mean(data, axis=None, keepdims=False, exclude=False):
    """Computes the mean of array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
293
        Axis or axes along which a mean operation is performed.
294 295
        The default, axis=None, will compute the mean of all elements in the input array.
        If axis is negative it counts from the last to the first axis.
296 297 298 299 300 301 302 303

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
304
        NOT in axis instead.
305 306 307 308 309 310

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
311
    axis = [axis] if isinstance(axis, int) else axis
312 313 314
    return _make.mean(data, axis, keepdims, exclude)


315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
def variance(data, axis=None, keepdims=False, exclude=False):
    """Computes the variance of data over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
        Axis or axes along which a variance operation is performed.
        The default, axis=None, will compute the variance of all elements in the input array.
        If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
    axis = [axis] if isinstance(axis, int) else axis
    m = mean(data, axis, True, exclude)
    return _make._variance(data, m, axis, keepdims, exclude)


def std(data, axis=None, keepdims=False, exclude=False):
    """Computes the standard deviation of data over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
        Axis or axes along which a standard deviation operation is performed.
        The default, axis=None, will compute the standard deviation of all elements in the
        input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
    axis = [axis] if isinstance(axis, int) else axis
    m = mean(data, axis, True, exclude)
    return sqrt(_make._variance(data, m, axis, keepdims, exclude))


def mean_variance(data, axis=None, keepdims=False, exclude=False):
    """Computes the mean and variance of data over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
        Axis or axes along which a mean and variance operation is performed.
        The default, axis=None, will compute the mean and variance of all elements in
        the input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
    axis = [axis] if isinstance(axis, int) else axis
    m = mean(data, axis, True, exclude)
    var = _make._variance(data, m, axis, keepdims, exclude)
    if not keepdims:
        m = squeeze(m)
    return TupleWrapper(Tuple((m, var)), 2)


def mean_std(data, axis=None, keepdims=False, exclude=False):
    """Computes the mean and standard deviation of data over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
        Axis or axes along which a mean and standard deviation operation is performed.
        The default, axis=None, will compute the mean and standard deviation of all elements in
        the input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
        NOT in axis instead.

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
    axis = [axis] if isinstance(axis, int) else axis
    m = mean(data, axis, True, exclude)
    s = sqrt(_make._variance(data, m, axis, keepdims, exclude))
    if not keepdims:
        m = squeeze(m)
    return TupleWrapper(Tuple((m, s)), 2)


449 450 451 452 453 454 455 456 457
def prod(data, axis=None, keepdims=False, exclude=False):
    """Computes the products of array elements over given axes.

    Parameters
    ----------
    data : relay.Expr
        The input data

    axis : None or int or tuple of int
458
        Axis or axes along which a product is performed.
459 460 461 462 463 464 465 466 467 468
        The default, axis=None, will find the indices of minimum element all of the elements of
        the input array. If axis is negative it counts from the last to the first axis.

    keepdims : bool
        If this is set to True, the axes which are reduced are left in the result as dimensions
        with size one.
        With this option, the result will broadcast correctly against the input array.

    exclude : bool
        If `exclude` is true, reduction will be performed on the axes that are
469
        NOT in axis instead.
470 471 472 473 474 475

    Returns
    -------
    result : relay.Expr
        The computed result.
    """
476
    axis = [axis] if isinstance(axis, int) else axis
477
    return _make.prod(data, axis, keepdims, exclude)