#%% Setup.
from collections import namedtuple

import numpy as np

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import Nadam
from keras.layers.advanced_activations import PReLU
from keras.utils import np_utils
from keras.utils.visualize_util import plot
from keras import backend as K
from keras.callbacks import TensorBoard, ModelCheckpoint

from eva.models.pixelcnn import PixelCNN
from eva.models.gated_pixelcnn import GatedPixelCNN

Data = namedtuple('Data', 'x y')

nb_classes = 10
img_rows, img_cols = 28, 28

nb_filters = 128
blocks = 1

batch_size = 128
nb_epoch = 40

def clean_data(x, y, rows, cols):
    if K.image_dim_ordering() == 'th':
        x = x.reshape(x.shape[0], 1, rows, cols)
        input_shape = (1, rows, cols)
    else:
        x = x.reshape(x.shape[0], rows, cols, 1)
        input_shape = (rows, cols, 1)

    x = x.astype('float32') / 255

    y = np_utils.to_categorical(y, nb_classes)

    print('X shape:', x.shape)
    print(x.shape[0], 'samples')

    return x, y

def get_data(rows, cols):
    return [Data(*clean_data(*data, rows, cols)) for data in mnist.load_data()]

train, test = get_data(img_rows, img_cols)
data = np.concatenate((train.x, test.x))

data = np.repeat(data, 3, -1)

model = GatedPixelCNN(data.shape[1:], nb_filters, blocks)

model.summary()

plot(model)

#%% Train.
# model.fit(data, data,
#           batch_size=batch_size, nb_epoch=nb_epoch,
#           verbose=1, callbacks=[TensorBoard(), ModelCheckpoint('model.h5')])

model.fit(data,
          [(np.expand_dims(data[:, :, :, 0].reshape(data.shape[0], data.shape[1]*data.shape[2]), -1)*255).astype(int),
           (np.expand_dims(data[:, :, :, 1].reshape(data.shape[0], data.shape[1]*data.shape[2]), -1)*255).astype(int),
           (np.expand_dims(data[:, :, :, 2].reshape(data.shape[0], data.shape[1]*data.shape[2]), -1)*255).astype(int)],
          batch_size=batch_size, nb_epoch=nb_epoch,
          verbose=1, callbacks=[ModelCheckpoint('model.h5', save_weights_only=True)]) # Only weights because Keras is a bitch.

@@ -607,415 +607,116 @@
 NN%0A%0A
-Data = namedtuple('Data', 'x y')%0A%0Anb_classes = 10%0Aimg_rows, img_cols = 28, 28%0A%0Anb_filters = 128%0Ablocks = 1%0A%0Abatch_size = 128%0Anb_epoch = 40%0A%0Adef clean_data(x, y, rows, cols):%0A    if K.image_dim_ordering() == 'th':%0A        x = x.reshape(x.shape%5B0%5D, 1, rows, cols)%0A        input_shape = (1, rows, cols)%0A    else:%0A        x = x.reshape(x.shape%5B0%5D, rows, cols, 1)%0A        input_shape = (rows, cols, 1)%0A%0A    x = x
+#%25%25 Data.%0A(train, _), (test, _) = mnist.load_data()%0Adata = np.concatenate((train, test), axis=0)%0Adata = data
 .ast
@@ -733,369 +733,126 @@
 32')
+%0Adata
  /
+=
  255%0A
-%0A    y = np_utils.to_categorical(y, nb_classes)%0A%0A    print('X shape:', x.shape)%0A    print(x.shape%5B0%5D, 'samples')%0A%0A    return x, y%0A%0Adef get_data(rows, cols):%0A    return %5BData(*clean_data(*data, rows, cols)) for data in mnist.load_data()%5D%0A%0Atrain, test = get_data(img_rows, img_cols)%0Adata = np.concatenate((train.x, test.x))%0A%0Adata = np.repeat(data, 3
+data = np.repeat(np.expand_dims(data, -1), 3, -1)%0A%0A#%25%25 Model.%0A# model = PixelCNN(data.shape%5B1:%5D, 126
 , 
--
 1)%0A
-%0A
 mode
@@ -889,26 +889,14 @@
 :%5D, 
-nb_filters, blocks
+126, 1
 )%0A%0Am
@@ -939,166 +939,8 @@
 in.%0A
-# model.fit(data, data,%0A#           batch_size=batch_size, nb_epoch=nb_epoch,%0A#           verbose=1, callbacks=%5BTensorBoard(), ModelCheckpoint('model.h5')%5D)%0A%0A
 mode
@@ -1330,26 +1330,18 @@
 ch_size=
-batch_size
+32
 , nb_epo
@@ -1343,24 +1343,18 @@
 b_epoch=
-nb_epoch
+40
 ,%0A      
@@ -1379,16 +1379,31 @@
 lbacks=%5B
+TensorBoard(), 
 ModelChe

Clean and improve MNIST