|
|
|
@ -65,6 +65,7 @@ __all__ = [
|
|
|
|
|
'reduce_prod',
|
|
|
|
|
'sequence_first_step',
|
|
|
|
|
'sequence_last_step',
|
|
|
|
|
'sequence_slice',
|
|
|
|
|
'dropout',
|
|
|
|
|
'split',
|
|
|
|
|
'ctc_greedy_decoder',
|
|
|
|
@ -1903,6 +1904,76 @@ def sequence_last_step(input):
|
|
|
|
|
return sequence_pool(input=input, pool_type="last")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sequence_slice(input, offset, length, name=None):
|
|
|
|
|
"""
|
|
|
|
|
**Sequence Slice Layer**
|
|
|
|
|
|
|
|
|
|
The layer crops a subsequence from given sequence with given start
|
|
|
|
|
offset and subsequence length.
|
|
|
|
|
|
|
|
|
|
It only supports sequence data (LoDTensor with lod_level equal to 1).
|
|
|
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
|
|
- Case:
|
|
|
|
|
|
|
|
|
|
Given the input Variable **input**:
|
|
|
|
|
|
|
|
|
|
input.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]],
|
|
|
|
|
input.lod = [[3, 2]],
|
|
|
|
|
input.dims = (5, 2),
|
|
|
|
|
|
|
|
|
|
with offset.data = [[0], [1]] and length.data = [[2], [1]],
|
|
|
|
|
|
|
|
|
|
the output Variable will be
|
|
|
|
|
|
|
|
|
|
out.data = [[a1, a2], [b1, b2], [e1, e2]],
|
|
|
|
|
out.lod = [[2, 1]],
|
|
|
|
|
out.dims = (3, 2).
|
|
|
|
|
|
|
|
|
|
NOTE: The first dimension size of **input**, **offset** and **length**
|
|
|
|
|
should be equal. The **offset** should start from 0.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input(Variable): The input Variable which consists of the complete
|
|
|
|
|
sequences.
|
|
|
|
|
offset(Variable): The offset to slice each sequence.
|
|
|
|
|
length(Variable): The length of each subsequence.
|
|
|
|
|
name(str|None): A name for this layer(optional). If set None, the
|
|
|
|
|
layer will be named automatically.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The output subsequences.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
seqs = fluid.layers.data(name='x', shape=[10, 5],
|
|
|
|
|
dtype='float32', lod_level=1)
|
|
|
|
|
offset = fluid.layers.assign(input=np.array([[0, 1]]).astype("int32"))
|
|
|
|
|
length = fluid.layers.assign(input=np.array([[2, 1]]).astype("int32"))
|
|
|
|
|
subseqs = fluid.layers.sequence_slice(input=seqs, offset=offset,
|
|
|
|
|
length=length)
|
|
|
|
|
"""
|
|
|
|
|
helper = LayerHelper("sequence_slice", **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
out = helper.create_tmp_variable(dtype)
|
|
|
|
|
|
|
|
|
|
offset.stop_gradient = True
|
|
|
|
|
length.stop_gradient = True
|
|
|
|
|
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type="sequence_slice",
|
|
|
|
|
inputs={"X": input,
|
|
|
|
|
"Offset": offset,
|
|
|
|
|
"Length": length},
|
|
|
|
|
outputs={"Out": out})
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@templatedoc()
|
|
|
|
|
def pool2d(input,
|
|
|
|
|
pool_size=-1,
|
|
|
|
|