In [1]:
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers import Convolution2D, MaxPooling2D,UpSampling2D
from keras.utils import np_utils
from joblib import Parallel, delayed
import matplotlib.pyplot as plt
#Seed for reproducibilty
np.random.seed(1338)


Using Theano backend.

In [2]:
%%time
#Loading the training and testing data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
img_rows, img_cols = 28, 28
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255


CPU times: user 1.69 s, sys: 148 ms, total: 1.84 s
Wall time: 1.84 s

In [3]:
%%time
#Selecting 6000 random examples from the test data
test_rows = np.random.randint(0,X_test.shape[0],6000)
X_test = X_test[test_rows]

#Selecting the 5918 examples where the output is 6
X_six = X_train[y_train == 6]
#Selecting the examples where the output is not 6
X_not_six = X_train[y_train != 6]

#Selecting 6000 random examples from the data that contains only the data where the output is not 6
random_rows = np.random.randint(0,X_six.shape[0],6000)
X_not_six = X_not_six[random_rows]


CPU times: user 52 ms, sys: 20 ms, total: 72 ms
Wall time: 74.7 ms

In [4]:
%%time
#Appending the data with output as 6 and data with output as not six
X_train = np.append(X_six,X_not_six)
#Reshaping the appended data to appropraite form
X_train = X_train.reshape(X_six.shape[0] + X_not_six.shape[0], 1, img_rows, img_cols)


CPU times: user 8 ms, sys: 4 ms, total: 12 ms
Wall time: 14.7 ms

In [5]:
%%time
#Input Dimensions
input_img = Input(shape=(1, 28, 28))
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(input_img)
encoded = MaxPooling2D((2, 2), border_mode='same')(x)

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

autoencoder = Model(input_img, decoded)


CPU times: user 816 ms, sys: 72 ms, total: 888 ms
Wall time: 1.02 s

In [6]:
%%time
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')


CPU times: user 40 ms, sys: 0 ns, total: 40 ms
Wall time: 37.7 ms

In [7]:
%%time
autoencoder.fit(X_train, X_train,
                nb_epoch=15,
                batch_size=128,
                shuffle=True,
                validation_data=(X_test, X_test))


Train on 11918 samples, validate on 6000 samples
Epoch 1/15
11918/11918 [==============================] - 25s - loss: 0.1682 - val_loss: 0.0860
Epoch 2/15
11918/11918 [==============================] - 18s - loss: 0.0807 - val_loss: 0.0775
Epoch 3/15
11918/11918 [==============================] - 14s - loss: 0.0750 - val_loss: 0.0726
Epoch 4/15
11918/11918 [==============================] - 14s - loss: 0.0728 - val_loss: 0.0703
Epoch 5/15
11918/11918 [==============================] - 13s - loss: 0.0718 - val_loss: 0.0694
Epoch 6/15
11918/11918 [==============================] - 15s - loss: 0.0709 - val_loss: 0.0695
Epoch 7/15
11918/11918 [==============================] - 15s - loss: 0.0705 - val_loss: 0.0703
Epoch 8/15
11918/11918 [==============================] - 14s - loss: 0.0700 - val_loss: 0.0689
Epoch 9/15
11918/11918 [==============================] - 14s - loss: 0.0698 - val_loss: 0.0685
Epoch 10/15
 4608/11918 [==========>...................] - ETA: 7s - loss: 0.0692
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-a56384ac61c7> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', 'autoencoder.fit(X_train, X_train,\n                nb_epoch=15,\n                batch_size=128,\n                shuffle=True,\n                validation_data=(X_test, X_test))')

/home/prajwal/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2118             magic_arg_s = self.var_expand(line, stack_depth)
   2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
   2121             return result
   2122 

<decorator-gen-60> in time(self, line, cell, local_ns)

/home/prajwal/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/prajwal/anaconda3/lib/python3.5/site-packages/IPython/core/magics/execution.py in time(self, line, cell, local_ns)
   1171         if mode=='eval':
   1172             st = clock2()
-> 1173             out = eval(code, glob, local_ns)
   1174             end = clock2()
   1175         else:

<timed eval> in <module>()

/home/prajwal/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight)
   1044                               verbose=verbose, callbacks=callbacks,
   1045                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
-> 1046                               callback_metrics=callback_metrics)
   1047 
   1048     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/home/prajwal/anaconda3/lib/python3.5/site-packages/keras/engine/training.py in _fit_loop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics)
    782                 batch_logs['size'] = len(batch_ids)
    783                 callbacks.on_batch_begin(batch_index, batch_logs)
--> 784                 outs = f(ins_batch)
    785                 if type(outs) != list:
    786                     outs = [outs]

/home/prajwal/anaconda3/lib/python3.5/site-packages/keras/backend/theano_backend.py in __call__(self, inputs)
    505     def __call__(self, inputs):
    506         assert type(inputs) in {list, tuple}
--> 507         return self.function(*inputs)
    508 
    509 

/home/prajwal/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    860         try:
    861             outputs =\
--> 862                 self.fn() if output_subset is None else\
    863                 self.fn(output_subset=output_subset)
    864         except Exception:

KeyboardInterrupt: 

In [ ]:
%%time
decoded_imgs = autoencoder.predict(X_test)

In [ ]:
%%time
# use Matplotlib (don't ask)
import matplotlib.pyplot as plt

n = 10  # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(X_test[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 + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

In [ ]: