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