Implementing RBMs with Sklearn involves several steps:
- Data Preprocessing: Prepare your dataset by cleaning, normalizing, and standardizing it as required.
- RBMs Configuration: Set hyperparameters such as the number of visible and hidden units, learning rate, number of training epochs, and batch size.
- Model Initialization: Initialize the RBM model using Sklearn’s BernoulliRBM class, specifying the hyperparameters defined in step 2.
- Model Training: Train the RBM model using your preprocessed data. Sklearn provides a fit method for this purpose.
- Feature Extraction: After training, you can use the RBM as a feature extractor. Transform your data using the RBM to obtain learned features.
- Application: Apply these learned features to various machine learning tasks like classification, regression, or clustering.
Example Implementation of RBM Model
- First, we will import the essential libraries needed to demonstrate the example.
Python3
import numpy as np
from sklearn.datasets import load_digits
from sklearn.neural_network import BernoulliRBM
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
|
- Next we will load the dataset. Here, we are using the Classification Digits Dataset.
Python3
digits = datasets.load_digits()
X = digits.data
y = digits.target
|
- For the preprocessing of the data, we are standardizing the dataset using standard scaler. You can use other methods for doing so.
Python3
scaler = StandardScaler()
X = scaler.fit_transform(X)
X_train, X_test, Y_train, Y_test = train_test_split(X, y,test_size = 0.2 ,random_state = 42 )
|
- Then, we will initialize a Bernoulli RBM model. A Bernoulli RBM model takes the following parameters:
- n_components: It determines the dimensionality of the features that the RBM will learn during training. Here we are taking 64 units.
- learning_rate: This controls how much the RBM’s parameters (weights and biases) are updated at each iteration of training. For our example, its 0.1.
- n_iter: It is similar to number of epochs the training will run for, which is 20 for our example.
- batch_size: specifies the number of samples used in each mini-batch during training
- verbose: It determines the verbosity level. If value is 0, the model runs in silent mode.
- random_state: used to set a random seed for result reproducibility.
- After initializing, we will fit the model on our input and then transform the data to be used as the feature representations.
Python3
knn.fit(X_train, Y_train)
Y_pred = knn.predict(X_test)
print (
"KNN without using RBM:\n" ,
classification_report(Y_test, Y_pred)
)
|
KNN without using RBM:
precision recall f1-score support
0 1.00 1.00 1.00 33
1 0.97 1.00 0.98 28
2 0.97 1.00 0.99 33
3 0.97 0.97 0.97 34
4 0.98 1.00 0.99 46
5 0.96 0.96 0.96 47
6 0.97 1.00 0.99 35
7 1.00 0.94 0.97 34
8 0.97 0.97 0.97 30
9 0.95 0.90 0.92 40
accuracy 0.97 360
macro avg 0.97 0.97 0.97 360
weighted avg 0.97 0.97 0.97 360
Python3
knn = KNeighborsClassifier(n_neighbors = 7 , algorithm = 'kd_tree' )
rbm = BernoulliRBM(n_components = 625 , learning_rate = 0.00001 , n_iter = 10 , verbose = False , random_state = 42 )
rbm_features_classifier = Pipeline(steps = [( "rbm" , rbm), ( "KNN" , knn)])
rbm_features_classifier.fit(X_train, Y_train)
|
Pipeline
BernoulliRBM
KNeighborsClassifier
KNeighborsClassifier(algorithm='kd_tree', n_neighbors=7)
Python3
Y_pred = rbm_features_classifier.predict(X_test)
print (
"KNN using RBM features:\n" ,
classification_report(Y_test, Y_pred)
)
|
KNN using RBM features:
precision recall f1-score support
0 1.00 1.00 1.00 33
1 0.93 1.00 0.97 28
2 0.94 1.00 0.97 33
3 0.97 0.97 0.97 34
4 0.98 1.00 0.99 46
5 0.98 0.96 0.97 47
6 0.97 1.00 0.99 35
7 1.00 0.94 0.97 34
8 0.97 0.93 0.95 30
9 0.95 0.90 0.92 40
accuracy 0.97 360
macro avg 0.97 0.97 0.97 360
weighted avg 0.97 0.97 0.97 360
- Lastly, we will visualize the original data and the transformed feature representation.
Python3
fig, axes = plt.subplots( 1 , 2 , figsize = ( 10 , 5 ))
axes[ 0 ].imshow(X[ 0 ].reshape( 8 , 8 ), cmap = plt.cm.gray_r, interpolation = 'nearest' )
axes[ 0 ].set_title( 'Original Image' )
axes[ 1 ].imshow(X_transformed[ 0 ].reshape( 8 , 8 ), cmap = plt.cm.gray_r, interpolation = 'nearest' )
axes[ 1 ].set_title( 'Transformed Image' )
plt.show()
|
Restricted Boltzmann Machine
Restricted Boltzmann Machine (RBM) with Practical Implementation
In the world of machine learning, one algorithm that has gained significant attention is the Restricted Boltzmann Machine (RBM). RBMs are powerful generative models that have been widely used for various applications, such as dimensionality reduction, feature learning, and collaborative filtering. In this article, we will explore the concepts and steps involved in training and using RBMs, along with some good examples to solidify our understanding.