Back to Cosmos

Rbm

code/artificial_intelligence/src/restricted_boltzmann_machine/rbm.ipynb

latest1.4 KB
Original Source
python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
python
training_set = pd.read_csv('train.csv')
test_set = pd.read_csv('test.csv')
python
x_train = training_set.iloc[:, 1:].values
x_train
python
x_test = test_set.iloc[:, :].values
x_test
python
x_train = x_train.astype('float32')/255.
x_test = x_test.astype('float32')/255.
python
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:]))
python
x_train.shape
python
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))
python
from sklearn.neural_network import BernoulliRBM
rbm = BernoulliRBM(n_components=100, learning_rate=0.01, random_state=0, verbose=True)
rbm.fit(x_train)
python
xx = x_train[:40].copy()
for ii in range(10000):
    for n in range(40):
        xx[n] = rbm.gibbs(xx[n])
python
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))
python
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);