title: keras: cnn+lstm model using time distributed layer runtime error
body: <p>I am using cnn with lstm model using time distributed layer for image classification. Although I have compiled the model, still it shows </p>
  
  <pre><code>RuntimeError: You must compile your model before using it.
  </code></pre>
  
  <p>I searched on multiple sites but I cannot find solution to my problem.
  Here is my code:</p>
  
  <pre><code>from keras.models import Sequential
  from keras.layers import Convolution2D
  from keras.layers import MaxPooling2D
  from keras.layers import Flatten
  from keras.layers import Dense
  from keras.layers import Dropout
  from keras.layers import TimeDistributed
  from keras.layers import LSTM
  
  import warnings
  warnings.filterwarnings('ignore')
  
  # Initialising the CNN
  classifier = Sequential()
  
  # Step 1 - Convolution
  classifier.add(TimeDistributed(Convolution2D(32, (3, 3), padding = 'same', input_shape = (128, 128, 3), 
                                               activation = 'relu')))
  
  # Step 2 - 
  classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
  
  # Adding a second convolutional layer
  classifier.add(TimeDistributed(Convolution2D(64, (3, 3), padding = 'same', activation = 'relu')))
  classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
  
  # Adding a third conolutional layer
  classifier.add(TimeDistributed(Convolution2D(64, (3, 3), padding = 'same', activation = 'relu')))
  classifier.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
  
  # Step 3 - Flattening
  classifier.add(TimeDistributed(Flatten()))
  classifier.add(Dropout(rate = 0.5))
  
  # Step 4 - Full connection
  classifier.add(LSTM(256, return_sequences=False, dropout=0.5))
  classifier.add(Dense(output_dim = 8, activation = 'softmax'))
  
  # Compiling the CNN
  classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
  
  # Part 2 - Fitting the CNN to the images
  
  from keras.preprocessing.image import ImageDataGenerator
  
  train_datagen = ImageDataGenerator(rescale = 1./255,
                                     shear_range = 0.2,
                                     zoom_range = 0.2,
                                     height_shift_range =  0.1,
                                     width_shift_range = 0.1,
                                     channel_shift_range = 10)
  
  test_datagen = ImageDataGenerator(rescale = 1./255)
  
  training_set = train_datagen.flow_from_directory('dataset/mel/train/',
                                                   target_size = (128, 128),
                                                   batch_size = 32,
                                                   class_mode = 'categorical')
  
  test_set = test_datagen.flow_from_directory('dataset/mel/test/',
                                              target_size = (128, 128),
                                              batch_size = 32,
                                              class_mode = 'categorical')
  
  classifier.fit_generator(training_set,
                           samples_per_epoch = 1088,
                           nb_epoch = 1,
                           validation_data = test_set,
                           nb_val_samples = 352)
  </code></pre>
  
  <p>Here is the complete output message:</p>
  
  <pre><code>Found 1088 images belonging to 8 classes.
  Found 352 images belonging to 8 classes.
  ---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-8-6a3839aea8f8&gt; in &lt;module&gt;()
       81                          nb_epoch = 1,
       82                          validation_data = test_set,
  ---&gt; 83                          nb_val_samples = 352)
  
  ~/.local/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
       89                 warnings.warn('Update your `' + object_name +
       90                               '` call to the Keras 2 API: ' + signature, stacklevel=2)
  ---&gt; 91             return func(*args, **kwargs)
       92         wrapper._original_function = func
       93         return wrapper
  
  ~/.local/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
     1424             use_multiprocessing=use_multiprocessing,
     1425             shuffle=shuffle,
  -&gt; 1426             initial_epoch=initial_epoch)
     1427 
     1428     @interfaces.legacy_generator_methods_support
  
  ~/.local/lib/python3.5/site-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
       35 
       36     do_validation = bool(validation_data)
  ---&gt; 37     model._make_train_function()
       38     if do_validation:
       39         model._make_test_function()
  
  ~/.local/lib/python3.5/site-packages/keras/engine/training.py in _make_train_function(self)
      482     def _make_train_function(self):
      483         if not hasattr(self, 'train_function'):
  --&gt; 484             raise RuntimeError('You must compile your model before using it.')
      485         self._check_trainable_weights_consistency()
      486         if self.train_function is None:
  
  RuntimeError: You must compile your model before using it.
  </code></pre>
  
  <p>What can be the possible mistakes.
  Thanks</p>