Skip to content

Fix squeeze inside NeuralNetBinaryClassifier.infer #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed a bug that caused LoadInitState not to work with TrainEndCheckpoint (#528)
- Fixed NeuralNetBinaryClassifier wrongly squeezing the batch dimension when using batch_size = 1 (#558)


## [0.6.0] - 2019-07-19
Expand Down
4 changes: 2 additions & 2 deletions skorch/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def infer(self, x, **fit_params):

The first output of the ``module`` must be a single array that
has either shape (n,) or shape (n, 1). In the latter case, the
output will be squeezed to become 1-dim.
output will be reshaped to become 1-dim.

"""
y_infer = super().infer(x, **fit_params)
Expand All @@ -320,7 +320,7 @@ def infer(self, x, **fit_params):
"Expected module output to have shape (n,) or "
"(n, 1), got {} instead".format(tuple(y_infer.shape)))

y_infer = y_infer.squeeze()
y_infer = y_infer.reshape(-1)
if rest is None:
return y_infer
return (y_infer,) + tuple(rest)
Expand Down
9 changes: 9 additions & 0 deletions skorch/tests/test_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ def test_net_learns(self, net_cls, module_cls, data):
valid_acc = net.history[-1, 'valid_acc']
assert valid_acc > 0.65

def test_batch_size_one(self, net_cls, module_cls, data):
X, y = data
net = net_cls(
module_cls,
max_epochs=1,
batch_size=1,
)
net.fit(X, y)

def test_history_default_keys(self, net_fit):
expected_keys = {
'train_loss', 'valid_loss', 'epoch', 'dur', 'batches', 'valid_acc'
Expand Down