elementwise_max

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

该OP逐元素对比输入的两个多维Tensor,并且把各个位置更大的元素保存到返回结果中。

等式是:

elementwise_max - 图1

  • elementwise_max - 图2 :多维Tensor。
  • elementwise_max - 图3 :多维Tensor。
  • 此运算算子有两种情况:
  • elementwise_max - 图4shapeelementwise_max - 图5 相同。
  • elementwise_max - 图6shapeelementwise_max - 图7 的连续子序列。
  • 对于情况2:
  • elementwise_max - 图8shape 匹配 elementwise_max - 图9shape,其中 axiselementwise_max - 图10elementwise_max - 图11 上的起始维度的位置。
  • 如果 axis 为-1(默认值),则 elementwise_max - 图12
  • 考虑到子序列, elementwise_max - 图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)- 多维Tensor。数据类型为 float32float64int32int64
    • y (Variable)- 多维Tensor。数据类型为 float32float64int32int64
    • axis (int32, 可选)- Y的维度对应到X维度上时的索引。默认值为 -1。
    • act (string, 可选)- 激活函数名称,作用于输出上。默认值为None。详细请参考 激活函数 , 常见的激活函数有: relu tanh sigmoid 等。
    • name (string, 可选)- 输出的名字。默认值为None。该参数供开发人员打印调试信息时使用,具体用法请参见 Name

返回: 维度和数据类型与 x 相同的多维Tensor。

返回类型: 多维Tensor。

代码示例 1

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

代码示例 2

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