|
|
@ -55,34 +55,36 @@ __all__ = [
|
|
|
|
|
|
|
|
|
|
|
|
def split_lod_tensor(input, mask, level=0):
|
|
|
|
def split_lod_tensor(input, mask, level=0):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
**split_lod_tensor**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This function takes in an input that contains the complete lod information,
|
|
|
|
This function takes in an input that contains the complete lod information,
|
|
|
|
and takes in a mask which is used to mask certain parts of the input.
|
|
|
|
and takes in a mask which is used to mask certain parts of the input.
|
|
|
|
The output is the true branch and the false branch with the mask applied to
|
|
|
|
The output is the true branch and the false branch with the mask applied to
|
|
|
|
the input at a certain level in the tensor.
|
|
|
|
the input at a certain level in the tensor. Mainly used in IfElse to split
|
|
|
|
|
|
|
|
data into two parts.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
input(tuple|list|None): The input tensor that contains complete
|
|
|
|
input(tuple|list|None): The input tensor that contains complete
|
|
|
|
lod information needed to construct the output.
|
|
|
|
lod information needed to construct the output.
|
|
|
|
mask(list): A bool column vector which masks the input.
|
|
|
|
mask(list): A bool column vector which masks the input.
|
|
|
|
level(int): The specific lod level to rank.
|
|
|
|
level(int): The specific lod level to split.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
Variable: The true branch of tensor as per the mask applied to input.
|
|
|
|
tuple(Variable, Variable):
|
|
|
|
Variable: The false branch of tensor as per the mask applied to input.
|
|
|
|
The true branch of tensor as per the mask applied to input.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The false branch of tensor as per the mask applied to input.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Examples:
|
|
|
|
.. code-block:: python
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
x = layers.data(name='x', shape=[1])
|
|
|
|
x = fluid.layers.data(name='x', shape=[1])
|
|
|
|
x.persistable = True
|
|
|
|
x.persistable = True
|
|
|
|
|
|
|
|
|
|
|
|
y = layers.data(name='y', shape=[1])
|
|
|
|
y = fluid.layers.data(name='y', shape=[1])
|
|
|
|
y.persistable = True
|
|
|
|
y.persistable = True
|
|
|
|
|
|
|
|
|
|
|
|
out_true, out_false = layers.split_lod_tensor(
|
|
|
|
out_true, out_false = fluid.layers.split_lod_tensor(
|
|
|
|
input=x, mask=y, level=level)
|
|
|
|
input=x, mask=y, level=level)
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
helper = LayerHelper('split_lod_tensor', **locals())
|
|
|
|
helper = LayerHelper('split_lod_tensor', **locals())
|
|
|
|
out_true = helper.create_tmp_variable(dtype=input.dtype)
|
|
|
|
out_true = helper.create_tmp_variable(dtype=input.dtype)
|
|
|
@ -105,8 +107,9 @@ def merge_lod_tensor(in_true, in_false, x, mask, level=0):
|
|
|
|
|
|
|
|
|
|
|
|
This function takes in an input :math:`x`, the True branch, the False
|
|
|
|
This function takes in an input :math:`x`, the True branch, the False
|
|
|
|
branch and a binary :math:`mask`. Using this information, this function
|
|
|
|
branch and a binary :math:`mask`. Using this information, this function
|
|
|
|
merges the True and False branches of the tensor into a single Output
|
|
|
|
merges the True and False branches of the tensor into a single tensor as
|
|
|
|
at a certain lod level indiacted by :math:`level`.
|
|
|
|
output at a certain lod level indicated by :math:`level`. Used in IfElse
|
|
|
|
|
|
|
|
to merge the output if True block and False Block.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
in_true(tuple|list|None): The True branch to be merged.
|
|
|
|
in_true(tuple|list|None): The True branch to be merged.
|
|
|
@ -114,7 +117,7 @@ def merge_lod_tensor(in_true, in_false, x, mask, level=0):
|
|
|
|
x(tuple|list|None): The input tensor that contains complete
|
|
|
|
x(tuple|list|None): The input tensor that contains complete
|
|
|
|
lod information needed to construct the output.
|
|
|
|
lod information needed to construct the output.
|
|
|
|
mask(list): A bool column vector which masks the input.
|
|
|
|
mask(list): A bool column vector which masks the input.
|
|
|
|
level(int): The specific lod level to rank.
|
|
|
|
level(int): The specific lod level to merge.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
Variable: The merged output tensor.
|
|
|
|
Variable: The merged output tensor.
|
|
|
@ -965,14 +968,17 @@ def array_write(x, i, array=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_array(dtype):
|
|
|
|
def create_array(dtype):
|
|
|
|
"""This function creates an array of type :math:`LOD_TENSOR_ARRAY` using the
|
|
|
|
"""
|
|
|
|
LayerHelper.
|
|
|
|
**Create LoDTensorArray**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This function creates an array of LOD_TENSOR_ARRAY . It is mainly used to
|
|
|
|
|
|
|
|
implement RNN with array_write, array_read and While.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
dtype (int|float): The data type of the elements in the array.
|
|
|
|
dtype (int|float): The data type of the elements in the lod_tensor_array.
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Returns:
|
|
|
|
Variable: The tensor variable storing the elements of data type.
|
|
|
|
Variable: The lod_tensor_array variable storing the elements of data type.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Examples:
|
|
|
|
.. code-block:: python
|
|
|
|
.. code-block:: python
|
|
|
@ -1083,10 +1089,9 @@ def array_read(array, i):
|
|
|
|
Examples:
|
|
|
|
Examples:
|
|
|
|
.. code-block:: python
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
|
|
|
|
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
|
|
|
|
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
arr = fluid.layers.array_read(tmp, i=i)
|
|
|
|
arr = layers.array_read(tmp, i=i)
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
helper = LayerHelper('array_read', **locals())
|
|
|
|
helper = LayerHelper('array_read', **locals())
|
|
|
|
if not isinstance(
|
|
|
|
if not isinstance(
|
|
|
@ -1140,9 +1145,14 @@ def shrink_memory(x, i, table):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def array_length(array):
|
|
|
|
def array_length(array):
|
|
|
|
"""This function performs the operation to find the length of the input
|
|
|
|
"""
|
|
|
|
|
|
|
|
**Get the Length of Input LoDTensorArray**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This function performs the operation to find the length of the input
|
|
|
|
LOD_TENSOR_ARRAY.
|
|
|
|
LOD_TENSOR_ARRAY.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Related API: array_read, array_write, While.
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
array (LOD_TENSOR_ARRAY): The input array that will be used
|
|
|
|
array (LOD_TENSOR_ARRAY): The input array that will be used
|
|
|
|
to compute the length.
|
|
|
|
to compute the length.
|
|
|
@ -1151,12 +1161,13 @@ def array_length(array):
|
|
|
|
Variable: The length of the input LoDTensorArray.
|
|
|
|
Variable: The length of the input LoDTensorArray.
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Examples:
|
|
|
|
.. code-block::python
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
|
|
|
|
tmp = fluid.layers.zeros(shape=[10], dtype='int32')
|
|
|
|
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=10)
|
|
|
|
arr = fluid.layers.array_write(tmp, i=i)
|
|
|
|
arr = fluid.layers.array_write(tmp, i=i)
|
|
|
|
arr_len = fluid.layers.array_length(arr)
|
|
|
|
arr_len = fluid.layers.array_length(arr)
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
helper = LayerHelper('array_length', **locals())
|
|
|
|
helper = LayerHelper('array_length', **locals())
|
|
|
|
tmp = helper.create_tmp_variable(dtype='int64')
|
|
|
|
tmp = helper.create_tmp_variable(dtype='int64')
|
|
|
@ -1247,6 +1258,42 @@ class ConditionalBlock(object):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Switch(object):
|
|
|
|
class Switch(object):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Switch class works just like a `if-elif-else`. Can be used in learning rate scheduler
|
|
|
|
|
|
|
|
to modify learning rate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The Semantics:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1. A `switch` control-flow checks cases one-by-one.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. The condition of each case is a boolean value, which is a scalar Variable.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. It runs the first matched case, or the default case if there is one.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4. Once it matches a case, it runs the corresponding branch and only that branch.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lr = fluid.layers.tensor.create_global_var(
|
|
|
|
|
|
|
|
shape=[1],
|
|
|
|
|
|
|
|
value=0.0,
|
|
|
|
|
|
|
|
dtype='float32',
|
|
|
|
|
|
|
|
persistable=True,
|
|
|
|
|
|
|
|
name="learning_rate")
|
|
|
|
|
|
|
|
one_var = tensor.fill_constant(
|
|
|
|
|
|
|
|
shape=[1], dtype='float32', value=1.0)
|
|
|
|
|
|
|
|
two_var = tensor.fill_constant(
|
|
|
|
|
|
|
|
shape=[1], dtype='float32', value=2.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with fluid.layers.control_flow.Switch() as switch:
|
|
|
|
|
|
|
|
with switch.case(global_step == zero_var):
|
|
|
|
|
|
|
|
fluid.layers.tensor.assign(input=one_var, output=lr)
|
|
|
|
|
|
|
|
with switch.default():
|
|
|
|
|
|
|
|
fluid.layers.tensor.assign(input=two_var, output=lr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, name=None):
|
|
|
|
def __init__(self, name=None):
|
|
|
|
self.helper = LayerHelper('switch', name=name)
|
|
|
|
self.helper = LayerHelper('switch', name=name)
|
|
|
|
self.inside_scope = False
|
|
|
|
self.inside_scope = False
|
|
|
@ -1276,7 +1323,8 @@ class Switch(object):
|
|
|
|
return ConditionalBlockGuard(cond_block)
|
|
|
|
return ConditionalBlockGuard(cond_block)
|
|
|
|
|
|
|
|
|
|
|
|
def default(self):
|
|
|
|
def default(self):
|
|
|
|
"""create a default case for this switch
|
|
|
|
"""
|
|
|
|
|
|
|
|
create a default case for this switch
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pre_cond_num = len(self.pre_not_conditions)
|
|
|
|
pre_cond_num = len(self.pre_not_conditions)
|
|
|
|
if pre_cond_num == 0:
|
|
|
|
if pre_cond_num == 0:
|
|
|
|