title: Stacked Autoencoder for classification using mnist
body: <p>Am aware that container for autoencoder has been removed in new Keras. My aim is to extract the encoding representation of an input and feed it in as an input to the next layer i.e. stacked autoencoder for classification using three hidden layers. I got this error: </p>
  
  <p>Exception: Error when checking model target: expected dense_160 to have shape (None, 500) but got array with shape (60000L, 784L). I am not too sure if this code achieves my intention.  I will appreciate any guide on how to resolve this. Thanks.
  The code is pasted below: </p>
  
  <pre><code>from keras.layers import Input, Dense
  from keras.models import Model
  from keras.datasets import mnist
  import numpy as np
  
  nb_classes = 10
  nb_epoch=200
  batch_size=256
  hidden_layer1=784
  hidden_layer2=600
  hidden_layer3=500
  (x_train, y_train), (x_test, y_test) = mnist.load_data()
  x_train = x_train.astype('float32') / 255.
  x_test = x_test.astype('float32') / 255.
  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:])))
  print('Train samples: {}'.format(x_train.shape[0]))
  print('Test samples: {}'.format(x_test.shape[0]))
  
  from keras.utils import np_utils
  y_train = np_utils.to_categorical(y_train, nb_classes)
  y_test = np_utils.to_categorical(y_test, nb_classes)
  
  input_img = Input(shape=(784,))
  encoded = Dense(hidden_layer1, activation='relu')(input_img)
  encoded = Dense(hidden_layer2, activation='relu')(encoded)
  encoded=Dense(hidden_layer3,activation='softmax')(encoded)
  model = Model(input=input_img , output=encoded) 
  model.compile(optimizer='rmsprop',
            loss='categorical_crossentropy',
            metrics=['accuracy'])
  
  model.fit(x_train, x_train,
              nb_epoch=nb_epoch,
              batch_size=batch_size,
              shuffle=True,
              validation_data=(x_test, x_test))
  score = model.evaluate(x_test, y_test, verbose=0)
  
  print('Test score before fine turning:', score[0])
  print('Test accuracy after fine turning:', score[1])
  </code></pre>