Imports


In [2]:
%matplotlib inline

import seaborn as sns
import numpy as np

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Flatten, Dropout, Reshape
from keras.layers.recurrent import LSTM
from keras.layers import Convolution2D, MaxPooling2D

Constants


In [3]:
img_rows, img_cols = 28, 28
in_shape = (img_rows, img_cols, 1)
batch_size = 256
nb_epoch = 3

Data


In [4]:
X_train = np.random.rand(10000, 28, 28, 1)
Y_train = np.random.rand(10000, 1)
X_test = np.random.rand(1000, 28, 28, 1)

In [5]:
sns.heatmap(X_train[0].reshape(28,28), cmap='gray')


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f5c59371890>

Train Data


In [6]:
X_train = np.random.rand(10000, 28, 28, 1)
sns.heatmap(X_train[0].reshape(28, 28), cmap='gray')


Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f5c530e9050>

In [7]:
Y_train = np.random.rand(10000, 1)
Y_train[0]


Out[7]:
array([ 0.61000587])

Test Data


In [8]:
X_test = np.random.rand(1024, 28, 28, 1)

Model


In [9]:
model = Sequential()
model.add(Convolution2D(32, 3, 3, border_mode='same', activation='relu', input_shape=in_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(128, 3, 3, border_mode='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())

In [10]:
model.add(Reshape((1, -1)))
model.add(LSTM(100)) #return_sequences=True
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()


____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
convolution2d_1 (Convolution2D)  (None, 28, 28, 32)    320         convolution2d_input_1[0][0]      
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 14, 14, 32)    0           convolution2d_1[0][0]            
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 14, 14, 64)    18496       maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 7, 7, 64)      0           convolution2d_2[0][0]            
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 7, 7, 128)     73856       maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 3, 3, 128)     0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
flatten_1 (Flatten)              (None, 1152)          0           maxpooling2d_3[0][0]             
____________________________________________________________________________________________________
reshape_1 (Reshape)              (None, 1, 1152)       0           flatten_1[0][0]                  
____________________________________________________________________________________________________
lstm_1 (LSTM)                    (None, 100)           501200      reshape_1[0][0]                  
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 1)             101         lstm_1[0][0]                     
====================================================================================================
Total params: 593,973
Trainable params: 593,973
Non-trainable params: 0
____________________________________________________________________________________________________

Train model

I think it is just learning to output .5 to minimize loss as we are using completely random training data


In [11]:
model.fit(X_train, Y_train, # specify training data
          batch_size=1, # use this many images per mini-batch - memory dependent - 256
          nb_epoch=1, # go through my training data this number of times - 3
          shuffle=False,
          verbose=True # please print things 
         )


Epoch 1/1
10000/10000 [==============================] - 179s - loss: 0.0864   
Out[11]:
<keras.callbacks.History at 0x7f5c57eb3850>

Predictions


In [13]:
predictions = model.predict(X_test)

In [14]:
predictions[0]


Out[14]:
array([ 0.47382611], dtype=float32)

In [ ]:
predictions.mean()

In [ ]:
Y_train.mean()

In [ ]: