Skip to content

Fix: added pseudo-likelihood normalization option in RBM #23179 #31099

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions sklearn/neural_network/_rbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class BernoulliRBM(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstima
Pass an int for reproducible results across multiple function calls.
See :term:`Glossary <random_state>`.

normalize : bool, default=False
If True, returns the pseudo-likelihood normalized by the number
of visible units (features). This is useful for comparing models
trained on inputs of different dimensionalities.

Attributes
----------
intercept_hidden_ : array-like of shape (n_components,)
Expand Down Expand Up @@ -135,6 +140,7 @@ class BernoulliRBM(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstima
"n_iter": [Interval(Integral, 0, None, closed="left")],
"verbose": ["verbose"],
"random_state": ["random_state"],
"normalize": ["boolean"],
}

def __init__(
Expand All @@ -146,13 +152,15 @@ def __init__(
n_iter=10,
verbose=0,
random_state=None,
normalize=False,
):
self.n_components = n_components
self.learning_rate = learning_rate
self.batch_size = batch_size
self.n_iter = n_iter
self.verbose = verbose
self.random_state = random_state
self.normalize = normalize

def transform(self, X):
"""Compute the hidden layer activation probabilities, P(h=1|v=X).
Expand Down Expand Up @@ -341,14 +349,19 @@ def _fit(self, v_pos, rng):
h_neg[rng.uniform(size=h_neg.shape) < h_neg] = 1.0 # sample binomial
self.h_samples_ = np.floor(h_neg, h_neg)

def score_samples(self, X):
def score_samples(self, X, normalize=False):
"""Compute the pseudo-likelihood of X.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Values of the visible layer. Must be all-boolean (not checked).

normalize : bool, default=False
If True, returns the pseudo-likelihood normalized by the number
of visible units (features). This is useful for comparing models
trained on inputs of different dimensionalities.

Returns
-------
pseudo_likelihood : ndarray of shape (n_samples,)
Expand Down Expand Up @@ -380,7 +393,10 @@ def score_samples(self, X):
fe = self._free_energy(v)
fe_ = self._free_energy(v_)
# log(expit(x)) = log(1 / (1 + exp(-x)) = -np.logaddexp(0, -x)
return -v.shape[1] * np.logaddexp(0, -(fe_ - fe))
if normalize:
return -np.logaddexp(0, -(fe_ - fe))
else:
return -v.shape[1] * np.logaddexp(0, -(fe_ - fe))

@_fit_context(prefer_skip_nested_validation=True)
def fit(self, X, y=None):
Expand Down Expand Up @@ -430,7 +446,7 @@ def fit(self, X, y=None):
% (
type(self).__name__,
iteration,
self.score_samples(X).mean(),
self.score_samples(X, normalize=self.normalize).mean(),
end - begin,
)
)
Expand Down
27 changes: 27 additions & 0 deletions sklearn/neural_network/tests/test_rbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,33 @@ def test_rbm_verbose():
sys.stdout = old_stdout


def test_rbm_pseudolikelihood():
X1 = np.ones([11, 100])
rbm1 = BernoulliRBM(n_iter=5, random_state=42, normalize=False)
rbm1.fit(X1)
score1 = rbm1.score_samples(X1, normalize=False).mean()

X2 = np.ones([11, 1000])
rbm2 = BernoulliRBM(n_iter=5, random_state=42, normalize=False)
rbm2.fit(X2)
score2 = rbm2.score_samples(X2, normalize=False).mean()

X3 = np.ones([11, 100])
rbm3 = BernoulliRBM(n_iter=5, random_state=42, normalize=True)
rbm3.fit(X3)
score3 = rbm3.score_samples(X3, normalize=True).mean()

X4 = np.ones([11, 1000])
rbm4 = BernoulliRBM(n_iter=5, random_state=42, normalize=True)
rbm4.fit(X4)
score4 = rbm4.score_samples(X4, normalize=True).mean()

assert score1 < score3
assert score2 < score4
assert abs(score1) > abs(score3) * 10
assert abs(score2) > abs(score4) * 10


@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
def test_sparse_and_verbose(csc_container):
# Make sure RBM works with sparse input when verbose=True
Expand Down