.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_neural_networks_plot_rbm_logistic_classification.py: ============================================================== Restricted Boltzmann Machine features for digit classification ============================================================== For greyscale image data where pixel values can be interpreted as degrees of blackness on a white background, like handwritten digit recognition, the Bernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM `) can perform effective non-linear feature extraction. In order to learn good latent representations from a small dataset, we artificially generate more labeled data by perturbing the training data with linear shifts of 1 pixel in each direction. This example shows how to build a classification pipeline with a BernoulliRBM feature extractor and a :class:`LogisticRegression ` classifier. The hyperparameters of the entire model (learning rate, hidden layer size, regularization) were optimized by grid search, but the search is not reproduced here because of runtime constraints. Logistic regression on raw pixel values is presented for comparison. The example shows that the features extracted by the BernoulliRBM help improve the classification accuracy. .. image:: /auto_examples/neural_networks/images/sphx_glr_plot_rbm_logistic_classification_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none [BernoulliRBM] Iteration 1, pseudo-likelihood = -25.39, time = 0.15s [BernoulliRBM] Iteration 2, pseudo-likelihood = -23.72, time = 0.33s [BernoulliRBM] Iteration 3, pseudo-likelihood = -22.72, time = 0.31s [BernoulliRBM] Iteration 4, pseudo-likelihood = -21.86, time = 0.36s [BernoulliRBM] Iteration 5, pseudo-likelihood = -21.66, time = 0.33s [BernoulliRBM] Iteration 6, pseudo-likelihood = -21.00, time = 0.31s [BernoulliRBM] Iteration 7, pseudo-likelihood = -20.75, time = 0.28s [BernoulliRBM] Iteration 8, pseudo-likelihood = -20.52, time = 0.34s [BernoulliRBM] Iteration 9, pseudo-likelihood = -20.38, time = 0.32s [BernoulliRBM] Iteration 10, pseudo-likelihood = -20.23, time = 0.35s [BernoulliRBM] Iteration 11, pseudo-likelihood = -20.02, time = 0.34s [BernoulliRBM] Iteration 12, pseudo-likelihood = -19.93, time = 0.31s [BernoulliRBM] Iteration 13, pseudo-likelihood = -19.71, time = 0.32s [BernoulliRBM] Iteration 14, pseudo-likelihood = -19.69, time = 0.35s [BernoulliRBM] Iteration 15, pseudo-likelihood = -19.61, time = 0.33s [BernoulliRBM] Iteration 16, pseudo-likelihood = -19.57, time = 0.32s [BernoulliRBM] Iteration 17, pseudo-likelihood = -19.36, time = 0.30s [BernoulliRBM] Iteration 18, pseudo-likelihood = -19.22, time = 0.36s [BernoulliRBM] Iteration 19, pseudo-likelihood = -19.31, time = 0.34s [BernoulliRBM] Iteration 20, pseudo-likelihood = -19.21, time = 0.35s Logistic regression using RBM features: precision recall f1-score support 0 0.99 0.98 0.99 174 1 0.94 0.94 0.94 184 2 0.90 0.98 0.94 166 3 0.94 0.90 0.92 194 4 0.96 0.94 0.95 186 5 0.91 0.92 0.91 181 6 0.98 0.97 0.97 207 7 0.93 0.99 0.96 154 8 0.91 0.85 0.88 182 9 0.91 0.92 0.91 169 micro avg 0.94 0.94 0.94 1797 macro avg 0.94 0.94 0.94 1797 weighted avg 0.94 0.94 0.94 1797 Logistic regression using raw pixel features: precision recall f1-score support 0 0.90 0.93 0.91 174 1 0.60 0.59 0.59 184 2 0.75 0.85 0.80 166 3 0.77 0.79 0.78 194 4 0.81 0.84 0.82 186 5 0.77 0.75 0.76 181 6 0.90 0.87 0.89 207 7 0.86 0.88 0.87 154 8 0.67 0.58 0.62 182 9 0.75 0.76 0.76 169 micro avg 0.78 0.78 0.78 1797 macro avg 0.78 0.78 0.78 1797 weighted avg 0.78 0.78 0.78 1797 | .. code-block:: default from __future__ import print_function print(__doc__) # Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve # License: BSD import numpy as np import matplotlib.pyplot as plt from scipy.ndimage import convolve from sklearn import linear_model, datasets, metrics from sklearn.model_selection import train_test_split from sklearn.neural_network import BernoulliRBM from sklearn.pipeline import Pipeline from sklearn.base import clone # ############################################################################# # Setting up def nudge_dataset(X, Y): """ This produces a dataset 5 times bigger than the original one, by moving the 8x8 images in X around by 1px to left, right, down, up """ direction_vectors = [ [[0, 1, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 1], [0, 0, 0]], [[0, 0, 0], [0, 0, 0], [0, 1, 0]]] def shift(x, w): return convolve(x.reshape((8, 8)), mode='constant', weights=w).ravel() X = np.concatenate([X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]) Y = np.concatenate([Y for _ in range(5)], axis=0) return X, Y # Load Data digits = datasets.load_digits() X = np.asarray(digits.data, 'float32') X, Y = nudge_dataset(X, digits.target) X = (X - np.min(X, 0)) / (np.max(X, 0) + 0.0001) # 0-1 scaling X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size=0.2, random_state=0) # Models we will use logistic = linear_model.LogisticRegression(solver='lbfgs', max_iter=10000, multi_class='multinomial') rbm = BernoulliRBM(random_state=0, verbose=True) rbm_features_classifier = Pipeline( steps=[('rbm', rbm), ('logistic', logistic)]) # ############################################################################# # Training # Hyper-parameters. These were set by cross-validation, # using a GridSearchCV. Here we are not performing cross-validation to # save time. rbm.learning_rate = 0.06 rbm.n_iter = 20 # More components tend to give better prediction performance, but larger # fitting time rbm.n_components = 100 logistic.C = 6000 # Training RBM-Logistic Pipeline rbm_features_classifier.fit(X_train, Y_train) # Training the Logistic regression classifier directly on the pixel raw_pixel_classifier = clone(logistic) raw_pixel_classifier.C = 100. raw_pixel_classifier.fit(X_train, Y_train) # ############################################################################# # Evaluation Y_pred = rbm_features_classifier.predict(X_test) print("Logistic regression using RBM features:\n%s\n" % ( metrics.classification_report(Y_test, Y_pred))) Y_pred = raw_pixel_classifier.predict(X_test) print("Logistic regression using raw pixel features:\n%s\n" % ( metrics.classification_report(Y_test, Y_pred))) # ############################################################################# # Plotting plt.figure(figsize=(4.2, 4)) for i, comp in enumerate(rbm.components_): plt.subplot(10, 10, i + 1) plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation='nearest') plt.xticks(()) plt.yticks(()) plt.suptitle('100 components extracted by RBM', fontsize=16) plt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23) plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 49.334 seconds) .. _sphx_glr_download_auto_examples_neural_networks_plot_rbm_logistic_classification.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_rbm_logistic_classification.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_rbm_logistic_classification.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_