Skip to content

Commit d020d7f

Browse files
authored
add beam search doc (#11469)
1 parent 5972990 commit d020d7f

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

paddle/fluid/operators/activation_op.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Tanh Activation Operator.
143143
__attribute__((unused)) constexpr char TanhShrinkDoc[] = R"DOC(
144144
TanhShrink Activation Operator.
145145
146-
$$out = x - \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
146+
$$out = x - \\frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}$$
147147
148148
)DOC";
149149

@@ -385,7 +385,7 @@ class STanhOpMaker : public framework::OpProtoAndCheckerMaker {
385385
AddComment(R"DOC(
386386
STanh Activation Operator.
387387
388-
$$out = b * \frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
388+
$$out = b * \\frac{e^{a * x} - e^{-a * x}}{e^{a * x} + e^{-a * x}}$$
389389
390390
)DOC");
391391
}

python/paddle/fluid/layers/control_flow.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ def Print(input,
185185
Returns:
186186
Variable: Output tensor, same data with input tensor.
187187
188+
188189
Examples:
190+
189191
.. code-block:: python
190192
191-
value = some_layer(...)
192-
Print(value, summarize=10,
193-
message="The content of some_layer: ")
193+
value = some_layer(...)
194+
Print(value, summarize=10,
195+
message="The content of some_layer: ")
194196
'''
195197
helper = LayerHelper('print', **locals())
196198
out = helper.create_tmp_variable(dtype=helper.input_dtype())
@@ -1201,6 +1203,31 @@ def __exit__(self, exc_type, exc_val, exc_tb):
12011203

12021204

12031205
class ConditionalBlock(object):
1206+
'''
1207+
**ConditionalBlock**
1208+
1209+
ConditionalBlock is an operator that bind a block to a specific condition,
1210+
if the condition matches, the corresponding block will be executed.
1211+
1212+
Args:
1213+
inputs (Variable): bool conditions.
1214+
is_scalar_condition (bool): whether the branch is controled by a scalar.
1215+
name(str): name of this ConditionalBlock.
1216+
1217+
Examples:
1218+
.. code-block:: python
1219+
1220+
cond = layers.less_than(x=label, y=limit)
1221+
true_image, false_image = layers.split_lod_tensor(
1222+
input=image, mask=cond)
1223+
true_cond = layers.ConditionalBlock([true_image])
1224+
1225+
with true_cond.block():
1226+
...
1227+
with false_cond.block():
1228+
...
1229+
'''
1230+
12041231
def __init__(self, inputs, is_scalar_condition=False, name=None):
12051232
for each_input in inputs:
12061233
if not isinstance(each_input, Variable):

python/paddle/fluid/layers/nn.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,18 +2678,35 @@ def sequence_expand(x, y, ref_level=-1, name=None):
26782678

26792679
def beam_search(pre_ids, ids, scores, beam_size, end_id, level=0):
26802680
'''
2681+
**beam search**
2682+
26812683
This function implements the beam search algorithm.
26822684
2685+
Beam search is a classical algorithm for selecting candidate words
2686+
in a machine translation task.
2687+
2688+
Refer to `Beam search <https://en.wikipedia.org/wiki/Beam_search>`_
2689+
for more details.
2690+
26832691
Args:
2684-
pre_ids (Variable): ${pre_ids_comment}
2685-
ids (Variable): ${ids_comment}
2686-
scores (Variable): ${scores_comment}
2687-
beam_size (int): ${beam_size_comment}
2688-
end_id (int): ${end_id_comment}
2689-
level (int): ${level_comment}
2692+
pre_ids (Variable): ids in previous step.
2693+
ids (Variable): a LoDTensor of shape of [None,k]
2694+
scores (Variable): a LoDTensor that has the same shape and LoD with `ids`
2695+
beam_size (int): beam size for beam search
2696+
end_id (int): the token id which indicates the end of a sequence
2697+
level (int): the level of LoDTensor
26902698
26912699
Returns:
2692-
tuple: a tuple of beam_search output variables: selected_ids, selected_scores
2700+
tuple: a tuple of beam_search output variables: `selected_ids`, `selected_scores`
2701+
2702+
Examples:
2703+
.. code-block:: python
2704+
2705+
# current_score is a Tensor of shape (num_batch_size, embed_size), which
2706+
# consists score of each candidate word.
2707+
topk_scores, topk_indices = pd.topk(current_score, k=50)
2708+
selected_ids, selected_scores = pd.beam_search(
2709+
pre_ids, topk_indices, topk_scores, beam_size, end_id=10, level=0)
26932710
'''
26942711
helper = LayerHelper('beam_search', **locals())
26952712
score_type = scores.dtype

0 commit comments

Comments
 (0)