未验证 提交 bd14926e 编写于 作者: C cyberslack_lee 提交者: GitHub

[xdoctest] reformat example code with google style in No.91-94 (#55832)

* test=docs_preview

* test=docs_preview

* test=docs_preview

* test=docs_preview
上级 97ab6aa6
...@@ -189,20 +189,20 @@ class BeamSearchDecoder(Decoder): ...@@ -189,20 +189,20 @@ class BeamSearchDecoder(Decoder):
.. code-block:: python .. code-block:: python
import numpy as np >>> import numpy as np
import paddle >>> import paddle
from paddle.nn import BeamSearchDecoder, dynamic_decode >>> from paddle.nn import BeamSearchDecoder, dynamic_decode
from paddle.nn import GRUCell, Linear, Embedding >>> from paddle.nn import GRUCell, Linear, Embedding
trg_embeder = Embedding(100, 32) >>> trg_embeder = Embedding(100, 32)
output_layer = Linear(32, 32) >>> output_layer = Linear(32, 32)
decoder_cell = GRUCell(input_size=32, hidden_size=32) >>> decoder_cell = GRUCell(input_size=32, hidden_size=32)
decoder = BeamSearchDecoder(decoder_cell, >>> decoder = BeamSearchDecoder(decoder_cell,
start_token=0, ... start_token=0,
end_token=1, ... end_token=1,
beam_size=4, ... beam_size=4,
embedding_fn=trg_embeder, ... embedding_fn=trg_embeder,
output_fn=output_layer) ... output_fn=output_layer)
...
""" """
def __init__( def __init__(
...@@ -1054,22 +1054,24 @@ def dynamic_decode( ...@@ -1054,22 +1054,24 @@ def dynamic_decode(
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.nn import BeamSearchDecoder, dynamic_decode >>> from paddle.nn import BeamSearchDecoder, dynamic_decode
from paddle.nn import GRUCell, Linear, Embedding >>> from paddle.nn import GRUCell, Linear, Embedding
trg_embeder = Embedding(100, 32) >>> trg_embeder = Embedding(100, 32)
output_layer = Linear(32, 32) >>> output_layer = Linear(32, 32)
decoder_cell = GRUCell(input_size=32, hidden_size=32) >>> decoder_cell = GRUCell(input_size=32, hidden_size=32)
decoder = BeamSearchDecoder(decoder_cell, >>> decoder = BeamSearchDecoder(decoder_cell,
start_token=0, ... start_token=0,
end_token=1, ... end_token=1,
beam_size=4, ... beam_size=4,
embedding_fn=trg_embeder, ... embedding_fn=trg_embeder,
output_fn=output_layer) ... output_fn=output_layer)
encoder_output = paddle.ones((4, 8, 32), dtype=paddle.get_default_dtype()) >>> encoder_output = paddle.ones((4, 8, 32), dtype=paddle.get_default_dtype())
outputs = dynamic_decode(decoder=decoder, >>> outputs = dynamic_decode(decoder=decoder,
inits=decoder_cell.get_initial_states(encoder_output), ... inits=decoder_cell.get_initial_states(encoder_output),
max_step_num=10) ... max_step_num=10)
>>> print(outputs[0].shape)
[4, 11, 4]
""" """
if in_dynamic_mode(): if in_dynamic_mode():
return _dynamic_decode_imperative( return _dynamic_decode_imperative(
......
此差异已折叠。
...@@ -149,29 +149,31 @@ class ConvertibleQuantedLayer(Layer, metaclass=abc.ABCMeta): ...@@ -149,29 +149,31 @@ class ConvertibleQuantedLayer(Layer, metaclass=abc.ABCMeta):
It defines some functions to convert quantizers and observers to quantize It defines some functions to convert quantizers and observers to quantize
or dequantize operators that maintain the quantization parameters used or dequantize operators that maintain the quantization parameters used
during inference. during inference.
Examples: Examples:
.. code-block:: python .. code-block:: python
# Given codes in ./customized_quanter.py >>> # Given codes in ./customized_quanter.py
class CustomizedQuantedLayer(ConvertibleQuantedLayer): >>> class CustomizedQuantedLayer(ConvertibleQuantedLayer):
def __init__(self): ... def __init__(self):
super().__init__() ... super().__init__()
self.weight_a = paddle.create_parameter(shape=[1], dtype='float32') ... self.weight_a = paddle.create_parameter(shape=[1], dtype='float32')
self.weight_b = paddle.create_parameter(shape=[1], dtype='float32') ... self.weight_b = paddle.create_parameter(shape=[1], dtype='float32')
self.quanter_for_weight_a = None ... self.quanter_for_weight_a = None
self.activation_weight = None ... self.activation_weight = None
def forward(self, input): ...
qweight_a = self.quanter_for_weight_a(self.weight_a) ... def forward(self, input):
weight_b = self.weight_b ... qweight_a = self.quanter_for_weight_a(self.weight_a)
qinput = self.activation_weight(input) ... weight_b = self.weight_b
// compute with qweight_a, weight_b and qinput. ... qinput = self.activation_weight(input)
return qweight * qinput + weight_b ... # compute with qweight_a, weight_b and qinput.
... return qweight * qinput + weight_b
def weights_to_quanters(self): ...
return [('weight_a', 'quanter_for_weight_a')] ... def weights_to_quanters(self):
... return [('weight_a', 'quanter_for_weight_a')]
def activation_quanters(self): ...
return ['activation_weight'] ... def activation_quanters(self):
... return ['activation_weight']
""" """
def __init__(self): def __init__(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册