Skip to content

Commit 16b2c6d

Browse files
author
Yibing Liu
committed
Add py api for sequence_slice_op
test=develop
1 parent 2c9839c commit 16b2c6d

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

paddle/fluid/platform/profiler.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ void ParseEvents(const std::vector<std::vector<Event>>& events,
370370
std::vector<std::vector<Event>> merged_events_list;
371371
if (merge_thread) {
372372
std::vector<Event> merged_events;
373-
for (int i = 0; i < events.size(); ++i) {
374-
for (int j = 0; j < events[i].size(); ++j) {
373+
for (size_t i = 0; i < events.size(); ++i) {
374+
for (size_t j = 0; j < events[i].size(); ++j) {
375375
merged_events.push_back(events[i][j]);
376376
}
377377
}

python/paddle/fluid/layers/nn.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
'reduce_prod',
6565
'sequence_first_step',
6666
'sequence_last_step',
67+
'sequence_slice',
6768
'dropout',
6869
'split',
6970
'ctc_greedy_decoder',
@@ -1901,6 +1902,72 @@ def sequence_last_step(input):
19011902
return sequence_pool(input=input, pool_type="last")
19021903

19031904

1905+
def sequence_slice(input, offset, length, name=None):
1906+
"""
1907+
**Sequence Slice Layer**
1908+
1909+
The layer crops a subsequence from given sequence with given start
1910+
offset and subsequence length.
1911+
1912+
It only supports sequence data (LoDTensor with lod_level equal to 1).
1913+
1914+
.. code-block:: text
1915+
1916+
- Case:
1917+
Given the input Variable **input**,
1918+
input.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]],
1919+
input.lod = [[0, 3, 5]], input.dims = (5, 2)
1920+
1921+
with offset.data = [[0], [1]], length.data = [[2], [1]],
1922+
1923+
the output Variable will be
1924+
1925+
out.data = [[a1, a2], [b1, b2], [e1, e2]],
1926+
out.lod = [[0, 2, 3]], out.dims = (3, 2)
1927+
1928+
NOTE: The first dimension size of input, the size of offset and Length,
1929+
should be equal. The offset start from 0.
1930+
1931+
Args:
1932+
input(Variable): The input Variable which consists of the complete
1933+
sentences.
1934+
offset(Variable): The offset to slice each sequence.
1935+
length(Variable): The length of each subsequence.
1936+
name(str|None): A name for this layer(optional). If set None, the
1937+
layer will be named automatically.
1938+
1939+
Returns:
1940+
Variable: The subsequences.
1941+
1942+
Examples:
1943+
1944+
.. code-block:: python
1945+
1946+
import numpy as np
1947+
seqs = fluid.layers.data(name='x', shape=[10, 5],
1948+
dtype='float32', lod_level=1)
1949+
offset = fluid.layers.assign(input=np.array([[0, 1]]).astype("int32"))
1950+
length = fluid.layers.assign(input=np.array([[2, 1]]).astype("int32"))
1951+
subseqs = fluid.layers.sequence_slice(input=seqs, offset=offset,
1952+
length=length)
1953+
"""
1954+
helper = LayerHelper("sequence_slice", **locals())
1955+
dtype = helper.input_dtype()
1956+
out = helper.create_tmp_variable(dtype)
1957+
1958+
offset.stop_gradient = True
1959+
length.stop_gradient = True
1960+
1961+
helper.append_op(
1962+
type="sequence_slice",
1963+
inputs={"X": input,
1964+
"Offset": offset,
1965+
"Length": length},
1966+
outputs={"Out": out})
1967+
1968+
return out
1969+
1970+
19041971
@templatedoc()
19051972
def pool2d(input,
19061973
pool_size=-1,

python/paddle/fluid/tests/unittests/test_layers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,19 @@ def test_sequence_scatter(self):
406406
self.assertIsNotNone(out)
407407
print(str(program))
408408

409+
def test_sequence_slice(self):
410+
program = Program()
411+
with program_guard(program):
412+
import numpy as np
413+
seqs = layers.data(
414+
name='x', shape=[10, 5], dtype='float32', lod_level=1)
415+
offset = layers.assign(input=np.array([[0, 1]]).astype('int32'))
416+
length = layers.assign(input=np.array([[2, 1]]).astype('int32'))
417+
out = layers.sequence_slice(
418+
input=seqs, offset=offset, length=length)
419+
self.assertIsNotNone(out)
420+
print(str(program))
421+
409422
def test_lod_reset(self):
410423
program = Program()
411424
with program_guard(program):

0 commit comments

Comments
 (0)