megengine.functional.mod¶
- mod(x, y)[源代码]¶
Element-wise \(\operatorname{mod}(x, y)\) function.
Returns the remainder of division for each element \(x_i\) of the input tensor \(x\) and the respective element \(y_i\) of the input tensor \(y\).
注解
In general, similar to Python’s % operator, this function is not recommended for floating-point operands as semantics do not follow IEEE 754. That this function is specified to accept floating-point operands is primarily for reasons of backward compatibility.
mod
是 NumPy 中remainder
的别名。
- 参数
- 返回类型
- 返回
包含逐元素结果的张量。 返回的张量必须具有由 类型提升规则 确定的数据类型。
Special cases
For floating-point operands,
If either \(x_i\) or \(y_i\) is
NaN
, the result isNaN
.If \(x_i\) is either
+infinity
or-infinity
and \(y_i\) is either+infinity
or-infinity
, the result isNaN
.If \(x_i\) is either
+0
or-0
and \(y_i\) is either+0
or-0
, the result isNaN
.If \(x_i\) is
+0
and \(y_i\) is greater than 0, the result is+0
.If \(x_i\) is
-0
and \(y_i\) is greater than 0, the result is+0
.If \(x_i\) is
+0
and \(y_i\) is less than 0, the result is-0
.If \(x_i\) is
-0
and \(y_i\) is less than 0, the result is-0
.If \(x_i\) is greater than
0
and \(y_i\) is+0
, the result isNaN
.If \(x_i\) is greater than
0
and \(y_i\) is-0
, the result isNaN
.If \(x_i\) is less than
0
and \(y_i\) is+0
, the result isNaN
.If \(x_i\) is less than
0
and \(y_i\) is-0
, the result isNaN
.If \(x_i\) is
+infinity
and \(y_i\) is a positive (i.e., greater than 0) finite number, the result isNaN
.If \(x_i\) is
+infinity
and \(y_i\) is a negative (i.e., less than 0) finite number, the result isNaN
.If \(x_i\) is
-infinity
and \(y_i\) is a positive (i.e., greater than 0) finite number, the result isNaN
.If \(x_i\) is
-infinity
and \(y_i\) is a negative (i.e., less than 0) finite number, the result isNaN
.If \(x_i\) is a positive (i.e., greater than
0
) finite number and \(y_i\) is+infinity
, the result is \(x_i\). (note: this result matches Python behavior.)If \(x_i\) is a positive (i.e., greater than
0
) finite number and \(y_i\) is-infinity
, the result is \(y_i\). (note: this result matches Python behavior.)If \(x_i\) is a negative (i.e., less than
0
) finite number and \(y_i\) is+infinity
, the result is \(y_i\). (note: this results matches Python behavior.)If \(x_i\) is a negative (i.e., less than
0
) finite number and \(y_i\) is-infinity
, the result is \(x_i\). (note: this result matches Python behavior.)In the remaining cases, the result must match that of the Python
%
operator.
实际案例
>>> F.mod(8, 3) Tensor(2, dtype=int32, device=xpux:0)
Element-wise mod:
>>> x = Tensor([1, 2, 3, 4, 5]) >>> y = Tensor([1, 2, 1, 2, 1]) >>> F.mod(x, y) Tensor([0 0 0 0 0], dtype=int32, device=xpux:0)
Broadcasting:
>>> x = Tensor([1, 2, 3, 4, 5]) >>> F.mod(x, 3) Tensor([1 2 0 1 2], dtype=int32, device=xpux:0)