# Buggy code
def run_training_epoch_end(self, epoch_output):
    model = self.get_model()
    if self.is_overridden("training_epoch_end", model=model):
        epoch_output = model.training_epoch_end(epoch_output)
        _processed_outputs = self.process_output(epoch_output)
        log_epoch_metrics = _processed_outputs[2]
        callback_epoch_metrics = _processed_outputs[3]

        # add the metrics to the loggers
        self.log_metrics(log_epoch_metrics, {})

        # add metrics to callbacks
        self.callback_metrics.update(callback_epoch_metrics)

        # add metrics to progress_bar
        self.add_progress_bar_metrics(_processed_outputs[1])


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-a4569c0ab5cd> in <module>
3 logging.getLogger('allennlp').setLevel(logging.INFO)
4
----> 5 train("/shared/labs/workflow/configs/ner_span_path_2.jsonnet", "/Dropbox/EvidLabs/atee/lab_data_enrichment_project/train/DataEnrichmentSpanPath/DataEnrichmentSpanPath__INV_M_span_path_42_43", AllenNlpLightningModule, tags=[])
6
7 # train("/shared/labs/workflow/configs/candidate_span_grouper.jsonnet", "./DataEnrichmentBunchedCrfTaggerTest", AllenNlpLightningModule, tags=["Frozen"])

/code/evid-research/evid2/pytorch_lightning/trainer/config_file_trainer.py in train(config_file, project_dir, lightning_module, run_id, run_name_override, tags, dry_run)
171     # 3 START TRAINING
172     # ------------------------
--> 173     trainer.fit(model)
174
175

/code/pytorch-lightning/pytorch_lightning/trainer/trainer.py in fit(self, model, train_dataloader, val_dataloaders)
971         # easier to avoid NCCL issues
972         elif self.use_dp:
--> 973             self.dp_train(model)
974
975         elif self.use_horovod:

/code/pytorch-lightning/pytorch_lightning/trainer/distrib_parts.py in dp_train(self, model)
265         model = LightningDataParallel(model, device_ids=device_ids)
266
--> 267         self.run_pretrain_routine(model)
268
269         model.forward = model_autocast_original_forward

/code/pytorch-lightning/pytorch_lightning/trainer/trainer.py in run_pretrain_routine(self, model)
1154
1155         # CORE TRAINING LOOP
-> 1156         self.train()
1157
1158     def test(

/code/pytorch-lightning/pytorch_lightning/trainer/training_loop.py in train(self)
368                 # RUN TNG EPOCH
369                 # -----------------
--> 370                 self.run_training_epoch()
371
372                 if self.max_steps and self.max_steps <= self.global_step:

/code/pytorch-lightning/pytorch_lightning/trainer/training_loop.py in run_training_epoch(self)
480             # SAVE METRICS TO LOGGERS
481             # -----------------------------------------
--> 482             self.save_train_loop_metrics_to_loggers(batch_idx, batch_output)
483
484             # progress global step according to grads progress

/code/pytorch-lightning/pytorch_lightning/trainer/training_loop.py in save_train_loop_metrics_to_loggers(self, batch_idx, batch_output)
564             # logs user requested information to logger
565             print("batch_output.batch_log_metrics", batch_output.batch_log_metrics.keys())
--> 566             self.log_metrics(batch_output.batch_log_metrics, batch_output.grad_norm_dic)
567
568     def save_loggers_in_training_loop(self, batch_idx):

/code/pytorch-lightning/pytorch_lightning/trainer/logging.py in log_metrics(self, metrics, grad_norm_dic, step)
72         if self.is_global_zero and self.logger is not None:
73             print("scalar_metrics", scalar_metrics.keys())
---> 74             self.logger.agg_and_log_metrics(scalar_metrics, step=step)
75             self.logger.save()
76

/code/pytorch-lightning/pytorch_lightning/loggers/base.py in agg_and_log_metrics(self, metrics, step)
131         """
132         print("metrics", metrics.keys())
--> 133         agg_step, metrics_to_log = self._aggregate_metrics(metrics=metrics, step=step)
134
135         if metrics_to_log:

/code/pytorch-lightning/pytorch_lightning/loggers/base.py in _aggregate_metrics(self, metrics, step)
88
89         # compute the metrics
---> 90         agg_step, agg_mets = self._reduce_agg_metrics()
91
92         # as new step received reset accumulator

/code/pytorch-lightning/pytorch_lightning/loggers/base.py in _reduce_agg_metrics(self)
109             print("self._agg_key_funcs", self._agg_key_funcs)
110             print("self._agg_default_func", self._agg_default_func)
--> 111             agg_mets = merge_dicts(self._metrics_to_agg, self._agg_key_funcs, self._agg_default_func)
112         return self._prev_step, agg_mets
113

/code/pytorch-lightning/pytorch_lightning/loggers/base.py in merge_dicts(dicts, agg_key_funcs, default_func)
386             print("fn or default_func", fn or default_func)
387             print("values_to_agg", values_to_agg)
--> 388             d_out[k] = (fn or default_func)(values_to_agg)
389
390     return d_out

<__array_function__ internals> in mean(*args, **kwargs)

/venvs/dev3.7/lib/python3.7/site-packages/numpy/core/fromnumeric.py in mean(a, axis, dtype, out, keepdims)
3333
3334     return _methods._mean(a, axis=axis, dtype=dtype,
-> 3335                           out=out, **kwargs)
3336
3337

/venvs/dev3.7/lib/python3.7/site-packages/numpy/core/_methods.py in _mean(a, axis, dtype, out, keepdims)
149             is_float16_result = True
150
--> 151     ret = umr_sum(arr, axis, dtype, out, keepdims)
152     if isinstance(ret, mu.ndarray):
153         ret = um.true_divide(

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

# Fixed code
def run_training_epoch_end(self, epoch_output):
    model = self.get_model()
    if self.is_overridden("training_epoch_end", model=model):
        self.global_step += 1
        epoch_output = model.training_epoch_end(epoch_output)
        _processed_outputs = self.process_output(epoch_output)
        log_epoch_metrics = _processed_outputs[2]
        callback_epoch_metrics = _processed_outputs[3]

        # add the metrics to the loggers
        self.log_metrics(log_epoch_metrics, {})

        # add metrics to callbacks
        self.callback_metrics.update(callback_epoch_metrics)

        # add metrics to progress_bar
        self.add_progress_bar_metrics(_processed_outputs[1])