(logger, model, last_step, eval_data_loader, eval_section, num_eval_items=None)
| 214 | |
| 215 | @staticmethod |
| 216 | def _eval_model(logger, model, last_step, eval_data_loader, eval_section, num_eval_items=None): |
| 217 | stats = collections.defaultdict(float) |
| 218 | model.eval() |
| 219 | with torch.no_grad(): |
| 220 | for eval_batch in eval_data_loader: |
| 221 | batch_res = model.eval_on_batch(eval_batch) |
| 222 | for k, v in batch_res.items(): |
| 223 | stats[k] += v |
| 224 | if num_eval_items and stats['total'] > num_eval_items: |
| 225 | break |
| 226 | model.train() |
| 227 | |
| 228 | # Divide each stat by 'total' |
| 229 | for k in stats: |
| 230 | if k != 'total': |
| 231 | stats[k] /= stats['total'] |
| 232 | if 'total' in stats: |
| 233 | del stats['total'] |
| 234 | |
| 235 | logger.log("Step {} stats, {}: {}".format( |
| 236 | last_step, eval_section, ", ".join( |
| 237 | "{} = {}".format(k, v) for k, v in stats.items()))) |
| 238 | |
| 239 | def add_parser(): |
| 240 | parser = argparse.ArgumentParser() |
no test coverage detected