From e524d81d14193c8db631a9850ef42ab32f4769eb Mon Sep 17 00:00:00 2001 From: yoyoIcy <142126786+yoyoIcy@users.noreply.github.com> Date: Sun, 17 Sep 2023 21:30:37 -0500 Subject: [PATCH] [xdoctest][task 331] reformat example code with google style in python/paddle/base/data_feeder.py (#57138) * [Doctest]fix No.331, test=docs_preview * enable static and clean fluid --------- Co-authored-by: SigureMo --- python/paddle/base/data_feeder.py | 121 ++++++++++++++++-------------- 1 file changed, 63 insertions(+), 58 deletions(-) diff --git a/python/paddle/base/data_feeder.py b/python/paddle/base/data_feeder.py index 0029c6f35a9aee..e65e0dcd334f31 100644 --- a/python/paddle/base/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -375,40 +375,43 @@ class DataFeeder: :code:`ValueError` - If some Variables are not in this Program. Example: - .. code-block:: python - - import numpy as np - import paddle - import paddle.base as base - - place = base.CPUPlace() - def reader(): - for _ in range(4): - yield np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32'), - - main_program = base.Program() - startup_program = base.Program() - - with base.program_guard(main_program, startup_program): - data_1 = paddle.static.data(name='data_1', shape=[None, 2, 2], dtype='float32') - data_2 = paddle.static.data(name='data_2', shape=[None, 1, 3], dtype='float32') - out = paddle.static.nn.fc(x=[data_1, data_2], size=2) - # ... - feeder = base.DataFeeder([data_1, data_2], place) - - exe = base.Executor(place) - exe.run(startup_program) - - feed_data = feeder.feed(reader()) - - # print feed_data to view feed results - # print(feed_data['data_1']) - # print(feed_data['data_2']) - - outs = exe.run(program=main_program, - feed=feed_data, - fetch_list=[out]) - print(outs) + .. code-block:: python + + >>> import numpy as np + >>> import paddle + >>> from paddle import base + + >>> paddle.enable_static() + >>> place = paddle.CPUPlace() + >>> def reader(): + ... for _ in range(4): + ... yield np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32'), + ... + >>> main_program = paddle.static.Program() + >>> startup_program = paddle.static.Program() + + >>> with paddle.static.program_guard(main_program, startup_program): + ... data_1 = paddle.static.data(name='data_1', shape=[None, 2, 2], dtype='float32') + ... data_2 = paddle.static.data(name='data_2', shape=[None, 1, 3], dtype='float32') + ... out = paddle.static.nn.fc(x=[data_1, data_2], size=2) + ... # ... + >>> feeder = base.DataFeeder([data_1, data_2], place) + + >>> exe = paddle.static.Executor(place) + >>> exe.run(startup_program) + + >>> feed_data = feeder.feed(reader()) + + >>> # print feed_data to view feed results + >>> # print(feed_data['data_1']) + >>> # print(feed_data['data_2']) + + >>> outs = exe.run( + ... program=main_program, + ... feed=feed_data, + ... fetch_list=[out] + ... ) + >>> print(outs) """ @@ -443,30 +446,32 @@ def feed(self, iterable): :code:`dict`: a :code:`dict` that contains (variable name - converted tensor) pairs Example: - .. code-block:: python - - # In this example, reader - generator will return a list of ndarray of 3 elements - # feed API will convert each ndarray input into a tensor - # the return result is a dict with keys: data_1, data_2, data_3 - # result['data_1'] a LoD-Tensor with shape of [5, 2, 1, 3]. 5 is batch size, and [2, 1, 3] is the real shape of data_1. - # result['data_2'], result['data_3'] are similar. - import numpy as np - import paddle.base as base - - def reader(limit=5): - for i in range(1, limit + 1): - yield np.ones([6]).astype('float32') * i , np.ones([1]).astype('int64') * i, np.random.random([9]).astype('float32') - - data_1 = paddle.static.data(name='data_1', shape=[None, 2, 1, 3]) - data_2 = paddle.static.data(name='data_2', shape=[None, 1], dtype='int64') - data_3 = paddle.static.data(name='data_3', shape=[None, 3, 3], dtype='float32') - feeder = base.DataFeeder(['data_1','data_2', 'data_3'], base.CPUPlace()) - - - result = feeder.feed(reader()) - print(result['data_1']) - print(result['data_2']) - print(result['data_3']) + .. code-block:: python + + >>> # In this example, reader - generator will return a list of ndarray of 3 elements + >>> # feed API will convert each ndarray input into a tensor + >>> # the return result is a dict with keys: data_1, data_2, data_3 + >>> # result['data_1'] a LoD-Tensor with shape of [5, 2, 1, 3]. 5 is batch size, and [2, 1, 3] is the real shape of data_1. + >>> # result['data_2'], result['data_3'] are similar. + >>> import numpy as np + >>> import paddle + >>> from paddle import base + + >>> paddle.enable_static() + + >>> def reader(limit=5): + ... for i in range(1, limit + 1): + ... yield np.ones([6]).astype('float32') * i , np.ones([1]).astype('int64') * i, np.random.random([9]).astype('float32') + ... + >>> data_1 = paddle.static.data(name='data_1', shape=[None, 2, 1, 3]) + >>> data_2 = paddle.static.data(name='data_2', shape=[None, 1], dtype='int64') + >>> data_3 = paddle.static.data(name='data_3', shape=[None, 3, 3], dtype='float32') + >>> feeder = base.DataFeeder(['data_1','data_2', 'data_3'], paddle.CPUPlace()) + + >>> result = feeder.feed(reader()) + >>> print(result['data_1']) + >>> print(result['data_2']) + >>> print(result['data_3']) """ converter = []