Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Scikit learn Scikit learn BernoulliRBM

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Neural Networks
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for Bernoulli Restricted Boltzmann Machine (RBM) feature extraction provided by scikit-learn.

Description

BernoulliRBM is a Restricted Boltzmann Machine with binary visible units and binary hidden units. Parameters are estimated using Stochastic Maximum Likelihood (SML), also known as Persistent Contrastive Divergence (PCD). It functions as an unsupervised feature extractor (transformer) that learns a nonlinear representation of the input data. The time complexity is O(d^2) assuming d is approximately n_features or n_components.

Usage

Use BernoulliRBM as a feature extraction step in a pipeline, particularly for binary or binarized data. It is commonly combined with a classifier in a pipeline for tasks like digit recognition. The learned hidden representations can capture complex patterns in the input data.

Code Reference

Source Location

Signature

class BernoulliRBM(ClassNamePrefixFeaturesOutMixin, TransformerMixin, BaseEstimator):
    def __init__(
        self,
        n_components=256,
        *,
        learning_rate=0.1,
        batch_size=10,
        n_iter=10,
        verbose=0,
        random_state=None,
    ):

Import

from sklearn.neural_network import BernoulliRBM

I/O Contract

Inputs

Name Type Required Description
n_components int No Number of binary hidden units (default=256)
learning_rate float No Learning rate for weight updates (default=0.1)
batch_size int No Number of examples per minibatch (default=10)
n_iter int No Number of iterations/sweeps over the training dataset (default=10)
verbose int No Verbosity level (default=0)
random_state int, RandomState, or None No Random state for reproducibility

Outputs

Name Type Description
intercept_hidden_ ndarray of shape (n_components,) Biases of the hidden units
intercept_visible_ ndarray of shape (n_features,) Biases of the visible units
components_ ndarray of shape (n_components, n_features) Weight matrix connecting visible and hidden units
h_samples_ ndarray of shape (batch_size, n_components) Hidden activation sampled from the model distribution
n_features_in_ int Number of features seen during fit

Usage Examples

Basic Usage

from sklearn.neural_network import BernoulliRBM
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

X, y = load_digits(return_X_y=True)
X = MinMaxScaler().fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

rbm = BernoulliRBM(n_components=100, learning_rate=0.06, n_iter=20, random_state=42)
logistic = LogisticRegression(max_iter=1000)

pipe = Pipeline([("rbm", rbm), ("logistic", logistic)])
pipe.fit(X_train, y_train)
print(f"Accuracy: {pipe.score(X_test, y_test):.3f}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment