Skip to content

[MRG] 32/64-bit float consistency with BernoulliRBM - #16352

Merged
rth merged 14 commits into
scikit-learn:masterfrom
Henley13:bernoulli_preserving_dtypes
Jun 23, 2020
Merged

rth merged 14 commits into
scikit-learn:masterfrom
Henley13:bernoulli_preserving_dtypes

Conversation

@Henley13

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Works on #11000 for BernoulliRBM.

What does this implement/fix? Explain your changes.

Prevent the transformer from converting float32 to float64.

Any other comments?

Maybe we should wait to merge a generic test for dtype consistency (see #16290) before merging this one.

@Henley13 Henley13 changed the title [MRG] Bernoulli preserving dtypes [MRG] 32/64-bit float consistency with BernoulliRBM Jan 31, 2020

# dtype_in and dtype_out consistent
assert Xt.dtype == dtype_out, ('transform dtype: {} - original dtype: {}'
.format(Xt.dtype, X.dtype))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While #16290 is not merged, we also should add a check that the results of fit_transform are close enough with float32 and float64 input.

@ksslng ksslng Feb 2, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also should check that the attributes of the estimator are close on float32 and float64 inputs, which I think is better to be done in the individual tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a dedicated test for that.

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this more susceptible to loss of precision in intermediate matrix multiplications?

@rth

rth commented Feb 3, 2020 •

Copy link
Copy Markdown
Member

Is this more susceptible to loss of precision in intermediate matrix multiplications?

I guess this question could apply to any neural net architecture, and yet all DL libraries use float32 and sometimes even floa16 lately. Maybe they just don't enforce that outputs in f64/f32 are identical? Not sure. If so, I wonder whether we should. Possibly checking that the convergence critera was reached no matter what it was is enough (at least for some type of algorithms).

@rth rth added the Needs work label Feb 5, 2020

@jeremiedbb jeremiedbb left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Just a small comment. Also please add a what's new entry

Comment on lines +224 to +232
assert_almost_equal(Xt_64, Xt_32, 6)
assert_almost_equal(rbm_64.intercept_hidden_,
rbm_32.intercept_hidden_,
6)
assert_almost_equal(rbm_64.intercept_visible_,
rbm_32.intercept_visible_,
6)
assert_almost_equal(rbm_64.components_, rbm_32.components_, 6)
assert_almost_equal(rbm_64.h_samples_, rbm_32.h_samples_, 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use assert_allclose instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread sklearn/neural_network/_rbm.py Outdated
h : ndarray of shape (n_samples, n_components)
Corresponding mean field values for the hidden layer.
"""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread sklearn/neural_network/_rbm.py Outdated

p = safe_sparse_dot(v, self.components_.T)
p += self.intercept_hidden_

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@rth rth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment thread doc/whats_new/v0.23.rst Outdated
:class:`neural_network.MLPClassifier` by clipping the probabilities.
:pr:`16117` by `Thomas Fan`_.

- |Enhancement| Prevent the transformer from converting float32 to float64 in

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Avoid converting float32 input to float64 in ..."?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better ^^

@rth
rth self-requested a review June 22, 2020 12:54
@rth rth removed the Needs work label Jun 23, 2020
@rth
rth merged commit 3583149 into scikit-learn:master Jun 23, 2020
@rth

rth commented Jun 23, 2020

Copy link
Copy Markdown
Member

Thanks @Henley13 !

rubywerman pushed a commit to MLH-Fellowship/scikit-learn that referenced this pull request Jun 24, 2020
jayzed82 pushed a commit to jayzed82/scikit-learn that referenced this pull request Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants