<file_sep>from keras.datasets import mnist
from keras import backend as K
from keras.callbacks import TensorBoard
from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model
import numpy as np
import matplotlib.pyplot as plt


def load_mnist():
    (x_train, _), (x_test, _) = mnist.load_data()

    x_train = x_train.astype('float32') / 255.
    x_test = x_test.astype('float32') / 255.
    #
    # x_val = x_test[]

    x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
    x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
    return x_train, x_test


def introduce_noise(x_train, x_test, noise_factor=0.5):
    x_train_noisy = x_train + noise_factor * np.random.normal(
        loc=0.0, scale=1.0, size=x_train.shape
    )
    x_test_noisy = x_test + noise_factor * np.random.normal(
        loc=0.0, scale=1.0, size=x_test.shape
    )
    x_train_noisy = np.clip(x_train_noisy, 0., 1.)
    x_test_noisy = np.clip(x_test_noisy, 0., 1.)

    return x_train_noisy, x_test_noisy


def main():
    # load the mnist data set and introduce some noise.
    x_train, x_test = load_mnist()
    x_train_noisy, x_test_noisy = introduce_noise(x_train, x_test)

    # autoencoder part
    input_img = Input(shape=(28, 28, 1))
    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(input_img)
    x = MaxPooling2D((2, 2), border_mode='same')(x)
    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x)
    encoded = MaxPooling2D((2, 2), border_mode='same')(x)

    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(encoded)
    x = UpSampling2D((2, 2))(x)
    x = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(x)
    x = UpSampling2D((2, 2))(x)
    decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x)

    autoencoder = Model(input_img, decoded)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
    autoencoder.fit(x_train_noisy, x_train,
                    nb_epoch=1,
                    batch_size=128,
                    shuffle=True,
                    validation_split=1/12.,
                    callbacks=[TensorBoard(log_dir='/tmp/tb', histogram_freq=0,
                                           write_graph=False)])

    # decode (i.e. denoise) the images
    decoded_imgs = autoencoder.predict(x_test_noisy)
    n = 11
    plt.figure(figsize=(20, 4))
    for i in range(1, n):
        # display original
        ax = plt.subplot(2, n, i)
        plt.imshow(x_test_noisy[100 + i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

        # display reconstruction
        ax = plt.subplot(2, n, i + n)
        plt.imshow(decoded_imgs[100 + i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.show()

if __name__ == '__main__':
    main()
<file_sep>THEANO_FLAGS=device=gpu,floatX=float32 python3 vae_multi_view_faces.py
<file_sep>import numpy as np
from sklearn.neural_network import BernoulliRBM
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt



def load_data():
    """
    load training data and normalize
    """
    digits = load_digits()
    data = np.asarray(digits.data, dtype='float32')
    training_data = data / np.max(data)
    return training_data


def build_model(training_data):
    """
    build and train the rbm.
    """
    rbm = BernoulliRBM(random_state=0, verbose=True, n_components=100,
                       n_iter=50)
    rbm.fit(training_data)
    return rbm


def main():
    rbm = build_model(load_data())
    print(rbm.components_[0].shape)
    # visualize
    plt.figure(figsize=(5, 4.5))
    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()


if __name__ == '__main__':
    main()
<file_sep># unsupervised_deep_learning
Examples and implementations of unsupervised learning techniques used in the context of deep learning.
<file_sep>import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras import backend as K
from keras import objectives
from sklearn.cross_validation import train_test_split

from multi_view_faces import load_data


batch_size = 10
original_dim = 64 * 64
latent_dim = 2
intermediate_dim = 1024
nb_epoch = 8
epsilon_std = 1.0

x = Input(batch_shape=(batch_size, original_dim))
h = Dense(intermediate_dim, activation='relu')(x)
z_mean = Dense(latent_dim)(h)
z_log_var = Dense(latent_dim)(h)


def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
                              std=epsilon_std)
    return z_mean + K.exp(z_log_var / 2) * epsilon

z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

decoder_h = Dense(intermediate_dim, activation='relu')
decoder_mean = Dense(original_dim, activation='sigmoid')
h_decoded = decoder_h(z)
x_decoded_mean = decoder_mean(h_decoded)


def vae_loss(x, x_decoded_mean):
    xent_loss = original_dim * objectives.binary_crossentropy(x, x_decoded_mean)
    kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
    return xent_loss + kl_loss

vae = Model(x, x_decoded_mean)
vae.compile(optimizer='rmsprop', loss=vae_loss)

faces = load_data()

print(type(faces))
x_train, x_test = train_test_split(faces, test_size=0.1)
print(x_train)

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train[:4100]
x_test = x_test[:450]

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:])))


vae.fit(x_train, x_train,
        shuffle=True,
        nb_epoch=nb_epoch,
        batch_size=batch_size,
        validation_data=(x_test, x_test))

# build a model to project inputs on the latent space
encoder = Model(x, z_mean)


# sample from the learned distribution
decoder_input = Input(shape=(latent_dim,))
_h_decoded = decoder_h(decoder_input)
_x_decoded_mean = decoder_mean(_h_decoded)
generator = Model(decoder_input, _x_decoded_mean)



n = 15
face_size = 64
figure = np.zeros((face_size * n, face_size * n))
grid_x = norm.ppf(np.linspace(0.01, 0.99, n))
grid_y = norm.ppf(np.linspace(0.01, 0.99, n))

for i, yi in enumerate(grid_x):
    for j, xi in enumerate(grid_y):
        z_sample = np.array([[xi, yi]])
        x_decoded = generator.predict(z_sample)
        face = x_decoded[0].reshape(face_size, face_size)
        figure[i * face_size: (i + 1) * face_size,
               j * face_size: (j + 1) * face_size] = face

plt.figure(figsize=(10, 10))
plt.imshow(figure, cmap='Greys_r')
plt.show()

