In [1]:
import keras
import matplotlib.pyplot as plt
(Xtrain, ytrain),(Xtest,ytest) = keras.datasets.mnist.load_data()
model = keras.models.Sequential()
Xtrain = Xtrain.reshape(60000, 28*28)
ytrain = keras.utils.to_categorical(ytrain, 10)

model.add(keras.layers.Dense(128, input_dim=28*28, activation='elu'))
model.add(keras.layers.Dense(64, activation='elu'))
model.add(keras.layers.Dense(32, activation='elu'))
model.add(keras.layers.Dense(16, activation='elu', name='encoded'))
model.add(keras.layers.Dense(32, activation='elu'))
model.add(keras.layers.Dense(64, activation='elu'))
model.add(keras.layers.Dense(128, activation='elu'))
model.add(keras.layers.Dense(28*28, activation='elu'))
model.compile(loss='mse',optimizer='adam')
model.summary()

model.fit(Xtrain, Xtrain)

y = model.predict(Xtest.reshape(10000,28*28))
fig, ax = plt.subplots(1,2)
ax[0].imshow(Xtest[0].reshape(28,28), cmap='gray')
ax[1].imshow(y[0].reshape(28,28), cmap='gray')
plt.show()


Using TensorFlow backend.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_2 (Dense)              (None, 64)                8256      
_________________________________________________________________
dense_3 (Dense)              (None, 32)                2080      
_________________________________________________________________
encoded (Dense)              (None, 16)                528       
_________________________________________________________________
dense_4 (Dense)              (None, 32)                544       
_________________________________________________________________
dense_5 (Dense)              (None, 64)                2112      
_________________________________________________________________
dense_6 (Dense)              (None, 128)               8320      
_________________________________________________________________
dense_7 (Dense)              (None, 784)               101136    
=================================================================
Total params: 223,456
Trainable params: 223,456
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
60000/60000 [==============================] - 11s - loss: 2006.9240    
Epoch 2/10
60000/60000 [==============================] - 10s - loss: 1379.0430    
Epoch 3/10
60000/60000 [==============================] - 10s - loss: 1249.1457    
Epoch 4/10
60000/60000 [==============================] - 11s - loss: 1183.7958    
Epoch 5/10
60000/60000 [==============================] - 11s - loss: 1143.1354    
Epoch 6/10
60000/60000 [==============================] - 12s - loss: 1114.4017    
Epoch 7/10
60000/60000 [==============================] - 8s - loss: 1094.0596     
Epoch 8/10
60000/60000 [==============================] - 8s - loss: 1076.7818     
Epoch 9/10
60000/60000 [==============================] - 9s - loss: 1062.2477     
Epoch 10/10
60000/60000 [==============================] - 8s - loss: 1049.8796     

In [2]:
intermediate = keras.models.Model(inputs=model.input, outputs=model.get_layer('encoded').output)
intermediate_output = intermediate.predict(Xtest.reshape(10000,28*28))

In [3]:
fig, ax = plt.subplots(2,2)
ax[0][0].imshow(intermediate_output[0].reshape(4,4), cmap='gray')
ax[0][1].imshow(intermediate_output[1].reshape(4,4), cmap='gray')
ax[1][0].imshow(intermediate_output[2].reshape(4,4), cmap='gray')
ax[1][1].imshow(intermediate_output[3].reshape(4,4), cmap='gray')
plt.show()



In [ ]: