data generator
(self,
input_file,
batch_size,
epoch,
dev_count=1,
shuffle=True,
phase=None,
do_decode=False,
place=None)
| 330 | return self.features.get(phase, None) |
| 331 | |
| 332 | def data_generator(self, |
| 333 | input_file, |
| 334 | batch_size, |
| 335 | epoch, |
| 336 | dev_count=1, |
| 337 | shuffle=True, |
| 338 | phase=None, |
| 339 | do_decode=False, |
| 340 | place=None): |
| 341 | """data generator""" |
| 342 | examples = self._read_tsv(input_file) |
| 343 | if do_decode: |
| 344 | features = {} |
| 345 | for example in examples: |
| 346 | features[example.data_id] = example |
| 347 | self.features[phase] = features |
| 348 | |
| 349 | def wrapper(): |
| 350 | """wrapper""" |
| 351 | all_dev_batches = [] |
| 352 | for epoch_index in range(epoch): |
| 353 | if phase == "train": |
| 354 | self.current_example = 0 |
| 355 | self.current_epoch = epoch_index |
| 356 | |
| 357 | trainer_id = self.trainer_id |
| 358 | if shuffle: |
| 359 | np.random.shuffle(examples) |
| 360 | for batch_data in self._prepare_batch_data( |
| 361 | examples, batch_size, phase=phase, do_decode=do_decode, place=place): |
| 362 | if len(all_dev_batches) < dev_count: |
| 363 | all_dev_batches.append(batch_data) |
| 364 | if len(all_dev_batches) == dev_count: |
| 365 | yield all_dev_batches[trainer_id] |
| 366 | all_dev_batches = [] |
| 367 | |
| 368 | if phase != "train": |
| 369 | if trainer_id < len(all_dev_batches): |
| 370 | yield all_dev_batches[trainer_id] |
| 371 | |
| 372 | return wrapper |
| 373 | |
| 374 | def _to_lodtensor(self, data, place, lod=None): |
| 375 | data_tensor = fluid.LoDTensor() |