diff --git a/pytorch_lightning/core/step_result.py b/pytorch_lightning/core/step_result.py index 8d8e578fbc7a8..14ff996aa3949 100644 --- a/pytorch_lightning/core/step_result.py +++ b/pytorch_lightning/core/step_result.py @@ -894,6 +894,8 @@ def write_dict(self, predictions_dict, filename='predictions.pt'): def weighted_mean(result, weights): + if not isinstance(result, torch.Tensor): + result = torch.tensor(result) weights = weights.to(result.device)[:result.size(0)] numerator = torch.dot(result.float(), weights.transpose(-1, 0).float()) result = numerator / weights.sum().float() diff --git a/tests/trainer/logging/test_eval_loop_logging_1_0.py b/tests/trainer/logging/test_eval_loop_logging_1_0.py index d417f9a2f6ad4..831e59965e1a4 100644 --- a/tests/trainer/logging/test_eval_loop_logging_1_0.py +++ b/tests/trainer/logging/test_eval_loop_logging_1_0.py @@ -196,6 +196,40 @@ def validation_epoch_end(self, outputs): assert len(trainer.dev_debugger.logged_metrics) == max_epochs +def test_eval_float_logging(tmpdir): + """ + Tests that only training_step can be used + """ + os.environ['PL_DEV_DEBUG'] = '1' + + class TestModel(BoringModel): + + def validation_step(self, batch, batch_idx): + output = self.layer(batch) + loss = self.loss(batch, output) + self.log('a', 12.0) + return {"x": loss} + + model = TestModel() + + trainer = Trainer( + default_root_dir=tmpdir, + limit_train_batches=2, + limit_val_batches=2, + max_epochs=1, + row_log_interval=1, + weights_summary=None, + ) + trainer.fit(model) + + # make sure all the metrics are available for callbacks + logged_metrics = set(trainer.logged_metrics.keys()) + expected_logged_metrics = { + 'a', + } + assert logged_metrics == expected_logged_metrics + + def test_monitor_val_epoch_end(tmpdir): epoch_min_loss_override = 0 model = SimpleModule()