code/artificial_intelligence/src/restricted_boltzmann_machine/rbm.ipynb
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
training_set = pd.read_csv('train.csv')
test_set = pd.read_csv('test.csv')
x_train = training_set.iloc[:, 1:].values
x_train
x_test = test_set.iloc[:, :].values
x_test
x_train = x_train.astype('float32')/255.
x_test = x_test.astype('float32')/255.
x_train = x_train.reshape(len(x_train), np.prod(x_train.shape[1:]))
x_test = x_test.reshape(len(x_test), np.prod(x_test.shape[1:]))
x_train.shape
n = 10
plt.figure(figsize = (20, 4))
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(x_train[i].reshape(28, 28))
from sklearn.neural_network import BernoulliRBM
rbm = BernoulliRBM(n_components=100, learning_rate=0.01, random_state=0, verbose=True)
rbm.fit(x_train)
xx = x_train[:40].copy()
for ii in range(10000):
for n in range(40):
xx[n] = rbm.gibbs(xx[n])
n = 10
plt.figure(figsize = (20, 4))
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(xx[i].reshape(28, 28))
plt.figure(figsize=(20, 20))
for i, comp in enumerate(rbm.components_):
plt.subplot(10, 10, i + 1)
plt.imshow(comp.reshape((28, 28)), cmap=plt.cm.RdBu,
interpolation='nearest', vmin=-2.5, vmax=2.5)
plt.axis('off')
plt.suptitle('100 components extracted by RBM', fontsize=16);