|
19 | 19 | # pylint: disable=wildcard-import, unused-wildcard-import,redefined-outer-name
|
20 | 20 | """Contrib NDArray API of MXNet."""
|
21 | 21 | import math
|
| 22 | +import numpy as np |
22 | 23 | from ..context import current_context
|
23 | 24 | from ..random import uniform
|
24 | 25 | from ..base import _as_list
|
|
28 | 29 | except ImportError:
|
29 | 30 | pass
|
30 | 31 |
|
31 |
| -__all__ = ["rand_zipfian", "foreach", "while_loop", "cond"] |
| 32 | +__all__ = ["rand_zipfian", "foreach", "while_loop", "cond", "isinf", "isfinite", "isnan"] |
32 | 33 |
|
33 | 34 | # pylint: disable=line-too-long
|
34 | 35 | def rand_zipfian(true_classes, num_sampled, range_max, ctx=None):
|
@@ -460,3 +461,84 @@ def _to_python_scalar(inputs, type_, name):
|
460 | 461 | return then_func()
|
461 | 462 | else:
|
462 | 463 | return else_func()
|
| 464 | + |
| 465 | +def isinf(data): |
| 466 | + """Performs an element-wise check to determine if the NDArray contains an infinite element |
| 467 | + or not. |
| 468 | +
|
| 469 | +
|
| 470 | + Parameters |
| 471 | + ---------- |
| 472 | + input : NDArray |
| 473 | + An N-D NDArray. |
| 474 | +
|
| 475 | + Returns |
| 476 | + ------- |
| 477 | + output: NDArray |
| 478 | + The output NDarray, with same shape as input, where 1 indicates the array element is |
| 479 | + equal to positive or negative infinity and 0 otherwise. |
| 480 | +
|
| 481 | + Examples |
| 482 | + -------- |
| 483 | + >>> data = mx.nd.array([np.inf, -np.inf, np.NINF, -1]) |
| 484 | + >>> output = mx.nd.contrib.isinf(data) |
| 485 | + >>> output |
| 486 | + [1. 1. 1. 0.] |
| 487 | + <NDArray 4 @cpu(0)> |
| 488 | + """ |
| 489 | + return data.abs() == np.inf |
| 490 | + |
| 491 | +def isfinite(data): |
| 492 | + """Performs an element-wise check to determine if the NDArray contains an infinite element |
| 493 | + or not. |
| 494 | +
|
| 495 | +
|
| 496 | + Parameters |
| 497 | + ---------- |
| 498 | + input : NDArray |
| 499 | + An N-D NDArray. |
| 500 | +
|
| 501 | + Returns |
| 502 | + ------- |
| 503 | + output: NDArray |
| 504 | + The output NDarray, with same shape as input, where 1 indicates the array element is |
| 505 | + finite i.e. not equal to positive or negative infinity and 0 in places where it is |
| 506 | + positive or negative infinity. |
| 507 | +
|
| 508 | + Examples |
| 509 | + -------- |
| 510 | + >>> data = mx.nd.array([np.inf, -np.inf, np.NINF, -1]) |
| 511 | + >>> output = mx.nd.contrib.isfinite(data) |
| 512 | + >>> output |
| 513 | + [0. 0. 0. 1.] |
| 514 | + <NDArray 4 @cpu(0)> |
| 515 | + """ |
| 516 | + is_data_not_nan = data == data |
| 517 | + is_data_not_infinite = data.abs() != np.inf |
| 518 | + return ndarray.logical_and(is_data_not_infinite, is_data_not_nan) |
| 519 | + |
| 520 | +def isnan(data): |
| 521 | + """Performs an element-wise check to determine if the NDArray contains a NaN element |
| 522 | + or not. |
| 523 | +
|
| 524 | +
|
| 525 | + Parameters |
| 526 | + ---------- |
| 527 | + input : NDArray |
| 528 | + An N-D NDArray. |
| 529 | +
|
| 530 | + Returns |
| 531 | + ------- |
| 532 | + output: NDArray |
| 533 | + The output NDarray, with same shape as input, where 1 indicates the array element is |
| 534 | + NaN i.e. Not a Number and 0 otherwise. |
| 535 | +
|
| 536 | + Examples |
| 537 | + -------- |
| 538 | + >>> data = mx.nd.array([np.nan, -1]) |
| 539 | + >>> output = mx.nd.contrib.isnan(data) |
| 540 | + >>> output |
| 541 | + [1. 0.] |
| 542 | + <NDArray 2 @cpu(0)> |
| 543 | + """ |
| 544 | + return data != data |
0 commit comments