firstn

paddle.fluid.io.firstn(reader, n)[source]

paddle.fluid.io.firstn ( firstn ) is recommended to use, and paddle.reader.firstn is an alias.

This API creates a decorated reader, and limits the max number of samples that reader could return.

Parameters
  • reader (callable) – the input reader.

  • n (int) – the max number of samples in the reader.

Returns

the decorated reader.

Return type

callable

Examples

import paddle.fluid as fluid

def reader():
    for i in range(100):
        yield i
firstn_reader = fluid.io.firstn(reader, 5)
for e in firstn_reader():
    print(e)
# the outputs are: 0 1 2 3 4