提交 175b847f 编写于 作者: M minqiyang

Add API example for logical ops and clip ops

test=develop
上级 1d9b2a45
...@@ -726,11 +726,11 @@ def dynamic_gru(input, ...@@ -726,11 +726,11 @@ def dynamic_gru(input,
create ParamAttr as param_attr. If the Initializer of the param_attr create ParamAttr as param_attr. If the Initializer of the param_attr
is not set, the parameter is initialized with Xavier. Default: None. is not set, the parameter is initialized with Xavier. Default: None.
bias_attr (ParamAttr|bool|None): The parameter attribute for the bias bias_attr (ParamAttr|bool|None): The parameter attribute for the bias
of GRU. Note that the bias with :math:`(1 \\times 3D)` concatenates of GRU. Note that the bias with :math:`(1 \\times 3D)` concatenates
the bias in the update gate, reset gate and candidate calculations. the bias in the update gate, reset gate and candidate calculations.
If it is set to False, no bias will be applied to the update gate, If it is set to False, no bias will be applied to the update gate,
reset gate and candidate calculations. If it is set to None or one reset gate and candidate calculations. If it is set to None or one
attribute of ParamAttr, dynamic_gru will create ParamAttr as attribute of ParamAttr, dynamic_gru will create ParamAttr as
bias_attr. If the Initializer of the bias_attr is not set, the bias bias_attr. If the Initializer of the bias_attr is not set, the bias
is initialized zero. Default: None. is initialized zero. Default: None.
is_reverse(bool): Whether to compute reversed GRU, default is_reverse(bool): Whether to compute reversed GRU, default
...@@ -847,11 +847,11 @@ def gru_unit(input, ...@@ -847,11 +847,11 @@ def gru_unit(input,
create ParamAttr as param_attr. If the Initializer of the param_attr create ParamAttr as param_attr. If the Initializer of the param_attr
is not set, the parameter is initialized with Xavier. Default: None. is not set, the parameter is initialized with Xavier. Default: None.
bias_attr (ParamAttr|bool|None): The parameter attribute for the bias bias_attr (ParamAttr|bool|None): The parameter attribute for the bias
of GRU. Note that the bias with :math:`(1 \\times 3D)` concatenates of GRU. Note that the bias with :math:`(1 \\times 3D)` concatenates
the bias in the update gate, reset gate and candidate calculations. the bias in the update gate, reset gate and candidate calculations.
If it is set to False, no bias will be applied to the update gate, If it is set to False, no bias will be applied to the update gate,
reset gate and candidate calculations. If it is set to None or one reset gate and candidate calculations. If it is set to None or one
attribute of ParamAttr, gru_unit will create ParamAttr as attribute of ParamAttr, gru_unit will create ParamAttr as
bias_attr. If the Initializer of the bias_attr is not set, the bias bias_attr. If the Initializer of the bias_attr is not set, the bias
is initialized zero. Default: None. is initialized zero. Default: None.
activation (string): The activation type for cell (actNode). activation (string): The activation type for cell (actNode).
...@@ -1064,9 +1064,9 @@ def dropout(x, ...@@ -1064,9 +1064,9 @@ def dropout(x,
inference: out = input inference: out = input
(make is a tensor same shape with input, value is 0 or 1 (make is a tensor same shape with input, value is 0 or 1
ratio of 0 is dropout_prob) ratio of 0 is dropout_prob)
dropout op can be removed from the program. dropout op can be removed from the program.
the program will be efficient the program will be efficient
Returns: Returns:
...@@ -2149,7 +2149,7 @@ def pool2d(input, ...@@ -2149,7 +2149,7 @@ def pool2d(input,
ceil_mode (bool): ${ceil_mode_comment} ceil_mode (bool): ${ceil_mode_comment}
name (str|None): A name for this layer(optional). If set None, the name (str|None): A name for this layer(optional). If set None, the
layer will be named automatically. layer will be named automatically.
exclusive (bool): Whether to exclude padding points in average pooling exclusive (bool): Whether to exclude padding points in average pooling
mode, default is true mode, default is true
Returns: Returns:
...@@ -2240,7 +2240,7 @@ def pool3d(input, ...@@ -2240,7 +2240,7 @@ def pool3d(input,
ceil_mode (bool): ${ceil_mode_comment} ceil_mode (bool): ${ceil_mode_comment}
name (str): A name for this layer(optional). If set None, the layer name (str): A name for this layer(optional). If set None, the layer
will be named automatically. will be named automatically.
exclusive (bool): Whether to exclude padding points in average pooling exclusive (bool): Whether to exclude padding points in average pooling
mode, default is true mode, default is true
Returns: Returns:
...@@ -4342,7 +4342,7 @@ def nce(input, ...@@ -4342,7 +4342,7 @@ def nce(input,
sampler (str): The sampler used to sample class from negtive classes. sampler (str): The sampler used to sample class from negtive classes.
It can be 'uniform', 'log_uniform' or 'custom_dist'. It can be 'uniform', 'log_uniform' or 'custom_dist'.
default: 'uniform'. default: 'uniform'.
custom_dist (Variable): A tensor with shape [num_total_classes]. custom_dist (Variable): A tensor with shape [num_total_classes].
It is used when sampler is set to 'custom_dist'. It is used when sampler is set to 'custom_dist'.
custom_dist[i] is the probsbility of i-th class to be sampled. custom_dist[i] is the probsbility of i-th class to be sampled.
default: None. default: None.
...@@ -4385,7 +4385,7 @@ def nce(input, ...@@ -4385,7 +4385,7 @@ def nce(input,
num_neg_samples=3, num_neg_samples=3,
sampler="custom_dist", sampler="custom_dist",
custom_dist=dist) custom_dist=dist)
""" """
helper = LayerHelper('nce', **locals()) helper = LayerHelper('nce', **locals())
assert isinstance(input, Variable) assert isinstance(input, Variable)
...@@ -4556,9 +4556,9 @@ def transpose(x, perm, name=None): ...@@ -4556,9 +4556,9 @@ def transpose(x, perm, name=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
# use append_batch_size=False to avoid prepending extra # use append_batch_size=False to avoid prepending extra
# batch size in shape # batch size in shape
x = fluid.layers.data(name='x', shape=[5, 10, 15], x = fluid.layers.data(name='x', shape=[5, 10, 15],
dtype='float32', append_batch_size=False) dtype='float32', append_batch_size=False)
x_transposed = layers.transpose(x, perm=[1, 0, 2]) x_transposed = layers.transpose(x, perm=[1, 0, 2])
""" """
...@@ -4835,7 +4835,7 @@ def softmax_with_cross_entropy(logits, ...@@ -4835,7 +4835,7 @@ def softmax_with_cross_entropy(logits,
3) If numeric_stable_mode is True, softmax is calculated first by: 3) If numeric_stable_mode is True, softmax is calculated first by:
.. math:: .. math::
max_j = \\max_{i=0}^{K}{\\text{logit}_i} max_j = \\max_{i=0}^{K}{\\text{logit}_i}
log\\_max\\_sum_j = \\log\\sum_{i=0}^{K}\\exp(logit_i - max_j) log\\_max\\_sum_j = \\log\\sum_{i=0}^{K}\\exp(logit_i - max_j)
...@@ -4858,18 +4858,18 @@ def softmax_with_cross_entropy(logits, ...@@ -4858,18 +4858,18 @@ def softmax_with_cross_entropy(logits,
numeric_stable_mode (bool): A flag to indicate whether to use a more numeric_stable_mode (bool): A flag to indicate whether to use a more
numerically stable algorithm. Only valid numerically stable algorithm. Only valid
when soft_label is False and GPU is used. when soft_label is False and GPU is used.
When soft_label is True or CPU is used, When soft_label is True or CPU is used,
the algorithm is always numerically stable. the algorithm is always numerically stable.
Note that the speed may be slower when use Note that the speed may be slower when use
stable algorithm. Default: False stable algorithm. Default: False
return_softmax (bool): A flag indicating whether to return the softmax return_softmax (bool): A flag indicating whether to return the softmax
along with the cross entropy loss. Default: False along with the cross entropy loss. Default: False
Returns: Returns:
Variable or Tuple of two Variables: Return the cross entropy loss if Variable or Tuple of two Variables: Return the cross entropy loss if
`return_softmax` is False, otherwise the tuple `return_softmax` is False, otherwise the tuple
(loss, softmax), where the cross entropy loss is (loss, softmax), where the cross entropy loss is
a 2-D tensor with shape [N x 1], and softmax is a a 2-D tensor with shape [N x 1], and softmax is a
2-D tensor with shape [N x K]. 2-D tensor with shape [N x K].
Examples: Examples:
...@@ -5756,20 +5756,20 @@ def image_resize(input, ...@@ -5756,20 +5756,20 @@ def image_resize(input,
Default: None Default: None
name(str|None): A name for this layer(optional). If set None, the layer name(str|None): A name for this layer(optional). If set None, the layer
will be named automatically. will be named automatically.
resample(str): The resample method. It supports 'BILINEAR' and 'NEAREST' resample(str): The resample method. It supports 'BILINEAR' and 'NEAREST'
currently. currently.
Default: 'BILINEAR' Default: 'BILINEAR'
actual_shape(Variable): An optional input to specify output shape actual_shape(Variable): An optional input to specify output shape
dynamically. If provided, image resize dynamically. If provided, image resize
according to this given shape rather than according to this given shape rather than
:attr:`out_shape` and :attr:`scale` specifying :attr:`out_shape` and :attr:`scale` specifying
shape. That is to say actual_shape has the shape. That is to say actual_shape has the
highest priority. It is recommended to use highest priority. It is recommended to use
actual_shape instead of :attr:`out_shape` if you actual_shape instead of :attr:`out_shape` if you
want to specify output shape dynamically. When want to specify output shape dynamically. When
using actual_shape to specify output shape, one of using actual_shape to specify output shape, one of
:attr:`out_shape` and :attr:`scale` should also be :attr:`out_shape` and :attr:`scale` should also be
set, otherwise errors would be occured in graph set, otherwise errors would be occured in graph
constructing stage. constructing stage.
Default: None Default: None
...@@ -5780,7 +5780,7 @@ def image_resize(input, ...@@ -5780,7 +5780,7 @@ def image_resize(input,
Raises: Raises:
TypeError: out_shape should be a list or tuple or Variable. TypeError: out_shape should be a list or tuple or Variable.
TypeError: actual_shape should either be Variable or None. TypeError: actual_shape should either be Variable or None.
ValueError: The 'resample' of image_resize can only be 'BILINEAR' ValueError: The 'resample' of image_resize can only be 'BILINEAR'
or 'NEAREST' currently. or 'NEAREST' currently.
ValueError: One of out_shape and scale must not be None. ValueError: One of out_shape and scale must not be None.
ValueError: out_shape length should be 2. ValueError: out_shape length should be 2.
...@@ -5852,17 +5852,17 @@ def resize_bilinear(input, ...@@ -5852,17 +5852,17 @@ def resize_bilinear(input,
name=None, name=None,
actual_shape=None): actual_shape=None):
""" """
Resize input by performing bilinear interpolation based on given Resize input by performing bilinear interpolation based on given
output shape which specified by actual_shape, out_shape and scale output shape which specified by actual_shape, out_shape and scale
in priority order. in priority order.
Bilinear interpolation is an extension of linear interpolation for Bilinear interpolation is an extension of linear interpolation for
interpolating functions of two variables (e.g. H-direction and interpolating functions of two variables (e.g. H-direction and
W-direction in this op) on a rectilinear 2D grid. The key idea is W-direction in this op) on a rectilinear 2D grid. The key idea is
to perform linear interpolation first in one direction, and then to perform linear interpolation first in one direction, and then
again in the other direction. again in the other direction.
For details of bilinear interpolation, please refer to Wikipedia: For details of bilinear interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Bilinear_interpolation https://en.wikipedia.org/wiki/Bilinear_interpolation
Args: Args:
...@@ -5875,17 +5875,17 @@ def resize_bilinear(input, ...@@ -5875,17 +5875,17 @@ def resize_bilinear(input,
a higher priority than scale. Default: None. a higher priority than scale. Default: None.
name(str|None): The output variable name. name(str|None): The output variable name.
actual_shape(Variable): An optional input to specify output shape actual_shape(Variable): An optional input to specify output shape
dynamically. If provided, image resize dynamically. If provided, image resize
according to this given shape rather than according to this given shape rather than
:attr:`out_shape` and :attr:`scale` specifying :attr:`out_shape` and :attr:`scale` specifying
shape. That is to say actual_shape has the shape. That is to say actual_shape has the
highest priority. It is recommended to use highest priority. It is recommended to use
actual_shape instead of :attr:`out_shape` if you actual_shape instead of :attr:`out_shape` if you
want to specify output shape dynamically. When want to specify output shape dynamically. When
using actual_shape to specify output shape, one of using actual_shape to specify output shape, one of
:attr:`out_shape` and :attr:`scale` should also be :attr:`out_shape` and :attr:`scale` should also be
set, otherwise errors would be occured in graph set, otherwise errors would be occured in graph
constructing stage. constructing stage.
Default: None Default: None
...@@ -5909,11 +5909,11 @@ def resize_nearest(input, ...@@ -5909,11 +5909,11 @@ def resize_nearest(input,
actual_shape=None): actual_shape=None):
""" """
Resize input by performing nearest neighbor interpolation in both the Resize input by performing nearest neighbor interpolation in both the
3rd dimention(in height direction) and the 4th dimention(in width 3rd dimention(in height direction) and the 4th dimention(in width
direction) based on given output shape which specified by actual_shape, direction) based on given output shape which specified by actual_shape,
out_shape and scale in priority order. out_shape and scale in priority order.
For details of nearest neighbor interpolation, please refer to Wikipedia: For details of nearest neighbor interpolation, please refer to Wikipedia:
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
Args: Args:
...@@ -5926,17 +5926,17 @@ def resize_nearest(input, ...@@ -5926,17 +5926,17 @@ def resize_nearest(input,
a higher priority than scale. Default: None. a higher priority than scale. Default: None.
name(str|None): The output variable name. name(str|None): The output variable name.
actual_shape(Variable): An optional input to specify output shape actual_shape(Variable): An optional input to specify output shape
dynamically. If provided, image resize dynamically. If provided, image resize
according to this given shape rather than according to this given shape rather than
:attr:`out_shape` and :attr:`scale` specifying :attr:`out_shape` and :attr:`scale` specifying
shape. That is to say actual_shape has the shape. That is to say actual_shape has the
highest priority. It is recommended to use highest priority. It is recommended to use
actual_shape instead of :attr:`out_shape` if you actual_shape instead of :attr:`out_shape` if you
want to specify output shape dynamically. When want to specify output shape dynamically. When
using actual_shape to specify output shape, one of using actual_shape to specify output shape, one of
:attr:`out_shape` and :attr:`scale` should also be :attr:`out_shape` and :attr:`scale` should also be
set, otherwise errors would be occured in graph set, otherwise errors would be occured in graph
constructing stage. constructing stage.
Default: None Default: None
...@@ -6446,15 +6446,15 @@ def affine_grid(theta, out_shape, name=None): ...@@ -6446,15 +6446,15 @@ def affine_grid(theta, out_shape, name=None):
[x_14, x_15, x_16]] [x_14, x_15, x_16]]
[[x_21, x_22, x_23] [[x_21, x_22, x_23]
[x_24, x_25, x_26]]] [x_24, x_25, x_26]]]
out_shape = [2, 3, 5, 5] out_shape = [2, 3, 5, 5]
Step 1: Step 1:
Generate normalized coordinates according to out_shape. Generate normalized coordinates according to out_shape.
The values of the normalized coordinates are in the interval between -1 and 1. The values of the normalized coordinates are in the interval between -1 and 1.
The shape of the normalized coordinates is [2, H, W] as below: The shape of the normalized coordinates is [2, H, W] as below:
C = [[[-1. -1. -1. -1. -1. ] C = [[[-1. -1. -1. -1. -1. ]
[-0.5 -0.5 -0.5 -0.5 -0.5] [-0.5 -0.5 -0.5 -0.5 -0.5]
[ 0. 0. 0. 0. 0. ] [ 0. 0. 0. 0. 0. ]
...@@ -7702,6 +7702,15 @@ def logical_and(x, y, out=None, name=None): ...@@ -7702,6 +7702,15 @@ def logical_and(x, y, out=None, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
left = fluid.layers.data(
name='left', shape=[1], dtype='int32')
right = fluid.layers.data(
name='right', shape=[1], dtype='int32')
result = fluid.layers.logical_and(x=left, y=right)
""" """
return _logical_op( return _logical_op(
...@@ -7721,6 +7730,15 @@ def logical_or(x, y, out=None, name=None): ...@@ -7721,6 +7730,15 @@ def logical_or(x, y, out=None, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
left = fluid.layers.data(
name='left', shape=[1], dtype='int32')
right = fluid.layers.data(
name='right', shape=[1], dtype='int32')
result = fluid.layers.logical_or(x=left, y=right)
""" """
return _logical_op( return _logical_op(
...@@ -7740,6 +7758,15 @@ def logical_xor(x, y, out=None, name=None): ...@@ -7740,6 +7758,15 @@ def logical_xor(x, y, out=None, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
left = fluid.layers.data(
name='left', shape=[1], dtype='int32')
right = fluid.layers.data(
name='right', shape=[1], dtype='int32')
result = fluid.layers.logical_xor(x=left, y=right)
""" """
return _logical_op( return _logical_op(
...@@ -7758,6 +7785,13 @@ def logical_not(x, out=None, name=None): ...@@ -7758,6 +7785,13 @@ def logical_not(x, out=None, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
left = fluid.layers.data(
name='left', shape=[1], dtype='int32')
result = fluid.layers.logical_not(x=left)
""" """
return _logical_op( return _logical_op(
...@@ -7777,6 +7811,13 @@ def clip(x, min, max, name=None): ...@@ -7777,6 +7811,13 @@ def clip(x, min, max, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
input = fluid.layers.data(
name='data', shape=[1], dtype='float32')
reward = fluid.layers.clip(x=input, min=-1.0, max=1.0)
""" """
helper = LayerHelper("clip", **locals()) helper = LayerHelper("clip", **locals())
...@@ -7809,6 +7850,13 @@ def clip_by_norm(x, max_norm, name=None): ...@@ -7809,6 +7850,13 @@ def clip_by_norm(x, max_norm, name=None):
Returns: Returns:
out(${out_type}): ${out_comment} out(${out_type}): ${out_comment}
Examples:
.. code-block:: python
input = fluid.layers.data(
name='data', shape=[1], dtype='float32')
reward = fluid.layers.clip_by_norm(x=input, max_norm=1.0)
""" """
helper = LayerHelper("clip_by_norm", **locals()) helper = LayerHelper("clip_by_norm", **locals())
...@@ -7954,19 +8002,19 @@ def maxout(x, groups, name=None): ...@@ -7954,19 +8002,19 @@ def maxout(x, groups, name=None):
def space_to_depth(x, blocksize, name=None): def space_to_depth(x, blocksize, name=None):
""" """
Gives a blocksize to space_to_depth the input LoDtensor with Layout: [batch, channel, height, width] Gives a blocksize to space_to_depth the input LoDtensor with Layout: [batch, channel, height, width]
This op rearranges blocks of spatial data, into depth. More specifically, this op outputs a copy of the This op rearranges blocks of spatial data, into depth. More specifically, this op outputs a copy of the
input LoDtensor where values from the height and width dimensions are moved to the channel dimension. input LoDtensor where values from the height and width dimensions are moved to the channel dimension.
The attr blocksize indicates the input block size. The attr blocksize indicates the input block size.
space_to_depth will reorgnize the elements of input with shape[batch, channel, height, width] according space_to_depth will reorgnize the elements of input with shape[batch, channel, height, width] according
to blocksize to construct output with shape [batch, channel * blocksize * blocksize, height/blocksize, width/blocksize]: to blocksize to construct output with shape [batch, channel * blocksize * blocksize, height/blocksize, width/blocksize]:
space_to_depth is used to This operation is useful for resizing the activations between convolutions space_to_depth is used to This operation is useful for resizing the activations between convolutions
(but keeping all data) (but keeping all data)
- Non-overlapping blocks of size block_size x block size are rearranged into depth at each location. - Non-overlapping blocks of size block_size x block size are rearranged into depth at each location.
- The depth of the output tensor is block_size * block_size * input channel - The depth of the output tensor is block_size * block_size * input channel
- The Y, X coordinates within each block of the input become the high order component of the output channel index - The Y, X coordinates within each block of the input become the high order component of the output channel index
- channel should be divisible by square of blocksize - channel should be divisible by square of blocksize
- height, width should be divsible by blocksize - height, width should be divsible by blocksize
...@@ -8013,7 +8061,7 @@ def space_to_depth(x, blocksize, name=None): ...@@ -8013,7 +8061,7 @@ def space_to_depth(x, blocksize, name=None):
@templatedoc() @templatedoc()
def sequence_reverse(x, name=None): def sequence_reverse(x, name=None):
""" """
${comment} ${comment}
Args: Args:
...@@ -8080,21 +8128,21 @@ def affine_channel(x, scale=None, bias=None, data_layout='NCHW', name=None): ...@@ -8080,21 +8128,21 @@ def affine_channel(x, scale=None, bias=None, data_layout='NCHW', name=None):
def similarity_focus(input, axis, indexes, name=None): def similarity_focus(input, axis, indexes, name=None):
""" """
SimilarityFocus Operator SimilarityFocus Operator
Generate a similarity focus mask with the same shape of input using the following method: Generate a similarity focus mask with the same shape of input using the following method:
1. Extract the 3-D tensor(here the first dimension is BatchSize) corresponding 1. Extract the 3-D tensor(here the first dimension is BatchSize) corresponding
to the axis according to the indexes. For example, if axis=1 and indexes=[a], to the axis according to the indexes. For example, if axis=1 and indexes=[a],
it will get the matrix T=X[:, a, :, :]. In this case, if the shape of input X it will get the matrix T=X[:, a, :, :]. In this case, if the shape of input X
is (BatchSize, A, B, C), the shape of tensor T is (BatchSize, B, C). is (BatchSize, A, B, C), the shape of tensor T is (BatchSize, B, C).
2. For each index, find the largest numbers in the tensor T, so that the same 2. For each index, find the largest numbers in the tensor T, so that the same
row and same column has at most one number(what it means is that if the row and same column has at most one number(what it means is that if the
largest number has been found in the i-th row and the j-th column, then largest number has been found in the i-th row and the j-th column, then
the numbers in the i-th row or j-th column will be skipped. And then the the numbers in the i-th row or j-th column will be skipped. And then the
next largest number will be selected from the remaining numbers. Obviously next largest number will be selected from the remaining numbers. Obviously
there will be min(B, C) numbers), and mark the corresponding position of the there will be min(B, C) numbers), and mark the corresponding position of the
3-D similarity focus mask as 1, otherwise as 0. Do elementwise-or for 3-D similarity focus mask as 1, otherwise as 0. Do elementwise-or for
each index. each index.
3. Broadcast the 3-D similarity focus mask to the same shape of input X. 3. Broadcast the 3-D similarity focus mask to the same shape of input X.
...@@ -8150,16 +8198,16 @@ def similarity_focus(input, axis, indexes, name=None): ...@@ -8150,16 +8198,16 @@ def similarity_focus(input, axis, indexes, name=None):
[1.0, 0.0]]]] [1.0, 0.0]]]]
Args: Args:
input(Variable): The input tensor variable(default float). It should input(Variable): The input tensor variable(default float). It should
be a 4-D tensor with shape [BatchSize, A, B, C]. be a 4-D tensor with shape [BatchSize, A, B, C].
axis(int): Indicating the dimension to be selected. It can only be axis(int): Indicating the dimension to be selected. It can only be
1, 2 or 3. 1, 2 or 3.
indexes(list): Indicating the indexes of the selected dimension. indexes(list): Indicating the indexes of the selected dimension.
Returns: Returns:
Variable: A tensor variable with the same shape and same type Variable: A tensor variable with the same shape and same type
as the input. as the input.
Examples: Examples:
.. code-block:: python .. code-block:: python
data = fluid.layers.data( data = fluid.layers.data(
...@@ -8262,12 +8310,12 @@ def hash(input, hash_size, num_hash=1, name=None): ...@@ -8262,12 +8310,12 @@ def hash(input, hash_size, num_hash=1, name=None):
@templatedoc() @templatedoc()
def grid_sampler(x, grid, name=None): def grid_sampler(x, grid, name=None):
""" """
This operation samples input X by using bilinear interpolation based on This operation samples input X by using bilinear interpolation based on
flow field grid, which is usually gennerated by affine_grid. The grid of flow field grid, which is usually gennerated by affine_grid. The grid of
shape [N, H, W, 2] is the concatenation of (grid_x, grid_y) coordinates shape [N, H, W, 2] is the concatenation of (grid_x, grid_y) coordinates
with shape [N, H, W] each, where grid_x is indexing the 4th dimension with shape [N, H, W] each, where grid_x is indexing the 4th dimension
(in width dimension) of input data x and grid_y is indexng the 3rd (in width dimension) of input data x and grid_y is indexng the 3rd
dimention (in height dimension), finally results is the bilinear dimention (in height dimension), finally results is the bilinear
interpolation value of 4 nearest corner points. interpolation value of 4 nearest corner points.
Step 1: Step 1:
...@@ -8277,7 +8325,7 @@ def grid_sampler(x, grid, name=None): ...@@ -8277,7 +8325,7 @@ def grid_sampler(x, grid, name=None):
grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1) grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1)
Step 2: Step 2:
Indices input data X with grid (x, y) in each [H, W] area, and bilinear Indices input data X with grid (x, y) in each [H, W] area, and bilinear
interpolate point value by 4 nearest points. interpolate point value by 4 nearest points.
wn ------- y_n ------- en wn ------- y_n ------- en
...@@ -8314,7 +8362,7 @@ def grid_sampler(x, grid, name=None): ...@@ -8314,7 +8362,7 @@ def grid_sampler(x, grid, name=None):
name (str, default None): The name of this layer. name (str, default None): The name of this layer.
Returns: Returns:
out(Variable): Output of shape [N, C, H, W] data samples input X out(Variable): Output of shape [N, C, H, W] data samples input X
using bilnear interpolation based on input grid. using bilnear interpolation based on input grid.
Exmples: Exmples:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册