未验证 提交 5d26d79f 编写于 作者: O ooooo-create 提交者: GitHub

[xdoctest] reformat example code with google style in No.21-30 (#55849)

* [Doctest]fix No.21, test=docs_preview

* Revert "[Doctest]fix No.21, test=docs_preview"

This reverts commit 76bcdb280e254d682be6fc6f85588f1940bb1ade.

* [Doctest]fix No.21, test=docs_preview

* fix bugs,test=docs_preview

* [Doctest]fix No.22-24,26,27, test=docs_preview

* update fix

* with pre-commit, test=docs_preview

* fix seed, test=docs_preview

* fix error, test=docs_preview

* fix seed, test=docs_preview

* fix seed, test=docs_preview
上级 5e96126d
...@@ -33,23 +33,23 @@ class PyLayerContext: ...@@ -33,23 +33,23 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
# ctx is a object of PyLayerContext. ... # ctx is a object of PyLayerContext.
y = paddle.tanh(x) ... y = paddle.tanh(x)
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# ctx is a object of PyLayerContext. ... # ctx is a object of PyLayerContext.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
""" """
def save_for_backward(self, *tensors): def save_for_backward(self, *tensors):
...@@ -68,24 +68,24 @@ class PyLayerContext: ...@@ -68,24 +68,24 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
# ctx is a context object that store some objects for backward. ... # ctx is a context object that store some objects for backward.
y = paddle.tanh(x) ... y = paddle.tanh(x)
# Pass tensors to backward. ... # Pass tensors to backward.
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# Get the tensors passed by forward. ... # Get the tensors passed by forward.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
""" """
self.container = tensors self.container = tensors
...@@ -101,24 +101,24 @@ class PyLayerContext: ...@@ -101,24 +101,24 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
# ctx is a context object that store some objects for backward. ... # ctx is a context object that store some objects for backward.
y = paddle.tanh(x) ... y = paddle.tanh(x)
# Pass tensors to backward. ... # Pass tensors to backward.
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# Get the tensors passed by forward. ... # Get the tensors passed by forward.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
""" """
return self.container return self.container
...@@ -135,30 +135,31 @@ class PyLayerContext: ...@@ -135,30 +135,31 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
class Exp(paddle.autograd.PyLayer): >>> class Exp(paddle.autograd.PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
ctx.mark_not_inplace(x) ... ctx.mark_not_inplace(x)
return x ... return x
...
@staticmethod ... @staticmethod
def backward(ctx, grad_output): ... def backward(ctx, grad_output):
out = grad_output.exp() ... out = grad_output.exp()
return out ... return out
x = paddle.randn((1, 1)) >>> paddle.seed(2023)
x.stop_gradient = False >>> x = paddle.randn((1, 1))
attn_layers = [] >>> x.stop_gradient = False
for idx in range(0, 2): >>> attn_layers = []
attn_layers.append(Exp()) >>> for idx in range(0, 2):
... attn_layers.append(Exp())
for step in range(0, 2):
a = x >>> for step in range(0, 2):
for j in range(0,2): ... a = x
a = attn_layers[j].apply(x) ... for j in range(0,2):
a.backward() ... a = attn_layers[j].apply(x)
... a.backward()
""" """
self.not_inplace_tensors = args self.not_inplace_tensors = args
...@@ -177,28 +178,28 @@ class PyLayerContext: ...@@ -177,28 +178,28 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
import numpy as np >>> import numpy as np
class Tanh(PyLayer): >>> class Tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
a = x + x ... a = x + x
b = x + x + x ... b = x + x + x
ctx.mark_non_differentiable(a) ... ctx.mark_non_differentiable(a)
return a, b ... return a, b
...
@staticmethod ... @staticmethod
def backward(ctx, grad_a, grad_b): ... def backward(ctx, grad_a, grad_b):
assert np.equal(grad_a.numpy(), paddle.zeros([1]).numpy()) ... assert np.equal(grad_a.numpy(), paddle.zeros([1]).numpy())
assert np.equal(grad_b.numpy(), paddle.ones([1], dtype="float64").numpy()) ... assert np.equal(grad_b.numpy(), paddle.ones([1], dtype="float64").numpy())
return grad_b ... return grad_b
x = paddle.ones([1], dtype="float64") >>> x = paddle.ones([1], dtype="float64")
x.stop_gradient = False >>> x.stop_gradient = False
a, b = Tanh.apply(x) >>> a, b = Tanh.apply(x)
b.sum().backward() >>> b.sum().backward()
""" """
self.non_differentiable = args self.non_differentiable = args
...@@ -216,38 +217,38 @@ class PyLayerContext: ...@@ -216,38 +217,38 @@ class PyLayerContext:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
import numpy as np >>> import numpy as np
class Tanh(PyLayer): >>> class Tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
return x+x+x, x+x ... return x+x+x, x+x
...
@staticmethod ... @staticmethod
def backward(ctx, grad, grad2): ... def backward(ctx, grad, grad2):
assert np.equal(grad2.numpy(), paddle.zeros([1]).numpy()) ... assert np.equal(grad2.numpy(), paddle.zeros([1]).numpy())
return grad ... return grad
class Tanh2(PyLayer): >>> class Tanh2(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
ctx.set_materialize_grads(False) ... ctx.set_materialize_grads(False)
return x+x+x, x+x ... return x+x+x, x+x
...
@staticmethod ... @staticmethod
def backward(ctx, grad, grad2): ... def backward(ctx, grad, grad2):
assert grad2==None ... assert grad2==None
return grad ... return grad
x = paddle.ones([1], dtype="float64") >>> x = paddle.ones([1], dtype="float64")
x.stop_gradient = False >>> x.stop_gradient = False
Tanh.apply(x)[0].backward() >>> Tanh.apply(x)[0].backward()
x2 = paddle.ones([1], dtype="float64") >>> x2 = paddle.ones([1], dtype="float64")
x2.stop_gradient = False >>> x2.stop_gradient = False
Tanh2.apply(x2)[0].backward() >>> Tanh2.apply(x2)[0].backward()
""" """
self.materialize_grads = value self.materialize_grads = value
...@@ -290,30 +291,34 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)): ...@@ -290,30 +291,34 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
y = paddle.tanh(x) ... y = paddle.tanh(x)
# Pass tensors to backward. ... # Pass tensors to backward.
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# Get the tensors passed by forward. ... # Get the tensors passed by forward.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
data = paddle.randn([2, 3], dtype="float64") >>> paddle.seed(2023)
data.stop_gradient = False >>> data = paddle.randn([2, 3], dtype="float64")
z = cus_tanh.apply(data) >>> data.stop_gradient = False
z.mean().backward() >>> z = cus_tanh.apply(data)
>>> z.mean().backward()
print(data.grad)
>>> print(data.grad)
Tensor(shape=[2, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
[[0.16604150, 0.05858341, 0.14051214],
[0.15677770, 0.01564609, 0.02991660]])
""" """
@staticmethod @staticmethod
...@@ -333,23 +338,23 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)): ...@@ -333,23 +338,23 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
y = paddle.tanh(x) ... y = paddle.tanh(x)
# Pass tensors to backward. ... # Pass tensors to backward.
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# Get the tensors passed by forward. ... # Get the tensors passed by forward.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
""" """
raise NotImplementedError( raise NotImplementedError(
"You must implement the forward function for PyLayer." "You must implement the forward function for PyLayer."
...@@ -373,23 +378,23 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)): ...@@ -373,23 +378,23 @@ class PyLayer(with_mateclass(PyLayerMeta, core.eager.PyLayer, PyLayerContext)):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_tanh(PyLayer): >>> class cus_tanh(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, x): ... def forward(ctx, x):
y = paddle.tanh(x) ... y = paddle.tanh(x)
# Pass tensors to backward. ... # Pass tensors to backward.
ctx.save_for_backward(y) ... ctx.save_for_backward(y)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
# Get the tensors passed by forward. ... # Get the tensors passed by forward.
y, = ctx.saved_tensor() ... y, = ctx.saved_tensor()
grad = dy * (1 - paddle.square(y)) ... grad = dy * (1 - paddle.square(y))
return grad ... return grad
""" """
raise NotImplementedError( raise NotImplementedError(
......
...@@ -45,58 +45,58 @@ class saved_tensors_hooks: ...@@ -45,58 +45,58 @@ class saved_tensors_hooks:
Examples: Examples:
.. code-block:: python .. code-block:: python
# Example1 >>> # Example1
import paddle >>> import paddle
def pack_hook(x): >>> def pack_hook(x):
print("Packing", x) ... print("Packing", x)
return x.numpy() ... return x.numpy()
def unpack_hook(x): >>> def unpack_hook(x):
print("UnPacking", x) ... print("UnPacking", x)
return paddle.to_tensor(x) ... return paddle.to_tensor(x)
a = paddle.ones([3,3]) >>> a = paddle.ones([3,3])
b = paddle.ones([3,3]) * 2 >>> b = paddle.ones([3,3]) * 2
a.stop_gradient = False >>> a.stop_gradient = False
b.stop_gradient = False >>> b.stop_gradient = False
with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook): >>> with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook):
y = paddle.multiply(a, b) ... y = paddle.multiply(a, b)
y.sum().backward() >>> y.sum().backward()
# Example2 >>> # Example2
import paddle >>> import paddle
from paddle.autograd import PyLayer >>> from paddle.autograd import PyLayer
class cus_multiply(PyLayer): >>> class cus_multiply(PyLayer):
@staticmethod ... @staticmethod
def forward(ctx, a, b): ... def forward(ctx, a, b):
y = paddle.multiply(a, b) ... y = paddle.multiply(a, b)
ctx.save_for_backward(a, b) ... ctx.save_for_backward(a, b)
return y ... return y
...
@staticmethod ... @staticmethod
def backward(ctx, dy): ... def backward(ctx, dy):
a,b = ctx.saved_tensor() ... a,b = ctx.saved_tensor()
grad_a = dy * a ... grad_a = dy * a
grad_b = dy * b ... grad_b = dy * b
return grad_a, grad_b ... return grad_a, grad_b
def pack_hook(x): >>> def pack_hook(x):
print("Packing", x) ... print("Packing", x)
return x.numpy() ... return x.numpy()
def unpack_hook(x): >>> def unpack_hook(x):
print("UnPacking", x) ... print("UnPacking", x)
return paddle.to_tensor(x) ... return paddle.to_tensor(x)
a = paddle.ones([3,3]) >>> a = paddle.ones([3,3])
b = paddle.ones([3,3]) * 2 >>> b = paddle.ones([3,3]) * 2
a.stop_gradient = False >>> a.stop_gradient = False
b.stop_gradient = False >>> b.stop_gradient = False
with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook): >>> with paddle.autograd.saved_tensors_hooks(pack_hook, unpack_hook):
y = cus_multiply.apply(a, b) ... y = cus_multiply.apply(a, b)
y.sum().backward() >>> y.sum().backward()
""" """
def __init__(self, pack_hook, unpack_hook): def __init__(self, pack_hook, unpack_hook):
......
...@@ -58,15 +58,19 @@ def iinfo(dtype): ...@@ -58,15 +58,19 @@ def iinfo(dtype):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
iinfo_uint8 = paddle.iinfo(paddle.uint8) >>> iinfo_uint8 = paddle.iinfo(paddle.uint8)
print(iinfo_uint8) >>> print(iinfo_uint8)
# paddle.iinfo(min=0, max=255, bits=8, dtype=uint8) paddle.iinfo(min=0, max=255, bits=8, dtype=uint8)
print(iinfo_uint8.min) # 0 >>> print(iinfo_uint8.min)
print(iinfo_uint8.max) # 255 0
print(iinfo_uint8.bits) # 8 >>> print(iinfo_uint8.max)
print(iinfo_uint8.dtype) # uint8 255
>>> print(iinfo_uint8.bits)
8
>>> print(iinfo_uint8.dtype)
uint8
""" """
return core_iinfo(dtype) return core_iinfo(dtype)
...@@ -98,17 +102,25 @@ def finfo(dtype): ...@@ -98,17 +102,25 @@ def finfo(dtype):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
finfo_float32 = paddle.finfo(paddle.float32) >>> finfo_float32 = paddle.finfo(paddle.float32)
print(finfo_float32.min) # -3.40282e+38 >>> print(finfo_float32.min)
print(finfo_float32.max) # 3.40282e+38 -3.4028234663852886e+38
print(finfo_float32.eps) # 1.19209e-07 >>> print(finfo_float32.max)
print(finfo_float32.resolution) # 1e-06 3.4028234663852886e+38
print(finfo_float32.smallest_normal) # 1.17549e-38 >>> print(finfo_float32.eps)
print(finfo_float32.tiny) # 1.17549e-38 1.1920928955078125e-07
print(finfo_float32.bits) # 32 >>> print(finfo_float32.resolution)
print(finfo_float32.dtype) # float32 1e-06
>>> print(finfo_float32.smallest_normal)
1.1754943508222875e-38
>>> print(finfo_float32.tiny)
1.1754943508222875e-38
>>> print(finfo_float32.bits)
32
>>> print(finfo_float32.dtype)
float32
""" """
return core_finfo(dtype) return core_finfo(dtype)
...@@ -35,8 +35,8 @@ def set_default_dtype(d): ...@@ -35,8 +35,8 @@ def set_default_dtype(d):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.set_default_dtype("float32") >>> paddle.set_default_dtype("float32")
""" """
if isinstance(d, type): if isinstance(d, type):
...@@ -76,7 +76,7 @@ def get_default_dtype(): ...@@ -76,7 +76,7 @@ def get_default_dtype():
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
paddle.get_default_dtype() >>> paddle.get_default_dtype()
""" """
return LayerHelperBase.get_default_dtype() return LayerHelperBase.get_default_dtype()
...@@ -677,100 +677,101 @@ def save(obj, path, protocol=4, **configs): ...@@ -677,100 +677,101 @@ def save(obj, path, protocol=4, **configs):
.. code-block:: python .. code-block:: python
:name: code-example-1 :name: code-example-1
# example 1: dynamic graph >>> # example 1: dynamic graph
import paddle >>> import paddle
emb = paddle.nn.Embedding(10, 10) >>> emb = paddle.nn.Embedding(10, 10)
layer_state_dict = emb.state_dict() >>> layer_state_dict = emb.state_dict()
# save state_dict of emb >>> # save state_dict of emb
paddle.save(layer_state_dict, "emb.pdparams") >>> paddle.save(layer_state_dict, "emb.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay( >>> scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True) ... d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam( >>> adam = paddle.optimizer.Adam(
learning_rate=scheduler, ... learning_rate=scheduler,
parameters=emb.parameters()) ... parameters=emb.parameters())
opt_state_dict = adam.state_dict() >>> opt_state_dict = adam.state_dict()
# save state_dict of optimizer >>> # save state_dict of optimizer
paddle.save(opt_state_dict, "adam.pdopt") >>> paddle.save(opt_state_dict, "adam.pdopt")
# save weight of emb >>> # save weight of emb
paddle.save(emb.weight, "emb.weight.pdtensor") >>> paddle.save(emb.weight, "emb.weight.pdtensor")
.. code-block:: python .. code-block:: python
:name: code-example-2 :name: code-example-2
# example 2: Save multiple state_dict at the same time >>> # example 2: Save multiple state_dict at the same time
import paddle >>> import paddle
from paddle import nn >>> from paddle import nn
from paddle.optimizer import Adam >>> from paddle.optimizer import Adam
layer = paddle.nn.Linear(3, 4) >>> layer = paddle.nn.Linear(3, 4)
adam = Adam(learning_rate=0.001, parameters=layer.parameters()) >>> adam = Adam(learning_rate=0.001, parameters=layer.parameters())
obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100} >>> obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100}
path = 'example/model.pdparams' >>> path = 'example/model.pdparams'
paddle.save(obj, path) >>> paddle.save(obj, path)
.. code-block:: python .. code-block:: python
:name: code-example-3 :name: code-example-3
# example 3: static graph >>> # example 3: static graph
import paddle >>> import paddle
import paddle.static as static >>> import paddle.static as static
paddle.enable_static() >>> paddle.enable_static()
# create network >>> # create network
x = paddle.static.data(name="x", shape=[None, 224], dtype='float32') >>> x = paddle.static.data(name="x", shape=[None, 224], dtype='float32')
z = paddle.static.nn.fc(x, 10) >>> z = paddle.static.nn.fc(x, 10)
place = paddle.CPUPlace() >>> place = paddle.CPUPlace()
exe = paddle.static.Executor(place) >>> exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program()) >>> exe.run(paddle.static.default_startup_program())
prog = paddle.static.default_main_program() >>> prog = paddle.static.default_main_program()
for var in prog.list_vars(): >>> for var in prog.list_vars():
if list(var.shape) == [224, 10]: ... if list(var.shape) == [224, 10]:
tensor = var.get_value() ... tensor = var.get_value()
break ... break
# save/load tensor >>> # save/load tensor
path_tensor = 'temp/tensor.pdtensor' >>> path_tensor = 'temp/tensor.pdtensor'
paddle.save(tensor, path_tensor) >>> paddle.save(tensor, path_tensor)
# save/load state_dict >>> # save/load state_dict
path_state_dict = 'temp/model.pdparams' >>> path_state_dict = 'temp/model.pdparams'
paddle.save(prog.state_dict("param"), path_tensor) >>> paddle.save(prog.state_dict("param"), path_tensor)
.. code-block:: python .. code-block:: python
:name: code-example-4 :name: code-example-4
# example 4: save program >>> # example 4: save program
import paddle >>> import paddle
paddle.enable_static() >>> paddle.enable_static()
data = paddle.static.data( >>> data = paddle.static.data(
name='x_static_save', shape=(None, 224), dtype='float32') ... name='x_static_save', shape=(None, 224), dtype='float32')
y_static = z = paddle.static.nn.fc(data, 10) >>> y_static = z = paddle.static.nn.fc(data, 10)
main_program = paddle.static.default_main_program() >>> main_program = paddle.static.default_main_program()
path = "example/main_program.pdmodel" >>> path = "example/main_program.pdmodel"
paddle.save(main_program, path) >>> paddle.save(main_program, path)
.. code-block:: python .. code-block:: python
:name: code-example-5 :name: code-example-5
# example 5: save object to memory >>> # example 5: save object to memory
from io import BytesIO >>> from io import BytesIO
import paddle >>> import paddle
from paddle.nn import Linear >>> from paddle.nn import Linear
paddle.disable_static() >>> paddle.disable_static()
linear = Linear(5, 10) >>> linear = Linear(5, 10)
state_dict = linear.state_dict() >>> state_dict = linear.state_dict()
byio = BytesIO() >>> byio = BytesIO()
paddle.save(state_dict, byio) >>> paddle.save(state_dict, byio)
tensor = paddle.randn([2, 3], dtype='float32') >>> paddle.seed(2023)
paddle.save(tensor, byio) >>> tensor = paddle.randn([2, 3], dtype='float32')
>>> paddle.save(tensor, byio)
''' '''
if _is_file_path(path): if _is_file_path(path):
...@@ -938,115 +939,115 @@ def load(path, **configs): ...@@ -938,115 +939,115 @@ def load(path, **configs):
.. code-block:: python .. code-block:: python
:name: code-example-1 :name: code-example-1
# example 1: dynamic graph >>> # example 1: dynamic graph
import paddle >>> import paddle
emb = paddle.nn.Embedding(10, 10) >>> emb = paddle.nn.Embedding(10, 10)
layer_state_dict = emb.state_dict() >>> layer_state_dict = emb.state_dict()
# save state_dict of emb >>> # save state_dict of emb
paddle.save(layer_state_dict, "emb.pdparams") >>> paddle.save(layer_state_dict, "emb.pdparams")
scheduler = paddle.optimizer.lr.NoamDecay( >>> scheduler = paddle.optimizer.lr.NoamDecay(
d_model=0.01, warmup_steps=100, verbose=True) ... d_model=0.01, warmup_steps=100, verbose=True)
adam = paddle.optimizer.Adam( >>> adam = paddle.optimizer.Adam(
learning_rate=scheduler, ... learning_rate=scheduler,
parameters=emb.parameters()) ... parameters=emb.parameters())
opt_state_dict = adam.state_dict() >>> opt_state_dict = adam.state_dict()
# save state_dict of optimizer >>> # save state_dict of optimizer
paddle.save(opt_state_dict, "adam.pdopt") >>> paddle.save(opt_state_dict, "adam.pdopt")
# save weight of emb >>> # save weight of emb
paddle.save(emb.weight, "emb.weight.pdtensor") >>> paddle.save(emb.weight, "emb.weight.pdtensor")
# load state_dict of emb >>> # load state_dict of emb
load_layer_state_dict = paddle.load("emb.pdparams") >>> load_layer_state_dict = paddle.load("emb.pdparams")
# load state_dict of optimizer >>> # load state_dict of optimizer
load_opt_state_dict = paddle.load("adam.pdopt") >>> load_opt_state_dict = paddle.load("adam.pdopt")
# load weight of emb >>> # load weight of emb
load_weight = paddle.load("emb.weight.pdtensor") >>> load_weight = paddle.load("emb.weight.pdtensor")
.. code-block:: python .. code-block:: python
:name: code-example-2 :name: code-example-2
# example 2: Load multiple state_dict at the same time >>> # example 2: Load multiple state_dict at the same time
import paddle >>> import paddle
from paddle import nn >>> from paddle import nn
from paddle.optimizer import Adam >>> from paddle.optimizer import Adam
layer = paddle.nn.Linear(3, 4) >>> layer = paddle.nn.Linear(3, 4)
adam = Adam(learning_rate=0.001, parameters=layer.parameters()) >>> adam = Adam(learning_rate=0.001, parameters=layer.parameters())
obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100} >>> obj = {'model': layer.state_dict(), 'opt': adam.state_dict(), 'epoch': 100}
path = 'example/model.pdparams' >>> path = 'example/model.pdparams'
paddle.save(obj, path) >>> paddle.save(obj, path)
obj_load = paddle.load(path) >>> obj_load = paddle.load(path)
.. code-block:: python .. code-block:: python
:name: code-example-3 :name: code-example-3
# example 3: static graph >>> # example 3: static graph
import paddle >>> import paddle
import paddle.static as static >>> import paddle.static as static
paddle.enable_static() >>> paddle.enable_static()
# create network >>> # create network
x = paddle.static.data(name="x", shape=[None, 224], dtype='float32') >>> x = paddle.static.data(name="x", shape=[None, 224], dtype='float32')
z = paddle.static.nn.fc(x, 10) >>> z = paddle.static.nn.fc(x, 10)
place = paddle.CPUPlace() >>> place = paddle.CPUPlace()
exe = paddle.static.Executor(place) >>> exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program()) >>> exe.run(paddle.static.default_startup_program())
prog = paddle.static.default_main_program() >>> prog = paddle.static.default_main_program()
for var in prog.list_vars(): >>> for var in prog.list_vars():
if list(var.shape) == [224, 10]: ... if list(var.shape) == [224, 10]:
tensor = var.get_value() ... tensor = var.get_value()
break ... break
# save/load tensor >>> # save/load tensor
path_tensor = 'temp/tensor.pdtensor' >>> path_tensor = 'temp/tensor.pdtensor'
paddle.save(tensor, path_tensor) >>> paddle.save(tensor, path_tensor)
load_tensor = paddle.load(path_tensor) >>> load_tensor = paddle.load(path_tensor)
# save/load state_dict >>> # save/load state_dict
path_state_dict = 'temp/model.pdparams' >>> path_state_dict = 'temp/model.pdparams'
paddle.save(prog.state_dict("param"), path_tensor) >>> paddle.save(prog.state_dict("param"), path_tensor)
load_state_dict = paddle.load(path_tensor) >>> load_state_dict = paddle.load(path_tensor)
.. code-block:: python .. code-block:: python
:name: code-example-4 :name: code-example-4
# example 4: load program >>> # example 4: load program
import paddle >>> import paddle
paddle.enable_static() >>> paddle.enable_static()
data = paddle.static.data( >>> data = paddle.static.data(
name='x_static_save', shape=(None, 224), dtype='float32') ... name='x_static_save', shape=(None, 224), dtype='float32')
y_static = z = paddle.static.nn.fc(data, 10) >>> y_static = z = paddle.static.nn.fc(data, 10)
main_program = paddle.static.default_main_program() >>> main_program = paddle.static.default_main_program()
path = "example/main_program.pdmodel" >>> path = "example/main_program.pdmodel"
paddle.save(main_program, path) >>> paddle.save(main_program, path)
load_main = paddle.load(path) >>> load_main = paddle.load(path)
print(load_main)
.. code-block:: python .. code-block:: python
:name: code-example-5 :name: code-example-5
# example 5: save object to memory >>> # example 5: save object to memory
from io import BytesIO >>> from io import BytesIO
import paddle >>> import paddle
from paddle.nn import Linear >>> from paddle.nn import Linear
paddle.disable_static() >>> paddle.disable_static()
linear = Linear(5, 10) >>> linear = Linear(5, 10)
state_dict = linear.state_dict() >>> state_dict = linear.state_dict()
byio = BytesIO() >>> byio = BytesIO()
paddle.save(state_dict, byio) >>> paddle.save(state_dict, byio)
tensor = paddle.randn([2, 3], dtype='float32') >>> paddle.seed(2023)
paddle.save(tensor, byio) >>> tensor = paddle.randn([2, 3], dtype='float32')
byio.seek(0) >>> paddle.save(tensor, byio)
# load state_dict >>> byio.seek(0)
dict_load = paddle.load(byio) >>> # load state_dict
>>> dict_load = paddle.load(byio)
''' '''
......
...@@ -94,12 +94,13 @@ def is_persistable(var): ...@@ -94,12 +94,13 @@ def is_persistable(var):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP('ValueError: var fc.b not in this block')
import paddle.fluid as fluid >>> import paddle
>>> import paddle.fluid as fluid
paddle.enable_static() >>> paddle.enable_static()
param = fluid.default_main_program().global_block().var('fc.b') >>> param = fluid.default_main_program().global_block().var('fc.b')
res = fluid.io.is_persistable(param) >>> res = fluid.io.is_persistable(param)
""" """
if ( if (
var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH var.desc.type() == core.VarDesc.VarType.FEED_MINIBATCH
...@@ -124,12 +125,13 @@ def is_parameter(var): ...@@ -124,12 +125,13 @@ def is_parameter(var):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> # doctest: +SKIP('ValueError: var fc.w not in this block')
import paddle.fluid as fluid >>> import paddle
>>> import paddle.fluid as fluid
paddle.enable_static() >>> paddle.enable_static()
param = fluid.default_main_program().global_block().var('fc.w') >>> param = fluid.default_main_program().global_block().var('fc.w')
res = fluid.io.is_parameter(param) >>> res = fluid.io.is_parameter(param)
""" """
return isinstance(var, Parameter) return isinstance(var, Parameter)
......
...@@ -34,8 +34,8 @@ def seed(seed): ...@@ -34,8 +34,8 @@ def seed(seed):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
gen = paddle.seed(102) >>> gen = paddle.seed(102)
""" """
# TODO(zhiqiu): 1. remove program.random_seed when all random-related op upgrade # TODO(zhiqiu): 1. remove program.random_seed when all random-related op upgrade
...@@ -75,8 +75,8 @@ def get_rng_state(device=None): ...@@ -75,8 +75,8 @@ def get_rng_state(device=None):
GeneratorState: object. GeneratorState: object.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sts = paddle.get_rng_state() >>> sts = paddle.get_rng_state()
""" """
state_list = [] state_list = []
if device is None: if device is None:
...@@ -129,8 +129,8 @@ def get_cuda_rng_state(): ...@@ -129,8 +129,8 @@ def get_cuda_rng_state():
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sts = paddle.get_cuda_rng_state() >>> sts = paddle.get_cuda_rng_state()
""" """
state_list = [] state_list = []
...@@ -158,9 +158,9 @@ def set_rng_state(state_list, device=None): ...@@ -158,9 +158,9 @@ def set_rng_state(state_list, device=None):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sts = paddle.get_rng_state() >>> sts = paddle.get_rng_state()
paddle.set_rng_state(sts) >>> paddle.set_rng_state(sts)
""" """
if device is None: if device is None:
...@@ -223,9 +223,9 @@ def set_cuda_rng_state(state_list): ...@@ -223,9 +223,9 @@ def set_cuda_rng_state(state_list):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sts = paddle.get_cuda_rng_state() >>> sts = paddle.get_cuda_rng_state()
paddle.set_cuda_rng_state(sts) >>> paddle.set_cuda_rng_state(sts)
""" """
if core.is_compiled_with_cuda(): if core.is_compiled_with_cuda():
......
...@@ -36,29 +36,29 @@ class Bilinear(Initializer): ...@@ -36,29 +36,29 @@ class Bilinear(Initializer):
.. code-block:: python .. code-block:: python
import math >>> import math
import paddle >>> import paddle
import paddle.nn as nn >>> import paddle.nn as nn
from paddle.regularizer import L2Decay >>> from paddle.regularizer import L2Decay
factor = 2 >>> factor = 2
C = 2 >>> C = 2
B = 8 >>> B = 8
H = W = 32 >>> H = W = 32
w_attr = paddle.ParamAttr(learning_rate=0., >>> w_attr = paddle.ParamAttr(learning_rate=0.,
regularizer=L2Decay(0.), ... regularizer=L2Decay(0.),
initializer=nn.initializer.Bilinear()) ... initializer=nn.initializer.Bilinear())
data = paddle.rand([B, 3, H, W], dtype='float32') >>> data = paddle.rand([B, 3, H, W], dtype='float32')
conv_up = nn.Conv2DTranspose(3, >>> conv_up = nn.Conv2DTranspose(3,
out_channels=C, ... out_channels=C,
kernel_size=2 * factor - factor % 2, ... kernel_size=2 * factor - factor % 2,
padding=int( ... padding=int(
math.ceil((factor - 1) / 2.)), ... math.ceil((factor - 1) / 2.)),
stride=factor, ... stride=factor,
weight_attr=w_attr, ... weight_attr=w_attr,
bias_attr=False) ... bias_attr=False)
x = conv_up(data) >>> x = conv_up(data)
Where, `out_channels=C` and `groups=C` means this is channel-wise transposed Where, `out_channels=C` and `groups=C` means this is channel-wise transposed
convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`, convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`,
......
...@@ -153,53 +153,62 @@ class Assign(NumpyArrayInitializer): ...@@ -153,53 +153,62 @@ class Assign(NumpyArrayInitializer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import numpy as np >>> import numpy as np
# numpy array >>> # numpy array
data_1 = paddle.ones(shape=[1, 2], dtype='float32') >>> data_1 = paddle.ones(shape=[1, 2], dtype='float32')
weight_attr_1 = paddle.framework.ParamAttr( >>> weight_attr_1 = paddle.framework.ParamAttr(
name="linear_weight_1", ... name="linear_weight_1",
initializer=paddle.nn.initializer.Assign(np.array([2, 2]))) ... initializer=paddle.nn.initializer.Assign(np.array([2, 2])))
bias_attr_1 = paddle.framework.ParamAttr( >>> bias_attr_1 = paddle.framework.ParamAttr(
name="linear_bias_1", ... name="linear_bias_1",
initializer=paddle.nn.initializer.Assign(np.array([2]))) ... initializer=paddle.nn.initializer.Assign(np.array([2])))
linear_1 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_1, bias_attr=bias_attr_1) >>> linear_1 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_1, bias_attr=bias_attr_1)
# linear_1.weight: [2. 2.] >>> print(linear_1.weight.numpy())
# linear_1.bias: [2.] [2. 2.]
>>> print(linear_1.bias.numpy())
res_1 = linear_1(data_1) [2.]
# res_1: [6.]
>>> res_1 = linear_1(data_1)
# python list >>> print(res_1.numpy())
data_2 = paddle.ones(shape=[1, 2], dtype='float32') [6.]
weight_attr_2 = paddle.framework.ParamAttr(
name="linear_weight_2", >>> # python list
initializer=paddle.nn.initializer.Assign([2, 2])) >>> data_2 = paddle.ones(shape=[1, 2], dtype='float32')
bias_attr_2 = paddle.framework.ParamAttr( >>> weight_attr_2 = paddle.framework.ParamAttr(
name="linear_bias_2", ... name="linear_weight_2",
initializer=paddle.nn.initializer.Assign([2])) ... initializer=paddle.nn.initializer.Assign([2, 2]))
linear_2 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_2, bias_attr=bias_attr_2) >>> bias_attr_2 = paddle.framework.ParamAttr(
# linear_2.weight: [2. 2.] ... name="linear_bias_2",
# linear_2.bias: [2.] ... initializer=paddle.nn.initializer.Assign([2]))
>>> linear_2 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_2, bias_attr=bias_attr_2)
res_2 = linear_2(data_2) >>> print(linear_2.weight.numpy())
# res_2: [6.] [2. 2.]
>>> print(linear_2.bias.numpy())
# tensor [2.]
data_3 = paddle.ones(shape=[1, 2], dtype='float32')
weight_attr_3 = paddle.framework.ParamAttr( >>> res_2 = linear_2(data_2)
name="linear_weight_3", >>> print(res_2.numpy())
initializer=paddle.nn.initializer.Assign(paddle.full([2], 2))) [6.]
bias_attr_3 = paddle.framework.ParamAttr(
name="linear_bias_3", >>> # tensor
initializer=paddle.nn.initializer.Assign(paddle.full([1], 2))) >>> data_3 = paddle.ones(shape=[1, 2], dtype='float32')
linear_3 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_3, bias_attr=bias_attr_3) >>> weight_attr_3 = paddle.framework.ParamAttr(
# linear_3.weight: [2. 2.] ... name="linear_weight_3",
# linear_3.bias: [2.] ... initializer=paddle.nn.initializer.Assign(paddle.full([2], 2)))
>>> bias_attr_3 = paddle.framework.ParamAttr(
res_3 = linear_3(data_3) ... name="linear_bias_3",
# res_3: [6.] ... initializer=paddle.nn.initializer.Assign(paddle.full([1], 2)))
>>> linear_3 = paddle.nn.Linear(2, 2, weight_attr=weight_attr_3, bias_attr=bias_attr_3)
>>> print(linear_3.weight.numpy())
[2. 2.]
>>> print(linear_3.bias.numpy())
[2.]
>>> res_3 = linear_3(data_3)
>>> print(res_3.numpy())
[6.]
""" """
def __init__(self, value, name=None): def __init__(self, value, name=None):
......
...@@ -88,18 +88,20 @@ class Constant(ConstantInitializer): ...@@ -88,18 +88,20 @@ class Constant(ConstantInitializer):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
import paddle.nn as nn >>> import paddle.nn as nn
data = paddle.rand([30, 10, 2], dtype='float32') >>> paddle.seed(2023)
linear = nn.Linear(2, >>> data = paddle.rand([30, 10, 2], dtype='float32')
4, >>> linear = nn.Linear(2,
weight_attr=nn.initializer.Constant(value=2.0)) ... 4,
res = linear(data) ... weight_attr=nn.initializer.Constant(value=2.0))
print(linear.weight) >>> res = linear(data)
# Tensor(shape=[2, 4], dtype=float32, place=Place(gpu:0), stop_gradient=False, >>> print(linear.weight)
# [[2., 2., 2., 2.], Parameter containing:
# [2., 2., 2., 2.]]) Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
[[2., 2., 2., 2.],
[2., 2., 2., 2.]])
""" """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册