From 02a579a9ee066348b6ccefccf54983e33b84fd7f Mon Sep 17 00:00:00 2001 From: LoneRanger <836253168@qq.com> Date: Mon, 11 Sep 2023 11:58:05 +0800 Subject: [PATCH] [xdoctest] reformat example code with google style in No.344-No.352 (#57134) * fix sample codes * fix bug * fix bug --- python/paddle/base/lod_tensor.py | 16 +- python/paddle/base/log_helper.py | 9 +- python/paddle/base/param_attr.py | 47 +- python/paddle/base/reader.py | 631 +++++++++++----------- python/paddle/base/unique_name.py | 62 ++- python/paddle/nn/initializer/lazy_init.py | 34 +- 6 files changed, 410 insertions(+), 389 deletions(-) diff --git a/python/paddle/base/lod_tensor.py b/python/paddle/base/lod_tensor.py index 02058c0f576dc..96e18ec8f3bde 100644 --- a/python/paddle/base/lod_tensor.py +++ b/python/paddle/base/lod_tensor.py @@ -62,10 +62,10 @@ def create_lod_tensor(data, recursive_seq_lens, place): .. code-block:: python - import paddle.base as base - import numpy as np + >>> import paddle.base as base + >>> import numpy as np - t = base.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], base.CPUPlace()) + >>> t = base.create_lod_tensor(np.ndarray([5, 30]), [[2, 3]], base.CPUPlace()) """ if isinstance(data, core.LoDTensor): return create_lod_tensor(np.array(data), recursive_seq_lens, place) @@ -154,13 +154,15 @@ def create_random_int_lodtensor( is inside [low, high]. Examples: + .. code-block:: python - import paddle.base as base + >>> import paddle.base as base - t = base.create_random_int_lodtensor(recursive_seq_lens=[[2, 3]], - base_shape=[30], place=base.CPUPlace(), low=0, high=10) - print(t.shape()) # [5, 30] + >>> t = base.create_random_int_lodtensor(recursive_seq_lens=[[2, 3]], + ... base_shape=[30], place=base.CPUPlace(), low=0, high=10) + >>> print(t.shape()) + [5, 30] """ assert isinstance(base_shape, list), "base_shape should be a list" # append the total number of basic elements to the front of its shape diff --git a/python/paddle/base/log_helper.py b/python/paddle/base/log_helper.py index c78041eabce7b..3d6fd9b1f289d 100644 --- a/python/paddle/base/log_helper.py +++ b/python/paddle/base/log_helper.py @@ -32,10 +32,15 @@ def get_logger(name, level, fmt=None): logging.Logger: logging logger with given settings Examples: + .. code-block:: python - logger = log_helper.get_logger(__name__, logging.INFO, - fmt='%(asctime)s-%(levelname)s: %(message)s') + >>> import paddle + >>> import logging + >>> from paddle.base import log_helper + + >>> logger = log_helper.get_logger(__name__, logging.INFO, + ... fmt='%(asctime)s-%(levelname)s: %(message)s') """ logger = logging.getLogger(name) diff --git a/python/paddle/base/param_attr.py b/python/paddle/base/param_attr.py index 15f2f89b12016..81f5d5470dfe4 100644 --- a/python/paddle/base/param_attr.py +++ b/python/paddle/base/param_attr.py @@ -61,14 +61,15 @@ class ParamAttr: .. code-block:: python - import paddle - - weight_attr = paddle.ParamAttr(name="weight", - learning_rate=0.5, - regularizer=paddle.regularizer.L2Decay(1.0), - trainable=True) - print(weight_attr.name) # "weight" - paddle.nn.Linear(3, 4, weight_attr=weight_attr) + >>> import paddle + + >>> weight_attr = paddle.ParamAttr(name="weight", + ... learning_rate=0.5, + ... regularizer=paddle.regularizer.L2Decay(1.0), + ... trainable=True) + >>> print(weight_attr.name) + weight + >>> paddle.nn.Linear(3, 4, weight_attr=weight_attr) """ def __init__( @@ -259,24 +260,24 @@ class WeightNormParamAttr(ParamAttr): .. code-block:: python - import paddle + >>> import paddle - paddle.enable_static() + >>> paddle.enable_static() - data = paddle.static.data(name="data", shape=[3, 32, 32], dtype="float32") - - fc = paddle.static.nn.fc(x=data, - size=1000, - weight_attr=paddle.static.WeightNormParamAttr( - dim=None, - name='weight_norm_param', - initializer=paddle.nn.initializer.Constant(1.0), - learning_rate=1.0, - regularizer=paddle.regularizer.L2Decay(0.1), - trainable=True, - do_model_average=False, - need_clip=True)) + >>> data = paddle.static.data(name="data", shape=[3, 32, 32], dtype="float32") + >>> fc = paddle.static.nn.fc(x=data, + ... size=1000, + ... weight_attr=paddle.static.WeightNormParamAttr( + ... dim=None, + ... name='weight_norm_param', + ... initializer=paddle.nn.initializer.Constant(1.0), + ... learning_rate=1.0, + ... regularizer=paddle.regularizer.L2Decay(0.1), + ... trainable=True, + ... do_model_average=False, + ... need_clip=True)) + ... """ # List to record the parameters reparameterized by weight normalization. # If these parameters are treated as Variable rather than Parameter, diff --git a/python/paddle/base/reader.py b/python/paddle/base/reader.py index 001c96bd7df26..04dd35fd5a3c0 100644 --- a/python/paddle/base/reader.py +++ b/python/paddle/base/reader.py @@ -463,22 +463,22 @@ def from_dataset(dataset, places, drop_last=True): .. code-block:: python - import paddle - import paddle.static as static + >>> import paddle + >>> import paddle.static as static - paddle.enable_static() + >>> paddle.enable_static() - image = static.data(name='image', shape=[None, 784], dtype='float32') - label = static.data(name='label', shape=[None, 1], dtype='int64') + >>> image = static.data(name='image', shape=[None, 784], dtype='float32') + >>> label = static.data(name='label', shape=[None, 1], dtype='int64') - dataset = paddle.distributed.QueueDataset() - dataset.init( - batch_size=32, - pipe_command='cat', - use_var=[image, label]) - dataset.set_filelist(['a.txt', 'b.txt', 'c.txt']) + >>> dataset = paddle.distributed.QueueDataset() + >>> dataset.init( + ... batch_size=32, + ... pipe_command='cat', + ... use_var=[image, label]) + >>> dataset.set_filelist(['a.txt', 'b.txt', 'c.txt']) - loader = paddle.base.io.DataLoader.from_dataset(dataset, static.cpu_places()) + >>> loader = paddle.base.io.DataLoader.from_dataset(dataset, static.cpu_places()) """ return DatasetLoader(dataset, places, drop_last) @@ -1150,6 +1150,7 @@ class PyReader(DataLoaderBase): reader(Reader) Examples: + 1. If iterable = False, the created PyReader object is almost the same as :code:`base.layers.py_reader()`. Operators would be inserted into the program. User should call :code:`start()` @@ -1160,56 +1161,56 @@ class PyReader(DataLoaderBase): .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - paddle.enable_static() - - EPOCH_NUM = 3 - ITER_NUM = 5 - BATCH_SIZE = 3 - - def network(image, label): - # User-defined network, here is an example of softmax regression. - predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') - return paddle.nn.functional.cross_entropy( - input=predict, label=label, - reduction='none', use_softmax=False - ) - - def reader_creator_random_image_and_label(height, width): - def reader(): - for i in range(ITER_NUM): - fake_image = np.random.uniform(low=0, - high=255, - size=[height, width]) - fake_label = np.ones([1]) - yield fake_image, fake_label - return reader - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - - reader = base.io.PyReader(feed_list=[image, label], - capacity=4, - iterable=False) - - user_defined_reader = reader_creator_random_image_and_label(784, 784) - reader.decorate_sample_list_generator( - paddle.batch(user_defined_reader, batch_size=BATCH_SIZE)) - loss = network(image, label) - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - for i in range(EPOCH_NUM): - reader.start() - while True: - try: - executor.run(feed=None) - except base.core.EOFException: - reader.reset() - break - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> EPOCH_NUM = 3 + >>> ITER_NUM = 5 + >>> BATCH_SIZE = 3 + + >>> def network(image, label): + ... # User-defined network, here is an example of softmax regression. + ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') + ... return paddle.nn.functional.cross_entropy( + ... input=predict, label=label, + ... reduction='none', use_softmax=False + ... ) + ... + >>> def reader_creator_random_image_and_label(height, width): + ... def reader(): + ... for i in range(ITER_NUM): + ... fake_image = np.random.uniform(low=0, + ... high=255, + ... size=[height, width]) + ... fake_label = np.ones([1]) + ... yield fake_image, fake_label + ... return reader + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + + >>> reader = base.io.PyReader(feed_list=[image, label], + ... capacity=4, + ... iterable=False) + ... + >>> user_defined_reader = reader_creator_random_image_and_label(784, 784) + >>> reader.decorate_sample_list_generator( + ... paddle.batch(user_defined_reader, batch_size=BATCH_SIZE)) + >>> loss = network(image, label) + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + >>> for i in range(EPOCH_NUM): + ... reader.start() + ... while True: + ... try: + ... executor.run(feed=None) + ... except base.core.EOFException: + ... reader.reset() + ... break + ... 2. If iterable=True, the created PyReader object is decoupled with the program. No operator would be inserted into the program. @@ -1219,78 +1220,78 @@ def reader(): .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - paddle.enable_static() - - EPOCH_NUM = 3 - ITER_NUM = 5 - BATCH_SIZE = 10 - - def network(image, label): - # User-defined network, here is an example of softmax regression. - predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') - return paddle.nn.functional.cross_entropy( - input=predict, label=label, - reduction='none', use_softmax=False - ) - - def reader_creator_random_image(height, width): - def reader(): - for i in range(ITER_NUM): - fake_image = np.random.uniform(low=0, high=255, size=[height, width]) - fake_label = np.ones([1]) - yield fake_image, fake_label - return reader - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True, return_list=False) - - user_defined_reader = reader_creator_random_image(784, 784) - reader.decorate_sample_list_generator( - paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), - base.core.CPUPlace()) - - loss = network(image, label) - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - - for _ in range(EPOCH_NUM): - for data in reader(): - executor.run(feed=data, fetch_list=[loss]) - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> EPOCH_NUM = 3 + >>> ITER_NUM = 5 + >>> BATCH_SIZE = 10 + + >>> def network(image, label): + ... # User-defined network, here is an example of softmax regression. + ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') + ... return paddle.nn.functional.cross_entropy( + ... input=predict, label=label, + ... reduction='none', use_softmax=False + ... ) + ... + >>> def reader_creator_random_image(height, width): + ... def reader(): + ... for i in range(ITER_NUM): + ... fake_image = np.random.uniform(low=0, high=255, size=[height, width]) + ... fake_label = np.ones([1]) + ... yield fake_image, fake_label + ... return reader + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + >>> reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True, return_list=False) + + >>> user_defined_reader = reader_creator_random_image(784, 784) + >>> reader.decorate_sample_list_generator( + ... paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), + ... base.core.CPUPlace()) + ... + >>> loss = network(image, label) + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + + >>> for _ in range(EPOCH_NUM): + ... for data in reader(): + ... executor.run(feed=data, fetch_list=[loss]) + ... 3. If return_list=True, the return values would be presented as list instead of dict. This is usually used in dygraph mode. .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - ITER_NUM = 5 - BATCH_SIZE = 10 - - def reader_creator_random_image(height, width): - def reader(): - for i in range(ITER_NUM): - yield np.random.uniform(low=0, high=255, size=[height, width]), \ - np.random.random_integers(low=0, high=9, size=[1]) - return reader - - place = base.CPUPlace() - with base.dygraph.guard(place): - py_reader = base.io.PyReader(capacity=2, return_list=True) - user_defined_reader = reader_creator_random_image(784, 784) - py_reader.decorate_sample_list_generator( - paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), - place) - for image, label in py_reader(): - relu = paddle.nn.functional.relu(image) + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> ITER_NUM = 5 + >>> BATCH_SIZE = 10 + + >>> def reader_creator_random_image(height, width): + ... def reader(): + ... for i in range(ITER_NUM): + ... yield np.random.uniform(low=0, high=255, size=[height, width]), \ + ... np.random.random_integers(low=0, high=9, size=[1]) + ... return reader + ... + >>> place = base.CPUPlace() + >>> with base.dygraph.guard(place): + ... py_reader = base.io.PyReader(capacity=2, return_list=True) + ... user_defined_reader = reader_creator_random_image(784, 784) + ... py_reader.decorate_sample_list_generator( + ... paddle.batch(user_defined_reader, batch_size=BATCH_SIZE), + ... place) + ... for image, label in py_reader(): + ... relu = paddle.nn.functional.relu(image) """ def __init__( @@ -1327,32 +1328,34 @@ def start(self): Example: .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - BATCH_SIZE = 10 - - def generator(): - for i in range(5): - yield np.random.uniform(low=0, high=255, size=[784, 784]), - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) - reader.decorate_sample_list_generator( - paddle.batch(generator, batch_size=BATCH_SIZE)) - - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - for i in range(3): - reader.start() - while True: - try: - executor.run(feed=None) - except base.core.EOFException: - reader.reset() - break - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> BATCH_SIZE = 10 + + >>> def generator(): + ... for i in range(5): + ... yield np.random.uniform(low=0, high=255, size=[784, 784]), + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) + >>> reader.decorate_sample_list_generator( + ... paddle.batch(generator, batch_size=BATCH_SIZE)) + ... + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + >>> for i in range(3): + ... reader.start() + ... while True: + ... try: + ... executor.run(feed=None) + ... except base.core.EOFException: + ... reader.reset() + ... break + ... ''' self._loader.start() @@ -1364,32 +1367,34 @@ def reset(self): Example: .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - BATCH_SIZE = 10 - - def generator(): - for i in range(5): - yield np.random.uniform(low=0, high=255, size=[784, 784]), - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) - reader.decorate_sample_list_generator( - paddle.batch(generator, batch_size=BATCH_SIZE)) - - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - for i in range(3): - reader.start() - while True: - try: - executor.run(feed=None) - except base.core.EOFException: - reader.reset() - break - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> BATCH_SIZE = 10 + + >>> def generator(): + ... for i in range(5): + ... yield np.random.uniform(low=0, high=255, size=[784, 784]), + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> reader = base.io.PyReader(feed_list=[image], capacity=4, iterable=False) + >>> reader.decorate_sample_list_generator( + ... paddle.batch(generator, batch_size=BATCH_SIZE)) + ... + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + >>> for i in range(3): + ... reader.start() + ... while True: + ... try: + ... executor.run(feed=None) + ... except base.core.EOFException: + ... reader.reset() + ... break + ... ''' self._loader.reset() @@ -1419,48 +1424,50 @@ def decorate_sample_generator( Example: .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - EPOCH_NUM = 3 - ITER_NUM = 15 - BATCH_SIZE = 3 - - def network(image, label): - # User-defined network, here is an example of softmax regression. - predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') - return paddle.nn.functional.cross_entropy( - input=predict, label=label, - reduction='none', use_softmax=False - ) - - def random_image_and_label_generator(height, width): - def generator(): - for i in range(ITER_NUM): - fake_image = np.random.uniform(low=0, - high=255, - size=[height, width]) - fake_label = np.array([1]) - yield fake_image, fake_label - return generator - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) - - user_defined_generator = random_image_and_label_generator(784, 784) - reader.decorate_sample_generator(user_defined_generator, - batch_size=BATCH_SIZE, - places=[base.CPUPlace()]) - loss = network(image, label) - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - - for _ in range(EPOCH_NUM): - for data in reader(): - executor.run(feed=data, fetch_list=[loss]) - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> EPOCH_NUM = 3 + >>> ITER_NUM = 15 + >>> BATCH_SIZE = 3 + + >>> def network(image, label): + ... # User-defined network, here is an example of softmax regression. + ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') + ... return paddle.nn.functional.cross_entropy( + ... input=predict, label=label, + ... reduction='none', use_softmax=False + ... ) + ... + >>> def random_image_and_label_generator(height, width): + ... def generator(): + ... for i in range(ITER_NUM): + ... fake_image = np.random.uniform(low=0, + ... high=255, + ... size=[height, width]) + ... fake_label = np.array([1]) + ... yield fake_image, fake_label + ... return generator + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + >>> reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + + >>> user_defined_generator = random_image_and_label_generator(784, 784) + >>> reader.decorate_sample_generator(user_defined_generator, + ... batch_size=BATCH_SIZE, + ... places=[base.CPUPlace()]) + >>> loss = network(image, label) + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + + >>> for _ in range(EPOCH_NUM): + ... for data in reader(): + ... executor.run(feed=data, fetch_list=[loss]) + ... ''' self._loader.set_sample_generator( sample_generator, batch_size, drop_last, places @@ -1484,51 +1491,51 @@ def decorate_sample_list_generator(self, reader, places=None): Example: .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - paddle.enable_static() - - EPOCH_NUM = 3 - ITER_NUM = 15 - BATCH_SIZE = 3 - - def network(image, label): - # User-defined network, here is an example of softmax regression. - predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') - return paddle.nn.functional.cross_entropy( - input=predict, label=label, - reduction='none', use_softmax=False - ) - - def random_image_and_label_generator(height, width): - def generator(): - for i in range(ITER_NUM): - fake_image = np.random.uniform(low=0, - high=255, - size=[height, width]) - fake_label = np.ones([1]) - yield fake_image, fake_label - return generator - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) - - user_defined_generator = random_image_and_label_generator(784, 784) - reader.decorate_sample_list_generator( - paddle.batch(user_defined_generator, batch_size=BATCH_SIZE), - base.core.CPUPlace()) - - loss = network(image, label) - executor = base.Executor(base.core.CPUPlace()) - executor.run(base.default_startup_program()) - - for _ in range(EPOCH_NUM): - for data in reader(): - executor.run(feed=data, fetch_list=[loss]) - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> EPOCH_NUM = 3 + >>> ITER_NUM = 15 + >>> BATCH_SIZE = 3 + + >>> def network(image, label): + ... # User-defined network, here is an example of softmax regression. + ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') + ... return paddle.nn.functional.cross_entropy( + ... input=predict, label=label, + ... reduction='none', use_softmax=False + ... ) + ... + >>> def random_image_and_label_generator(height, width): + ... def generator(): + ... for i in range(ITER_NUM): + ... fake_image = np.random.uniform(low=0, + ... high=255, + ... size=[height, width]) + ... fake_label = np.ones([1]) + ... yield fake_image, fake_label + ... return generator + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + >>> reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + + >>> user_defined_generator = random_image_and_label_generator(784, 784) + >>> reader.decorate_sample_list_generator( + ... paddle.batch(user_defined_generator, batch_size=BATCH_SIZE), + ... base.core.CPUPlace()) + ... + >>> loss = network(image, label) + >>> executor = base.Executor(base.core.CPUPlace()) + >>> executor.run(base.default_startup_program()) + + >>> for _ in range(EPOCH_NUM): + ... for data in reader(): + ... executor.run(feed=data, fetch_list=[loss]) + ... ''' self._loader.set_sample_list_generator(reader, places) @@ -1550,51 +1557,51 @@ def decorate_batch_generator(self, reader, places=None): Example: .. code-block:: python - import paddle - import paddle.base as base - import numpy as np - - paddle.enable_static() - - EPOCH_NUM = 3 - ITER_NUM = 15 - BATCH_SIZE = 3 - - def network(image, label): - # User-defined network, here is an example of softmax regression. - predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') - return paddle.nn.functional.cross_entropy( - input=predict, label=label, - reduction='none', use_softmax=False - ) - - def random_image_and_label_generator(height, width): - def generator(): - for i in range(ITER_NUM): - batch_image = np.random.uniform(low=0, - high=255, - size=[BATCH_SIZE, height, width]) - batch_label = np.ones([BATCH_SIZE, 1]) - batch_image = batch_image.astype('float32') - batch_label = batch_label.astype('int64') - yield batch_image, batch_label - return generator - - image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') - label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') - reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) - - user_defined_generator = random_image_and_label_generator(784, 784) - reader.decorate_batch_generator(user_defined_generator, base.CPUPlace()) - - loss = network(image, label) - executor = base.Executor(base.CPUPlace()) - executor.run(base.default_startup_program()) - - for _ in range(EPOCH_NUM): - for data in reader(): - executor.run(feed=data, fetch_list=[loss]) - + >>> import paddle + >>> import paddle.base as base + >>> import numpy as np + + >>> paddle.enable_static() + + >>> EPOCH_NUM = 3 + >>> ITER_NUM = 15 + >>> BATCH_SIZE = 3 + + >>> def network(image, label): + ... # User-defined network, here is an example of softmax regression. + ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') + ... return paddle.nn.functional.cross_entropy( + ... input=predict, label=label, + ... reduction='none', use_softmax=False + ... ) + ... + >>> def random_image_and_label_generator(height, width): + ... def generator(): + ... for i in range(ITER_NUM): + ... batch_image = np.random.uniform(low=0, + ... high=255, + ... size=[BATCH_SIZE, height, width]) + ... batch_label = np.ones([BATCH_SIZE, 1]) + ... batch_image = batch_image.astype('float32') + ... batch_label = batch_label.astype('int64') + ... yield batch_image, batch_label + ... return generator + ... + >>> image = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32') + >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') + >>> reader = base.io.PyReader(feed_list=[image, label], capacity=4, iterable=True) + + >>> user_defined_generator = random_image_and_label_generator(784, 784) + >>> reader.decorate_batch_generator(user_defined_generator, base.CPUPlace()) + + >>> loss = network(image, label) + >>> executor = base.Executor(base.CPUPlace()) + >>> executor.run(base.default_startup_program()) + + >>> for _ in range(EPOCH_NUM): + ... for data in reader(): + ... executor.run(feed=data, fetch_list=[loss]) + ... ''' self._loader.set_batch_generator(reader, places) diff --git a/python/paddle/base/unique_name.py b/python/paddle/base/unique_name.py index 863883a220c67..745675767f150 100644 --- a/python/paddle/base/unique_name.py +++ b/python/paddle/base/unique_name.py @@ -93,10 +93,11 @@ def generate(key): .. code-block:: python - import paddle - name1 = paddle.utils.unique_name.generate('fc') - name2 = paddle.utils.unique_name.generate('fc') - print(name1, name2) # fc_0, fc_1 + >>> import paddle + >>> name1 = paddle.utils.unique_name.generate('fc') + >>> name2 = paddle.utils.unique_name.generate('fc') + >>> print(name1, name2) + fc_0 fc_1 """ return generator(key) @@ -151,18 +152,21 @@ def switch(new_generator=None, new_para_name_checker=None): .. code-block:: python - import paddle - name1 = paddle.utils.unique_name.generate('fc') - name2 = paddle.utils.unique_name.generate('fc') - print(name1, name2) # fc_0, fc_1 - - pre_generator, pre_dygraph_name_checker = paddle.utils.unique_name.switch() # switch to a new anonymous namespace. - name2 = paddle.utils.unique_name.generate('fc') - print(name2) # fc_0 - - paddle.utils.unique_name.switch(pre_generator, pre_dygraph_name_checker) # switch back to pre_generator. - name3 = paddle.utils.unique_name.generate('fc') - print(name3) # fc_2, since pre_generator has generated fc_0, fc_1. + >>> import paddle + >>> name1 = paddle.utils.unique_name.generate('fc') + >>> name2 = paddle.utils.unique_name.generate('fc') + >>> print(name1, name2) + fc_0 fc_1 + + >>> pre_generator, pre_dygraph_name_checker = paddle.utils.unique_name.switch() # switch to a new anonymous namespace. + >>> name2 = paddle.utils.unique_name.generate('fc') + >>> print(name2) + fc_0 + + >>> paddle.utils.unique_name.switch(pre_generator, pre_dygraph_name_checker) # switch back to pre_generator. + >>> name3 = paddle.utils.unique_name.generate('fc') + >>> print(name3) + fc_2 """ global generator old_generator = generator @@ -200,18 +204,20 @@ def guard(new_generator=None): .. code-block:: python - import paddle - with paddle.utils.unique_name.guard(): - name_1 = paddle.utils.unique_name.generate('fc') - with paddle.utils.unique_name.guard(): - name_2 = paddle.utils.unique_name.generate('fc') - print(name_1, name_2) # fc_0, fc_0 - - with paddle.utils.unique_name.guard('A'): - name_1 = paddle.utils.unique_name.generate('fc') - with paddle.utils.unique_name.guard('B'): - name_2 = paddle.utils.unique_name.generate('fc') - print(name_1, name_2) # Afc_0, Bfc_0 + >>> import paddle + >>> with paddle.utils.unique_name.guard(): + ... name_1 = paddle.utils.unique_name.generate('fc') + >>> with paddle.utils.unique_name.guard(): + ... name_2 = paddle.utils.unique_name.generate('fc') + >>> print(name_1, name_2) + fc_0 fc_0 + + >>> with paddle.utils.unique_name.guard('A'): + ... name_1 = paddle.utils.unique_name.generate('fc') + >>> with paddle.utils.unique_name.guard('B'): + ... name_2 = paddle.utils.unique_name.generate('fc') + >>> print(name_1, name_2) + Afc_0 Bfc_0 """ if isinstance(new_generator, str): new_generator = UniqueNameGenerator(new_generator) diff --git a/python/paddle/nn/initializer/lazy_init.py b/python/paddle/nn/initializer/lazy_init.py index 7c67649738203..6c037bab8f035 100644 --- a/python/paddle/nn/initializer/lazy_init.py +++ b/python/paddle/nn/initializer/lazy_init.py @@ -98,16 +98,16 @@ class LazyGuard: .. code-block:: python - from paddle import LazyGuard - from paddle.nn import Linear - - with LazyGuard(): - # w and b are initialized lazily and have no memory. - net = Linear(10, 10) - - for param in net.parameters(): - # Initialize param and allocate memory explicitly. - param.initialize() + >>> from paddle import LazyGuard + >>> from paddle.nn import Linear + + >>> with LazyGuard(): + ... # w and b are initialized lazily and have no memory. + ... net = Linear(10, 10) + ... + >>> for param in net.parameters(): + ... # Initialize param and allocate memory explicitly. + ... param.initialize() """ def __enter__(self): @@ -118,14 +118,14 @@ def __enter__(self): .. code-block:: python - from paddle import LazyGuard - from paddle.nn import Linear - - with LazyGuard(): - fc = LazyInit(Linear)(10, 10) + >>> from paddle import LazyGuard + >>> from paddle.nn import Linear - for param in fc.parameters(): - param.initialize() + >>> with LazyGuard(): + ... fc = LazyInit(Linear)(10, 10) + ... + >>> for param in fc.parameters(): + ... param.initialize() """ lazy_init_helper().enable()