Paddle
Paddle copied to clipboard
为什么PaddlePaddle的where接口和numpy 的where接口不一样的?
我时间的是官网提供的dev安装包的
PaddlePaddle的where接口:
def where(condition):
"""
Return an int64 tensor with rank 2, specifying the coordinate of true element in `condition`.
Output's first dimension is the number of true element, second dimension is rank(number of dimension) of `condition`.
If there is zero true element, then an empty tensor will be generated.
Args:
condition(Variable): A bool tensor with rank at least 1.
Returns:
Variable: The tensor variable storing a 2-D tensor.
Examples:
.. code-block:: python
# condition is a tensor [True, False, True]
out = fluid.layers.where(condition) # [[0], [2]]
# condition is a tensor [[True, False], [False, True]]
out = fluid.layers.where(condition) # [[0, 0], [1, 1]]
# condition is a tensor [False, False, False]
out = fluid.layers.where(condition) # [[]]
"""
helper = LayerHelper("where", **locals())
out = helper.create_variable_for_type_inference(
dtype=core.VarDesc.VarType.INT64)
helper.append_op(
type='where', inputs={'Condition': condition}, outputs={'Out': [out]})
return out
numpy的where接口:
def where(condition, x=None, y=None):
"""
where(condition, [x, y])
Return elements chosen from `x` or `y` depending on `condition`.
.. note::
When only `condition` is provided, this function is a shorthand for
``np.asarray(condition).nonzero()``. Using `nonzero` directly should be
preferred, as it behaves correctly for subclasses. The rest of this
documentation covers only the case where all three arguments are
provided.
Parameters
----------
condition : array_like, bool
Where True, yield `x`, otherwise yield `y`.
x, y : array_like
Values from which to choose. `x`, `y` and `condition` need to be
broadcastable to some shape.
Returns
-------
out : ndarray
An array with elements from `x` where `condition` is True, and elements
from `y` elsewhere.
See Also
--------
choose
nonzero : The function that is called when x and y are omitted
Notes
-----
If all the arrays are 1-D, `where` is equivalent to::
[xv if c else yv
for c, xv, yv in zip(condition, x, y)]
Examples
--------
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.where(a < 5, a, 10*a)
array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
This can be used on multidimensional arrays too:
>>> np.where([[True, False], [True, True]],
... [[1, 2], [3, 4]],
... [[9, 8], [7, 6]])
array([[1, 8],
[3, 4]])
The shapes of x, y, and the condition are broadcast together:
>>> x, y = np.ogrid[:3, :4]
>>> np.where(x < y, x, 10 + y) # both x and 10+y are broadcast
array([[10, 0, 0, 0],
[10, 11, 1, 1],
[10, 11, 12, 2]])
>>> a = np.array([[0, 1, 2],
... [0, 2, 4],
... [0, 3, 6]])
>>> np.where(a < 4, a, -1) # -1 is broadcast
array([[ 0, 1, 2],
[ 0, 2, -1],
[ 0, 3, -1]])
"""
return (condition, x, y)
这个问题请设计op的同学帮忙回答下吧
@zhoukunsheng 如果我要实现一下这个,该如何使用?
zeros = tf.zeros_like(label)
# 只把pos的label设定为1,其余都为0
label_filter_invalid = tf.where(tf.less(label, 0), zeros, label)
当前还不支持这个功能,后续会迭代增加。如果急需该功能,可以使用fluid.layers.py_func实现。
@zhoukunsheng 目前我就是使用fluid.layers.py_func解决的。