title: How to serialize Keras models to use with Joblib?
body: <p>I'm trying to combine Keras and Joblib in order to generate multiple simple models and store them in an array so that I can project probe samples afterwards at the validation stage.</p>
  
  <p>I have this implementation of a Bootstrap Aggregating (<a href="https://github.com/rafaelvareto/HPLS-HFCN-openset/blob/feature/fully-connected-net/python/HFCN_openset_load.py" rel="nofollow noreferrer">Bagging</a>) method with several simple binary neural network models using Joblib. However, I've come across the following error as I try to predict:</p>
  
  <pre><code>Traceback (most recent call last):
  File "../HFCN_openset_load.py", line 264, in &lt;module&gt;
  main()
  File "../HFCN_openset_load.py", line 107, in main
  pr, roc = fcnhface(args, parallel_pool)
  File "../HFCN_openset_load.py", line 194, in fcnhface
  pred = models[k][0].predict(feature_vector.reshape(1, feature_vector.shape[0]))
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 1004, in predict
  if not self.built:
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 339, in built
  return self._built
  AttributeError: 'Sequential' object has no attribute '_built'
  </code></pre>
  
  <p>Below you'll find parts of my code where I believe the error may be at:</p>
  
  <pre><code>def getModel(input_shape,nclasses=2):
      make_keras_picklable()
      model = Sequential()
      model.add(Dense(64, activation='relu', input_shape=input_shape))
      model.add(Dropout(0.2))
      model.add(Dense(nclasses, activation='softmax'))
      model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])#RMSprop()
      return model
  
  def learn_fc_model(X, Y, split):
      boolean_label = [(split[key]+1)/2 for key in Y]
      y_train = np_utils.to_categorical(boolean_label, 2)
      model = getModel(input_shape=X[0].shape)
      model.fit(X, y_train, batch_size=40, nb_epoch=100, verbose=0)
      return (model, split)
  
  #Training using Joblib, models is a list of tuples (ANN models, any variable)
  with Parallel(n_jobs=4, verbose=15, backend='multiprocessing') as parallel_pool:
      models = parallel_pool(
          delayed(learn_fc_model) (numpy_x, numpy_y, split) for split in numpy_s
      )
  
  #Testing
  for k in range (0, len(models)):
      pred = models[k][0].predict(feature_vector.reshape(1, feature_vector.shape[0]))
  </code></pre>
  
  <p><strong>The link to the complete file is right <a href="https://github.com/rafaelvareto/HPLS-HFCN-openset/blob/feature/fully-connected-net/python/HFCN_openset_load.py" rel="nofollow noreferrer">here</a></strong></p>