diff --git a/CHANGES.md b/CHANGES.md index 178ce8d6..4a9ccf7a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fix `to_numpy` conversion of conjugate views of complex PyTorch tensors, + including tensors in nested lists, tuples and dictionaries. - `ValidSplit` now raises a clear error when it receives an `IterableDataset`, instead of an opaque `TypeError` about a missing length (#594) - `Checkpoint`'s sink message now reports the epoch the checkpoint was actually taken in, instead of the following one - Fix pickling a net with an `LRScheduler` callback dragging along a reference to the (potentially CUDA-resident) optimizer, which could prevent loading nets trained on CUDA on a CPU-only machine (#1096) diff --git a/skorch/tests/test_utils.py b/skorch/tests/test_utils.py index dfe32fc6..3fa0a617 100644 --- a/skorch/tests/test_utils.py +++ b/skorch/tests/test_utils.py @@ -164,16 +164,24 @@ def x_tensor(self): return torch.zeros(3, 4) @pytest.fixture - def x_tuple(self): - return torch.ones(3), torch.zeros(3, 4) + def x_tensor_conjugated(self, x_tensor): + # A real tensor's conj() does not set the conjugate bit. + return (x_tensor.to(torch.complex64) + 1j).conj() @pytest.fixture - def x_list(self): - return [torch.ones(3), torch.zeros(3, 4)] + def x_tuple(self, x_tensor_conjugated): + return torch.ones(3), torch.zeros(3, 4), x_tensor_conjugated @pytest.fixture - def x_dict(self): - return {'a': torch.ones(3), 'b': (torch.zeros(2), torch.zeros(3))} + def x_list(self, x_tensor_conjugated): + return [torch.ones(3), torch.zeros(3, 4), x_tensor_conjugated] + + @pytest.fixture + def x_dict(self, x_tensor_conjugated): + return { + 'a': torch.ones(3), 'b': (torch.zeros(2), torch.zeros(3)), + 'c': x_tensor_conjugated, + } def compare_array_to_tensor(self, x_numpy, x_tensor): assert isinstance(x_tensor, torch.Tensor) @@ -186,6 +194,11 @@ def test_tensor(self, to_numpy, x_tensor): x_numpy = to_numpy(x_tensor) self.compare_array_to_tensor(x_numpy, x_tensor) + def test_tensor_conjugated(self, to_numpy, x_tensor_conjugated): + x_numpy = to_numpy(x_tensor_conjugated) + self.compare_array_to_tensor(x_numpy, x_tensor_conjugated) + assert x_tensor_conjugated.is_conj() + def test_list(self, to_numpy, x_list): x_numpy = to_numpy(x_list) for entry_numpy, entry_torch in zip(x_numpy, x_list): @@ -201,6 +214,7 @@ def test_dict(self, to_numpy, x_dict): self.compare_array_to_tensor(x_numpy['a'], x_dict['a']) self.compare_array_to_tensor(x_numpy['b'][0], x_dict['b'][0]) self.compare_array_to_tensor(x_numpy['b'][1], x_dict['b'][1]) + self.compare_array_to_tensor(x_numpy['c'], x_dict['c']) @pytest.mark.parametrize('x_invalid', [ 1, diff --git a/skorch/utils.py b/skorch/utils.py index 22a10fae..16a4ce43 100644 --- a/skorch/utils.py +++ b/skorch/utils.py @@ -172,6 +172,9 @@ def to_numpy(X): if X.requires_grad: X = X.detach() + if hasattr(X, 'is_conj') and X.is_conj(): + X = X.resolve_conj() + return X.numpy()