text stringlengths 1 93.6k |
|---|
num_shards=args.distributed_world_size,
|
shard_id=args.distributed_rank,
|
num_workers=args.num_workers,
|
).next_epoch_itr(shuffle=False)
|
progress = progress_bar.build_progress_bar(
|
args, itr, epoch_itr.epoch,
|
prefix='valid on \'{}\' subset'.format(subset),
|
no_progress_bar='simple'
|
)
|
# reset validation loss meters
|
for k in ['valid_loss', 'valid_nll_loss']:
|
meter = trainer.get_meter(k)
|
if meter is not None:
|
meter.reset()
|
extra_meters = collections.defaultdict(lambda: AverageMeter())
|
for sample in progress:
|
log_output = trainer.valid_step(sample)
|
for k, v in log_output.items():
|
if k in ['loss', 'nll_loss', 'ntokens', 'nsentences', 'sample_size']:
|
continue
|
extra_meters[k].update(v)
|
# log validation stats
|
stats = get_valid_stats(trainer, args, extra_meters)
|
for k, meter in extra_meters.items():
|
stats[k] = meter.avg
|
progress.print(stats, tag=subset, step=trainer.get_num_updates())
|
valid_losses.append(
|
stats[args.best_checkpoint_metric].avg
|
if args.best_checkpoint_metric == 'loss'
|
else stats[args.best_checkpoint_metric]
|
)
|
return valid_losses
|
def get_valid_stats(trainer, args, extra_meters=None):
|
stats = collections.OrderedDict()
|
stats['loss'] = trainer.get_meter('valid_loss')
|
if trainer.get_meter('valid_nll_loss').count > 0:
|
nll_loss = trainer.get_meter('valid_nll_loss')
|
stats['nll_loss'] = nll_loss
|
else:
|
nll_loss = stats['loss']
|
stats['ppl'] = utils.get_perplexity(nll_loss.avg)
|
stats['num_updates'] = trainer.get_num_updates()
|
if hasattr(checkpoint_utils.save_checkpoint, 'best'):
|
key = 'best_{0}'.format(args.best_checkpoint_metric)
|
best_function = max if args.maximize_best_checkpoint_metric else min
|
current_metric = None
|
if args.best_checkpoint_metric == 'loss':
|
current_metric = stats['loss'].avg
|
elif args.best_checkpoint_metric in extra_meters:
|
current_metric = extra_meters[args.best_checkpoint_metric].avg
|
elif args.best_checkpoint_metric in stats:
|
current_metric = stats[args.best_checkpoint_metric]
|
else:
|
raise ValueError("best_checkpoint_metric not found in logs")
|
stats[key] = best_function(
|
checkpoint_utils.save_checkpoint.best,
|
current_metric,
|
)
|
return stats
|
def distributed_main(i, args, start_rank=0):
|
args.device_id = i
|
if args.distributed_rank is None: # torch.multiprocessing.spawn
|
args.distributed_rank = start_rank + i
|
main(args, init_distributed=True)
|
def cli_main():
|
parser = options.get_training_parser()
|
args = options.parse_args_and_arch(parser)
|
if args.distributed_init_method is None:
|
distributed_utils.infer_init_method(args)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.