(self, batch, batch_idx)
| 1434 | model.on_epoch_end() |
| 1435 | |
| 1436 | def run_training_batch(self, batch, batch_idx): |
| 1437 | # track grad norms |
| 1438 | grad_norm_dic = {} |
| 1439 | |
| 1440 | # track all metrics for callbacks |
| 1441 | all_callback_metrics = [] |
| 1442 | |
| 1443 | # track metrics to log |
| 1444 | all_log_metrics = [] |
| 1445 | |
| 1446 | if batch is None: |
| 1447 | return 0, grad_norm_dic, {} |
| 1448 | |
| 1449 | # hook |
| 1450 | if self.is_function_implemented('on_batch_start'): |
| 1451 | model_ref = self.get_model() |
| 1452 | response = model_ref.on_batch_start(batch) |
| 1453 | |
| 1454 | if response == -1: |
| 1455 | return -1, grad_norm_dic, {} |
| 1456 | |
| 1457 | splits = [batch] |
| 1458 | self.hiddens = None |
| 1459 | for split_idx, split_batch in enumerate(splits): |
| 1460 | self.split_idx = split_idx |
| 1461 | |
| 1462 | # call training_step once per optimizer |
| 1463 | for opt_idx, optimizer in enumerate(self.optimizers): |
| 1464 | if optimizer is None: |
| 1465 | continue |
| 1466 | # make sure only the gradients of the current optimizer's paramaters are calculated |
| 1467 | # in the training step to prevent dangling gradients in multiple-optimizer setup. |
| 1468 | if len(self.optimizers) > 1: |
| 1469 | for param in self.get_model().parameters(): |
| 1470 | param.requires_grad = False |
| 1471 | for group in optimizer.param_groups: |
| 1472 | for param in group['params']: |
| 1473 | param.requires_grad = True |
| 1474 | |
| 1475 | # wrap the forward step in a closure so second order methods work |
| 1476 | def optimizer_closure(): |
| 1477 | # forward pass |
| 1478 | output = self.training_forward( |
| 1479 | split_batch, batch_idx, opt_idx, self.hiddens) |
| 1480 | |
| 1481 | closure_loss = output[0] |
| 1482 | progress_bar_metrics = output[1] |
| 1483 | log_metrics = output[2] |
| 1484 | callback_metrics = output[3] |
| 1485 | self.hiddens = output[4] |
| 1486 | if closure_loss is None: |
| 1487 | return None |
| 1488 | |
| 1489 | # accumulate loss |
| 1490 | # (if accumulate_grad_batches = 1 no effect) |
| 1491 | closure_loss = closure_loss / self.accumulate_grad_batches |
| 1492 | |
| 1493 | # backward pass |
no test coverage detected