Skip to content

Commit 1269abf

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 9b9f9dcdac634888861953cb3308f6a08fa86a00
1 parent c81f787 commit 1269abf

File tree

1,209 files changed

+4714
-4343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,209 files changed

+4714
-4343
lines changed
Binary file not shown.

dev/_downloads/64788a28f138e0d4ba98dbbbb9116be5/plot_rbm_logistic_classification.ipynb

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\n# Restricted Boltzmann Machine features for digit classification\n\nFor greyscale image data where pixel values can be interpreted as degrees of\nblackness on a white background, like handwritten digit recognition, the\nBernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM\n<sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear\nfeature extraction.\n\nIn order to learn good latent representations from a small dataset, we\nartificially generate more labeled data by perturbing the training data with\nlinear shifts of 1 pixel in each direction.\n\nThis example shows how to build a classification pipeline with a BernoulliRBM\nfeature extractor and a :class:`LogisticRegression\n<sklearn.linear_model.LogisticRegression>` classifier. The hyperparameters\nof the entire model (learning rate, hidden layer size, regularization)\nwere optimized by grid search, but the search is not reproduced here because\nof runtime constraints.\n\nLogistic regression on raw pixel values is presented for comparison. The\nexample shows that the features extracted by the BernoulliRBM help improve the\nclassification accuracy.\n"
18+
"\n# Restricted Boltzmann Machine features for digit classification\n\nFor greyscale image data where pixel values can be interpreted as degrees of\nblackness on a white background, like handwritten digit recognition, the\nBernoulli Restricted Boltzmann machine model (:class:`BernoulliRBM\n<sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear\nfeature extraction.\n"
1919
]
2020
},
2121
{
@@ -26,7 +26,115 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"# Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve\n# License: BSD\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom scipy.ndimage import convolve\nfrom sklearn import linear_model, datasets, metrics\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neural_network import BernoulliRBM\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.preprocessing import minmax_scale\nfrom sklearn.base import clone\n\n\n# #############################################################################\n# Setting up\n\n\ndef nudge_dataset(X, Y):\n \"\"\"\n This produces a dataset 5 times bigger than the original one,\n by moving the 8x8 images in X around by 1px to left, right, down, up\n \"\"\"\n direction_vectors = [\n [[0, 1, 0], [0, 0, 0], [0, 0, 0]],\n [[0, 0, 0], [1, 0, 0], [0, 0, 0]],\n [[0, 0, 0], [0, 0, 1], [0, 0, 0]],\n [[0, 0, 0], [0, 0, 0], [0, 1, 0]],\n ]\n\n def shift(x, w):\n return convolve(x.reshape((8, 8)), mode=\"constant\", weights=w).ravel()\n\n X = np.concatenate(\n [X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]\n )\n Y = np.concatenate([Y for _ in range(5)], axis=0)\n return X, Y\n\n\n# Load Data\nX, y = datasets.load_digits(return_X_y=True)\nX = np.asarray(X, \"float32\")\nX, Y = nudge_dataset(X, y)\nX = minmax_scale(X, feature_range=(0, 1)) # 0-1 scaling\n\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)\n\n# Models we will use\nlogistic = linear_model.LogisticRegression(solver=\"newton-cg\", tol=1)\nrbm = BernoulliRBM(random_state=0, verbose=True)\n\nrbm_features_classifier = Pipeline(steps=[(\"rbm\", rbm), (\"logistic\", logistic)])\n\n# #############################################################################\n# Training\n\n# Hyper-parameters. These were set by cross-validation,\n# using a GridSearchCV. Here we are not performing cross-validation to\n# save time.\nrbm.learning_rate = 0.06\nrbm.n_iter = 10\n# More components tend to give better prediction performance, but larger\n# fitting time\nrbm.n_components = 100\nlogistic.C = 6000\n\n# Training RBM-Logistic Pipeline\nrbm_features_classifier.fit(X_train, Y_train)\n\n# Training the Logistic regression classifier directly on the pixel\nraw_pixel_classifier = clone(logistic)\nraw_pixel_classifier.C = 100.0\nraw_pixel_classifier.fit(X_train, Y_train)\n\n# #############################################################################\n# Evaluation\n\nY_pred = rbm_features_classifier.predict(X_test)\nprint(\n \"Logistic regression using RBM features:\\n%s\\n\"\n % (metrics.classification_report(Y_test, Y_pred))\n)\n\nY_pred = raw_pixel_classifier.predict(X_test)\nprint(\n \"Logistic regression using raw pixel features:\\n%s\\n\"\n % (metrics.classification_report(Y_test, Y_pred))\n)\n\n# #############################################################################\n# Plotting\n\nplt.figure(figsize=(4.2, 4))\nfor i, comp in enumerate(rbm.components_):\n plt.subplot(10, 10, i + 1)\n plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation=\"nearest\")\n plt.xticks(())\n plt.yticks(())\nplt.suptitle(\"100 components extracted by RBM\", fontsize=16)\nplt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)\n\nplt.show()"
29+
"# Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve\n# License: BSD"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"## Generate data\n\nIn order to learn good latent representations from a small dataset, we\nartificially generate more labeled data by perturbing the training data with\nlinear shifts of 1 pixel in each direction.\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"import numpy as np\n\nfrom scipy.ndimage import convolve\n\nfrom sklearn import datasets\nfrom sklearn.preprocessing import minmax_scale\n\nfrom sklearn.model_selection import train_test_split\n\n\ndef nudge_dataset(X, Y):\n \"\"\"\n This produces a dataset 5 times bigger than the original one,\n by moving the 8x8 images in X around by 1px to left, right, down, up\n \"\"\"\n direction_vectors = [\n [[0, 1, 0], [0, 0, 0], [0, 0, 0]],\n [[0, 0, 0], [1, 0, 0], [0, 0, 0]],\n [[0, 0, 0], [0, 0, 1], [0, 0, 0]],\n [[0, 0, 0], [0, 0, 0], [0, 1, 0]],\n ]\n\n def shift(x, w):\n return convolve(x.reshape((8, 8)), mode=\"constant\", weights=w).ravel()\n\n X = np.concatenate(\n [X] + [np.apply_along_axis(shift, 1, X, vector) for vector in direction_vectors]\n )\n Y = np.concatenate([Y for _ in range(5)], axis=0)\n return X, Y\n\n\nX, y = datasets.load_digits(return_X_y=True)\nX = np.asarray(X, \"float32\")\nX, Y = nudge_dataset(X, y)\nX = minmax_scale(X, feature_range=(0, 1)) # 0-1 scaling\n\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"## Models definition\n\nWe build a classification pipeline with a BernoulliRBM feature extractor and\na :class:`LogisticRegression <sklearn.linear_model.LogisticRegression>`\nclassifier.\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"from sklearn import linear_model\nfrom sklearn.neural_network import BernoulliRBM\nfrom sklearn.pipeline import Pipeline\n\nlogistic = linear_model.LogisticRegression(solver=\"newton-cg\", tol=1)\nrbm = BernoulliRBM(random_state=0, verbose=True)\n\nrbm_features_classifier = Pipeline(steps=[(\"rbm\", rbm), (\"logistic\", logistic)])"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"## Training\n\nThe hyperparameters of the entire model (learning rate, hidden layer size,\nregularization) were optimized by grid search, but the search is not\nreproduced here because of runtime constraints.\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"from sklearn.base import clone\n\n# Hyper-parameters. These were set by cross-validation,\n# using a GridSearchCV. Here we are not performing cross-validation to\n# save time.\nrbm.learning_rate = 0.06\nrbm.n_iter = 10\n\n# More components tend to give better prediction performance, but larger\n# fitting time\nrbm.n_components = 100\nlogistic.C = 6000\n\n# Training RBM-Logistic Pipeline\nrbm_features_classifier.fit(X_train, Y_train)\n\n# Training the Logistic regression classifier directly on the pixel\nraw_pixel_classifier = clone(logistic)\nraw_pixel_classifier.C = 100.0\nraw_pixel_classifier.fit(X_train, Y_train)"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"## Evaluation\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"from sklearn import metrics\n\nY_pred = rbm_features_classifier.predict(X_test)\nprint(\n \"Logistic regression using RBM features:\\n%s\\n\"\n % (metrics.classification_report(Y_test, Y_pred))\n)"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [],
111+
"source": [
112+
"Y_pred = raw_pixel_classifier.predict(X_test)\nprint(\n \"Logistic regression using raw pixel features:\\n%s\\n\"\n % (metrics.classification_report(Y_test, Y_pred))\n)"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"The features extracted by the BernoulliRBM help improve the classification\naccuracy with respect to the logistic regression on raw pixels.\n\n"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"## Plotting\n\n"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {
133+
"collapsed": false
134+
},
135+
"outputs": [],
136+
"source": [
137+
"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(4.2, 4))\nfor i, comp in enumerate(rbm.components_):\n plt.subplot(10, 10, i + 1)\n plt.imshow(comp.reshape((8, 8)), cmap=plt.cm.gray_r, interpolation=\"nearest\")\n plt.xticks(())\n plt.yticks(())\nplt.suptitle(\"100 components extracted by RBM\", fontsize=16)\nplt.subplots_adjust(0.08, 0.02, 0.92, 0.85, 0.08, 0.23)\n\nplt.show()"
30138
]
31139
}
32140
],
Binary file not shown.

