Reader that stops after `num_iter` batches. If `num_iter` <= 0 or is None, reverts to an unconstrained reader that exports a boolean blob indicating that the reader has exhausted the data steam.
| 408 | |
| 409 | |
| 410 | class ReaderWithLimit(ReaderWithLimitBase): |
| 411 | """Reader that stops after `num_iter` batches. |
| 412 | |
| 413 | If `num_iter` <= 0 or is None, reverts to an unconstrained reader that |
| 414 | exports a boolean blob indicating that the reader has exhausted |
| 415 | the data steam. |
| 416 | """ |
| 417 | def __init__(self, reader, num_iter=1): |
| 418 | """Class initializer. |
| 419 | |
| 420 | Args: |
| 421 | reader: The underlying reader object doing the actual read. |
| 422 | num_iter: Number of batches to read. If `None`, |
| 423 | the class reverts to a normal reader except that it also |
| 424 | produces a data_finished blob as a side effect to indicate |
| 425 | whether the input stream is exhausted. |
| 426 | """ |
| 427 | super().__init__(reader) |
| 428 | self.counter = None |
| 429 | self.num_iter = num_iter |
| 430 | if self.num_iter is not None: |
| 431 | self.counter = self.net.AddExternalInput( |
| 432 | self.net.NextName('counter')) |
| 433 | |
| 434 | def setup_limiter(self, global_init_net, global_finish_net): |
| 435 | if self.counter: |
| 436 | global_init_net.CreateCounter( |
| 437 | [], [self.counter], init_count=int(self.num_iter)) |
| 438 | |
| 439 | def check_limiter_condition(self, stop_condition_net): |
| 440 | if self.counter: |
| 441 | return stop_condition_net.CountDown([self.counter], 1) |
| 442 | else: |
| 443 | return stop_condition_net.ConstantFill( |
| 444 | [], 1, |
| 445 | shape=[], value=False, dtype=core.DataType.BOOL) |
| 446 | |
| 447 | |
| 448 | def CountUntil(num_iter): |
no outgoing calls
searching dependent graphs…