elementwise_mod

  • paddle.fluid.layers.elementwise_mod(x, y, axis=-1, act=None, name=None)[源代码]

该OP是逐元素取模算子,输入 x 与输入 y 逐元素取模,并将各个位置的输出元素保存到返回结果中。

等式为:

elementwise_mod - 图1

  • elementwise_mod - 图2 :多维Tensor。
  • elementwise_mod - 图3 :维度必须小于等于X维度的Tensor。
  • 对于这个运算算子有2种情况:
  • elementwise_mod - 图4shapeelementwise_mod - 图5 相同。
  • elementwise_mod - 图6shapeelementwise_mod - 图7 的连续子序列。
  • 对于情况2:
  • elementwise_mod - 图8 匹配 elementwise_mod - 图9 的形状(shape),其中 axiselementwise_mod - 图10elementwise_mod - 图11 上的起始维度的位置。
  • 如果 axis 为-1(默认值),则 elementwise_mod - 图12
  • 考虑到子序列, elementwise_mod - 图13 的大小为1的尾部维度将被忽略,例如shape(Y)=(2,1)=>(2)。

例如:

  1. shape(X) = (2, 3, 4, 5), shape(Y) = (,)
  2. shape(X) = (2, 3, 4, 5), shape(Y) = (5,)
  3. shape(X) = (2, 3, 4, 5), shape(Y) = (4, 5), with axis=-1(default) or axis=2
  4. shape(X) = (2, 3, 4, 5), shape(Y) = (3, 4), with axis=1
  5. shape(X) = (2, 3, 4, 5), shape(Y) = (2), with axis=0
  6. shape(X) = (2, 3, 4, 5), shape(Y) = (2, 1), with axis=0
  • 参数:
    • x (Variable)- 多维 TensorLoDTensor 。数据类型为 float32float64int32int64
    • y (Variable)- 多维 TensorLoDTensor 。数据类型为 float32float64int32int64
    • axis (int32,可选)- y 的维度对应到 x 维度上时的索引。默认值为 -1。
    • act (str,可选)- 激活函数名称,作用于输出上。详细请参考 激活函数 , 常见的激活函数有: relu tanh sigmoid 等。如果为None则不添加激活函数。默认值为None。
    • name (str,可选)- 输出的名字。默认值为None。该参数供开发人员打印调试信息时使用,具体用法请参见 Name

返回: 维度与 x 相同的 TensorLoDTensor ,数据类型与 x 相同。

返回类型: Variable。

代码示例 1

  1. import paddle.fluid as fluid
  2. import numpy as np
  3. def gen_data():
  4. return {
  5. "x": np.array([2, 3, 4]),
  6. "y": np.array([1, 5, 2])
  7. }
  8.  
  9. x = fluid.data(name="x", shape=[3], dtype='int64')
  10. y = fluid.data(name="y", shape=[3], dtype='int64')
  11. z = fluid.layers.elementwise_mod(x, y)
  12. place = fluid.CPUPlace()
  13. exe = fluid.Executor(place)
  14. z_value = exe.run(feed=gen_data(), fetch_list=[z.name])
  15. print(z_value) #[0,3,0]

代码示例 2

  1. import paddle.fluid as fluid
  2. import numpy as np
  3. def gen_data():
  4. return {
  5. "x": np.random.randint(1, 5, size=[2, 3, 4, 5]),
  6. "y": np.random.randint(1, 5, size=[3, 4])
  7. }
  8.  
  9. x = fluid.data(name="x", shape=[2,3,4,5], dtype='int64')
  10. y = fluid.data(name="y", shape=[3,4], dtype='int64')
  11. z = fluid.layers.elementwise_mod(x, y, axis=1)
  12. place = fluid.CPUPlace()
  13. exe = fluid.Executor(place)
  14. z_value = exe.run(feed=gen_data(),
  15. fetch_list=[z.name])
  16. print(z_value) # z.shape=[2,3,4,5]

代码示例 3

  1. import paddle.fluid as fluid
  2. import numpy as np
  3. def gen_data():
  4. return {
  5. "x": np.random.randint(1, 5, size=[2, 3, 4, 5]),
  6. "y": np.random.randint(1, 5, size=[5])
  7. }
  8.  
  9. x = fluid.data(name="x", shape=[2,3,4,5], dtype='int64')
  10. y = fluid.data(name="y", shape=[5], dtype='int64')
  11. z = fluid.layers.elementwise_mod(x, y, axis=3)
  12. place = fluid.CPUPlace()
  13. exe = fluid.Executor(place)
  14. z_value = exe.run(feed=gen_data(),
  15. fetch_list=[z.name])
  16. print(z_value) # z.shape=[2,3,4,5]