Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions skorch/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,34 @@ def test_tensor(self, to_numpy, x_tensor):
x_numpy = to_numpy(x_tensor)
self.compare_array_to_tensor(x_numpy, x_tensor)

@pytest.mark.parametrize('dtype', [torch.complex64, torch.complex128])
@pytest.mark.parametrize('requires_grad', [False, True])
@pytest.mark.parametrize('container', ['direct', 'list', 'tuple', 'dict'])
def test_conjugate_tensor(self, to_numpy, dtype, requires_grad, container):
data = torch.tensor(
[[1 + 2j, 3 - 4j], [5 + 0j, -1 + 6j]],
dtype=dtype, requires_grad=requires_grad)
before = data.detach().clone()
view = data.T.conj()
value = {
'direct': view, 'list': [view],
'tuple': (view,), 'dict': {'signal': view},
}[container]
result = to_numpy(value)
if container in ('list', 'tuple'):
assert isinstance(result, type(value))
result = result[0]
elif container == 'dict':
assert isinstance(result, dict)
result = result['signal']
self.compare_array_to_tensor(result, view)
expected = np.conjugate(before.numpy().T)
np.testing.assert_array_equal(result, expected)
assert result.dtype == expected.dtype
assert torch.equal(data, before)
assert view.is_conj()
assert view.requires_grad == requires_grad

Comment thread
ktz03 marked this conversation as resolved.
Resolved
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):
Expand Down
3 changes: 3 additions & 0 deletions skorch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ def to_numpy(X):
if X.requires_grad:
X = X.detach()

if hasattr(X, 'resolve_conj'):
X = X.resolve_conj()

Comment thread
ktz03 marked this conversation as resolved.
Resolved
return X.numpy()


Expand Down