dev/_downloads/74cf42fe2afd38be640572601152dbe6/plot_rbm_logistic_classification.py

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,27 @@
99
<sklearn.neural_network.BernoulliRBM>`) can perform effective non-linear
1010
feature extraction.
1111
12-
In order to learn good latent representations from a small dataset, we
13-
artificially generate more labeled data by perturbing the training data with
14-
linear shifts of 1 pixel in each direction.
15-
16-
This example shows how to build a classification pipeline with a BernoulliRBM
17-
feature extractor and a :class:`LogisticRegression
18-
<sklearn.linear_model.LogisticRegression>` classifier. The hyperparameters
19-
of the entire model (learning rate, hidden layer size, regularization)
20-
were optimized by grid search, but the search is not reproduced here because
21-
of runtime constraints.
22-
23-
Logistic regression on raw pixel values is presented for comparison. The
24-
example shows that the features extracted by the BernoulliRBM help improve the
25-
classification accuracy.
26-
2712
"""
2813

2914
# Authors: Yann N. Dauphin, Vlad Niculae, Gabriel Synnaeve
3015
# License: BSD
3116

17+
# %%
18+
# Generate data
19+
# -------------
20+
#
21+
# In order to learn good latent representations from a small dataset, we
22+
# artificially generate more labeled data by perturbing the training data with
23+
# linear shifts of 1 pixel in each direction.
24+
3225
import numpy as np
33-
import matplotlib.pyplot as plt
3426

3527
from scipy.ndimage import convolve
36-
from sklearn import linear_model, datasets, metrics
37-
from sklearn.model_selection import train_test_split
38-
from sklearn.neural_network import BernoulliRBM
39-
from sklearn.pipeline import Pipeline
40-
from sklearn.preprocessing import minmax_scale
41-
from sklearn.base import clone
4228

29+
from sklearn import datasets
30+
from sklearn.preprocessing import minmax_scale
4331

44-
# #############################################################################
45-
# Setting up
32+
from sklearn.model_selection import train_test_split
4633

4734

4835
def nudge_dataset(X, Y):
@@ -67,28 +54,46 @@ def shift(x, w):
6754
return X, Y
6855

6956

70-
# Load Data
7157
X, y = datasets.load_digits(return_X_y=True)
7258
X = np.asarray(X, "float32")
7359
X, Y = nudge_dataset(X, y)
7460
X = minmax_scale(X, feature_range=(0, 1)) # 0-1 scaling
7561

7662
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
7763

78-
# Models we will use
64+
# %%
65+
# Models definition
66+
# -----------------
67+
#
68+
# We build a classification pipeline with a BernoulliRBM feature extractor and
69+
# a :class:`LogisticRegression <sklearn.linear_model.LogisticRegression>`
70+
# classifier.
71+
72+
from sklearn import linear_model
73+
from sklearn.neural_network import BernoulliRBM
74+
from sklearn.pipeline import Pipeline
75+
7976
logistic = linear_model.LogisticRegression(solver="newton-cg", tol=1)
8077
rbm = BernoulliRBM(random_state=0, verbose=True)
8178

8279
rbm_features_classifier = Pipeline(steps=[("rbm", rbm), ("logistic", logistic)])
8380

84-
# #############################################################################
81+
# %%
8582
# Training
83+
# --------
84+
#
85+
# The hyperparameters of the entire model (learning rate, hidden layer size,
86+
# regularization) were optimized by grid search, but the search is not
87+
# reproduced here because of runtime constraints.
88+
89+
from sklearn.base import clone
8690

8791
# Hyper-parameters. These were set by cross-validation,
8892
# using a GridSearchCV. Here we are not performing cross-validation to
8993
# save time.
9094
rbm.learning_rate = 0.06
9195
rbm.n_iter = 10
96+
9297
# More components tend to give better prediction performance, but larger
9398
# fitting time
9499
rbm.n_components = 100
@@ -102,23 +107,34 @@ def shift(x, w):
102107
raw_pixel_classifier.C = 100.0
103108
raw_pixel_classifier.fit(X_train, Y_train)
104109

105-
# #############################################################################
110+
# %%
106111
# Evaluation
112+
# ----------
113+
114+
from sklearn import metrics
107115

108116
Y_pred = rbm_features_classifier.predict(X_test)
109117
print(
110118
"Logistic regression using RBM features:\n%s\n"
111119
% (metrics.classification_report(Y_test, Y_pred))
112120
)
113121

122+
# %%
114123
Y_pred = raw_pixel_classifier.predict(X_test)
115124
print(
116125
"Logistic regression using raw pixel features:\n%s\n"
117126
% (metrics.classification_report(Y_test, Y_pred))
118127
)
119128

120-
# #############################################################################
129+
# %%
130+
# The features extracted by the BernoulliRBM help improve the classification
131+
# accuracy with respect to the logistic regression on raw pixels.
132+
133+
# %%
121134
# Plotting
135+
# --------
136+
137+
import matplotlib.pyplot as plt
122138

123139
plt.figure(figsize=(4.2, 4))
124140
for i, comp in enumerate(rbm.components_):

dev/_downloads/scikit-learn-docs.zip

-3.73 KB
Binary file not shown.
-166 Bytes
156 Bytes
7 Bytes
-81 Bytes
723 Bytes

0 commit comments

Comments
 (0)