Skip to content

Commit

Permalink
Remove the repeated API.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingqing01 committed Apr 17, 2018
1 parent 0aa78d0 commit 9426765
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 51 deletions.
5 changes: 5 additions & 0 deletions doc/fluid/api/layers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,3 +815,8 @@ zeros
.. autofunction:: paddle.fluid.layers.zeros
:noindex:

topk
----

.. autofunction:: paddle.fluid.layers.topk
:noindex:
38 changes: 0 additions & 38 deletions python/paddle/fluid/layers/control_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
'Switch',
'lod_rank_table',
'max_sequence_len',
'topk',
'lod_tensor_to_array',
'array_to_lod_tensor',
'increment',
Expand Down Expand Up @@ -751,43 +750,6 @@ def max_sequence_len(rank_table):
return res


def topk(input, k):
"""
**topk**
This function performs the operation that selects the k entries in the input
vector and outputs their values and indices as vectors. Thus topk_out[j] is
the j-th largest entry in input, and its index is topk_indices[j]
Args:
input (Variable|list): The input tensor that has all the data.
k (int): The number of top elements that the function will pick.
Returns:
Variable: The variable of type array that contains the k largest entries
from input.
Variable: The variable of type array that contains the indices of k
largest entries from input.
Examples:
.. code-block:: python
x = fluid.layers.data(name='x', shape=[10])
k = 5
array = fluid.layers.topk(x, k)
"""
helper = LayerHelper('topk', **locals())
topk_out = helper.create_tmp_variable(dtype=input.dtype)
topk_indices = helper.create_tmp_variable(dtype='int64')
helper.append_op(
type='top_k',
inputs={'X': [input]},
outputs={'Out': [topk_out],
'Indices': [topk_indices]},
attrs={'k': k})
return topk_out, topk_indices


def lod_tensor_to_array(x, table):
""" Convert a LOD_TENSOR to an LOD_TENSOR_ARRAY.
Expand Down
11 changes: 3 additions & 8 deletions python/paddle/fluid/layers/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
def accuracy(input, label, k=1, correct=None, total=None):
"""
This function computes the accuracy using the input and label.
The output is the top_k inputs and their indices.
The output is the top k inputs and their indices.
"""
helper = LayerHelper("accuracy", **locals())
topk_out, topk_indices = nn.top_k(input, k=k)
topk_out, topk_indices = nn.topk(input, k=k)
acc_out = helper.create_tmp_variable(dtype="float32")
if correct is None:
correct = helper.create_tmp_variable(dtype="int64")
Expand Down Expand Up @@ -62,12 +62,7 @@ def auc(input, label, curve='ROC', num_thresholds=200):
helper = LayerHelper("auc", **locals())
topk_out = helper.create_tmp_variable(dtype=input.dtype)
topk_indices = helper.create_tmp_variable(dtype="int64")
helper.append_op(
type="top_k",
inputs={"X": [input]},
outputs={"Out": [topk_out],
"Indices": [topk_indices]},
attrs={"k": k})
topk_out, topk_indices = nn.topk(input, k=k)
auc_out = helper.create_tmp_variable(dtype="float32")
if correct is None:
correct = helper.create_tmp_variable(dtype="int64")
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
'edit_distance',
'l2_normalize',
'matmul',
'top_k',
'topk',
'warpctc',
'sequence_reshape',
'transpose',
Expand Down Expand Up @@ -2572,7 +2572,7 @@ def __check_input(x, y):
return out


def top_k(input, k):
def topk(input, k):
"""
This operator is used to find values and indices of the k largest entries
for the last dimension.
Expand All @@ -2598,7 +2598,7 @@ def top_k(input, k):
Examples:
.. code-block:: python
top5_values, top5_indices = layers.top_k(input, k=5)
top5_values, top5_indices = layers.topk(input, k=5)
"""
shape = input.shape
if k < 1 and k >= shape[-1]:
Expand Down Expand Up @@ -2760,7 +2760,7 @@ def ctc_greedy_decoder(input, blank, name=None):
cost = fluid.layers.ctc_greedy_decoder(input=x, blank=0)
"""
helper = LayerHelper("ctc_greedy_decoder", **locals())
_, topk_indices = top_k(input, k=1)
_, topk_indices = topk(input, k=1)

# ctc align op
ctc_out = helper.create_tmp_variable(dtype="int64")
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_topk(self):
program = Program()
with program_guard(program):
data = layers.data(name="label", shape=[200], dtype="float32")
values, indices = layers.top_k(data, k=5)
values, indices = layers.topk(data, k=5)
self.assertIsNotNone(values)
self.assertIsNotNone(indices)
print(str(program))
Expand Down

0 comments on commit 9426765

Please sign in to comment.