Set the data source of the PyReader object. The provided :code:`sample_generator` should be a Python generator, which yields list(numpy.ndarray)-typed data of each sample. :code:`places` must be set when the PyReader object is iterable. If all inputs have
(
self, sample_generator, batch_size, drop_last=True, places=None
)
| 1410 | self._loader.reset() |
| 1411 | |
| 1412 | def decorate_sample_generator( |
| 1413 | self, sample_generator, batch_size, drop_last=True, places=None |
| 1414 | ): |
| 1415 | ''' |
| 1416 | Set the data source of the PyReader object. |
| 1417 | |
| 1418 | The provided :code:`sample_generator` should be a Python generator, |
| 1419 | which yields list(numpy.ndarray)-typed data of each sample. |
| 1420 | |
| 1421 | :code:`places` must be set when the PyReader object is iterable. |
| 1422 | |
| 1423 | If all inputs have no lods, this method is faster than |
| 1424 | :code:`decorate_sample_list_generator(paddle.batch(sample_generator, ...))` . |
| 1425 | |
| 1426 | Args: |
| 1427 | sample_generator (generator): Python generator that yields |
| 1428 | list(numpy.ndarray)-typed sample data. |
| 1429 | batch_size (int): batch size. Must be larger than 0. |
| 1430 | drop_last (bool): Whether to drop the last batch when sample number |
| 1431 | is less than batch_size. |
| 1432 | places (None|list(CUDAPlace)|list(CPUPlace)): place list. Must |
| 1433 | be provided when PyReader is iterable. |
| 1434 | |
| 1435 | Example: |
| 1436 | .. code-block:: pycon |
| 1437 | |
| 1438 | >>> import paddle |
| 1439 | >>> import paddle.base as base |
| 1440 | >>> import numpy as np |
| 1441 | |
| 1442 | >>> paddle.enable_static() |
| 1443 | |
| 1444 | >>> EPOCH_NUM = 3 |
| 1445 | >>> ITER_NUM = 15 |
| 1446 | >>> BATCH_SIZE = 3 |
| 1447 | |
| 1448 | >>> def network(image, label): |
| 1449 | ... # User-defined network, here is an example of softmax regression. |
| 1450 | ... predict = paddle.static.nn.fc(x=image, size=10, activation='softmax') |
| 1451 | ... return paddle.nn.functional.cross_entropy( |
| 1452 | ... input=predict, |
| 1453 | ... label=label, |
| 1454 | ... reduction='none', |
| 1455 | ... use_softmax=False, |
| 1456 | ... ) |
| 1457 | |
| 1458 | >>> def random_image_and_label_generator(height, width): |
| 1459 | ... def generator(): |
| 1460 | ... for i in range(ITER_NUM): |
| 1461 | ... fake_image = np.random.uniform( |
| 1462 | ... low=0, |
| 1463 | ... high=255, |
| 1464 | ... size=[height, width], |
| 1465 | ... ) |
| 1466 | ... fake_label = np.array([1]) |
| 1467 | ... yield fake_image, fake_label |
| 1468 | ... |
| 1469 | ... return generator |