Skip to content

Commit

Permalink
[xdoctest][task 331] reformat example code with google style in pytho…
Browse files Browse the repository at this point in the history
…n/paddle/base/data_feeder.py (PaddlePaddle#57138)

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

* enable static and clean fluid

---------

Co-authored-by: SigureMo <[email protected]>
  • Loading branch information
yoyoIcy and SigureMo authored Sep 18, 2023
1 parent 1b31658 commit e524d81
Showing 1 changed file with 63 additions and 58 deletions.
121 changes: 63 additions & 58 deletions python/paddle/base/data_feeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""

Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit e524d81

Please sign in to comment.