Skip to content

Commit

Permalink
Fix FutureWarnings from numpy and torch (#921)
Browse files Browse the repository at this point in the history
- don't use torch.testing.assert_allclose, use assert_close instead
- don't use np.bool, use bool instead

Note that np.dtype('bool') == bool, so replace np.bool by bool should
just work.

- Adjust test that used ragged numpy array

Numpy used to allow creation of ragged arrays, e.g.

np.array([[1], [2, 2], [3, 3, 3]])

This does not work anymore with newer numpy versions (tested on
1.24.0rc2). To make it work, dtype=object has to be passed explicitly.
This does not any skorch code directly, but it was necessary to fix a
test.
  • Loading branch information
BenjaminBossan committed Dec 7, 2022
1 parent 5a19171 commit bd53b6e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion skorch/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def __getitem__(self, i):
raise IndexError("SliceDataset only supports slicing with 1 "
"dimensional arrays, got {} dimensions instead."
"".format(i.ndim))
if i.dtype == np.bool:
if i.dtype == bool:
i = np.flatnonzero(i)

return cls(self.dataset, idx=self.idx, indices=self.indices_[i])
Expand Down
2 changes: 1 addition & 1 deletion skorch/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def test_slice_twice(self, slds_cls, custom_ds, X, y, sl0, sl1, n):
@pytest.mark.parametrize('sl0, sl1, sl2', [
(slice(0, 50), slice(10, 20), 5),
([0, 10, 3, -8, 3], [1, 2, 3], 2),
(np.ones(100, dtype=np.bool), np.arange(10, 40), 29),
(np.ones(100, dtype=bool), np.arange(10, 40), 29),
])
def test_slice_three_times(self, slds_cls, custom_ds, X, y, sl0, sl1, sl2, n):
data = y if n else X
Expand Down
4 changes: 2 additions & 2 deletions skorch/tests/test_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def test_saved_net_is_same(
for key, original in net_loaded.module_.state_dict().items():
original = net.module_.state_dict()[key]
loaded = net_loaded.module_.state_dict()[key]
torch.testing.assert_allclose(loaded, original)
torch.testing.assert_close(loaded, original)

@pytest.mark.parametrize('storage', ['memory', 'str', 'path'])
def test_saved_params_is_same(
Expand Down Expand Up @@ -1087,7 +1087,7 @@ def test_saved_params_is_same(
assert len(state_dict_before) == len(state_dict_after)
for key, original in state_dict_before.items():
loaded = state_dict_after[key]
torch.testing.assert_allclose(loaded, original)
torch.testing.assert_close(loaded, original)

def test_latest_url_attribute(self, net, data, hf_hub_storer_cls):
# Check that the URL returned by the HF API is stored as latest_url. In
Expand Down
8 changes: 5 additions & 3 deletions skorch/tests/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,11 +2293,13 @@ def loss_fn(y_pred, y_true, **kwargs):
net.fit(X, y)

def test_net_variable_label_lengths(self, net_cls, sequence_module_cls):
# neural net should work fine with varying y_true sequences.
# neural net should work fine with variable length y_true sequences.
X = np.array([1, 5, 3, 6, 2])
y = np.array([[1], [1, 0, 1], [1, 1], [1, 1, 0], [1, 0]])
y = np.array([[1], [1, 0, 1], [1, 1], [1, 1, 0], [1, 0]], dtype=object)
X = X[:, np.newaxis].astype('float32')
y = np.array([np.array(n, dtype='float32')[:, np.newaxis] for n in y])
y = np.array(
[np.array(n, dtype='float32')[:, np.newaxis] for n in y], dtype=object
)

net = net_cls(
sequence_module_cls,
Expand Down
3 changes: 1 addition & 2 deletions skorch/tests/test_probabilistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
import torch
from torch.testing import assert_allclose

from skorch._version import Version
from skorch.utils import is_torch_data_type
Expand Down Expand Up @@ -272,7 +271,7 @@ def test_save_load_params(self, gp_fit, tmpdir):
gp_fit.get_all_learnable_params(), gp2.get_all_learnable_params(),
):
assert n0 == n1
assert_allclose(p0, p1)
torch.testing.assert_close(p0, p1)

##############
# functional #
Expand Down

0 comments on commit bd53b6e

Please sign in to comment.