Skip to content

Commit 2add14d

Browse files
author
dominicgkerr
committed
Update unittests to support logging 1-D tensors. Fixed exception typo
1 parent 09f32bd commit 2add14d

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/lightning/pytorch/core/module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ def __to_tensor(self, value: Union[Tensor, numbers.Number], name: str) -> Tensor
646646
# check tensor contains single element (implies value.ndim == 0), or is a non-empty 1D array
647647
if not (torch.numel(value) == 1 or (torch.numel(value) > 0 and value.ndim == 1)):
648648
raise ValueError(
649-
f"`self.log({name}, {value})` was called, but the tensor must have a single element,"
649+
f"`self.log({name}, {value})` was called, but the tensor must have a single element, "
650650
f"or a single non-empty dimension. You can try doing `self.log({name}, {value}.mean())`"
651651
)
652652
value = value.squeeze()

tests/tests_pytorch/trainer/logging_/test_train_loop_logging.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,18 @@ def training_step(self, *args):
640640

641641
class TestModel(BoringModel):
642642
def on_train_start(self):
643-
self.log("foo", torch.tensor([1.0, 2.0]))
643+
self.log("foo", torch.tensor([])) # empty
644644

645645
model = TestModel()
646-
with pytest.raises(ValueError, match="tensor must have a single element"):
646+
with pytest.raises(ValueError, match="tensor must have a single element, or a single non-empty dimension."):
647+
trainer.fit(model)
648+
649+
class TestModel(BoringModel):
650+
def on_train_start(self):
651+
self.log("foo", torch.tensor([[1.0], [2.0]])) # too-many dimensions
652+
653+
model = TestModel()
654+
with pytest.raises(ValueError, match="tensor must have a single element, or a single non-empty dimension."):
647655
trainer.fit(model)
648656

649657

0 commit comments

Comments
 (0)