title: How to save Scikit-Learn-Keras Model into a Persistence File (pickle/hd5/json/yaml)
body: <p>I have the following code, using <a href="https://github.com/fchollet/keras/blob/master/keras/wrappers/scikit_learn.py">Keras Scikit-Learn Wrapper</a>:</p>
  
  <pre><code>from keras.models import Sequential
  from sklearn import datasets
  from keras.layers import Dense
  from sklearn.model_selection import train_test_split
  from keras.wrappers.scikit_learn import KerasClassifier
  from sklearn.model_selection import StratifiedKFold
  from sklearn.model_selection import cross_val_score
  from sklearn import preprocessing
  import pickle
  import numpy as np
  import json
  
  def classifier(X, y):
      """
      Description of classifier
      """
      NOF_ROW, NOF_COL =  X.shape
  
      def create_model():
          # create model
          model = Sequential()
          model.add(Dense(12, input_dim=NOF_COL, init='uniform', activation='relu'))
          model.add(Dense(6, init='uniform', activation='relu'))
          model.add(Dense(1, init='uniform', activation='sigmoid'))
          # Compile model
          model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
          return model
  
      # evaluate using 10-fold cross validation
      seed = 7
      np.random.seed(seed)
      model = KerasClassifier(build_fn=create_model, nb_epoch=150, batch_size=10, verbose=0)
      return model
  
  
  def main():
      """
      Description of main
      """
  
      iris = datasets.load_iris()
      X, y = iris.data, iris.target
      X = preprocessing.scale(X)
  
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=0)
      model_tt = classifier(X_train, y_train)
      model_tt.fit(X_train,y_train)
  
      #--------------------------------------------------
      # This fail
      #-------------------------------------------------- 
      filename = 'finalized_model.sav'
      pickle.dump(model_tt, open(filename, 'wb'))
      # load the model from disk
      loaded_model = pickle.load(open(filename, 'rb'))
      result = loaded_model.score(X_test, Y_test)
      print(result)
  
      #--------------------------------------------------
      # This also fail
      #--------------------------------------------------
      # from keras.models import load_model       
      # model_tt.save('test_model.h5')
  
  
      #--------------------------------------------------
      # This works OK 
      #-------------------------------------------------- 
      # print model_tt.score(X_test, y_test)
      # print model_tt.predict_proba(X_test)
      # print model_tt.predict(X_test)
  
  
  # Output of predict_proba
  # 2nd column is the probability that the prediction is 1
  # this value is used as final score, which can be used
  # with other method as comparison
  # [   [ 0.25311464  0.74688536]
  #     [ 0.84401423  0.15598579]
  #     [ 0.96047372  0.03952631]
  #     ...,
  #     [ 0.25518912  0.74481088]
  #     [ 0.91467732  0.08532269]
  #     [ 0.25473493  0.74526507]]
  
  # Output of predict
  # [[1]
  # [0]
  # [0]
  # ...,
  # [1]
  # [0]
  # [1]]
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>As stated in the code there it fails at this line:</p>
  
  <pre><code>pickle.dump(model_tt, open(filename, 'wb'))
  </code></pre>
  
  <p>With this error:</p>
  
  <pre><code>pickle.PicklingError: Can't pickle &lt;function create_model at 0x101c09320&gt;: it's not found as __main__.create_model
  </code></pre>
  
  <p>How can I get around it?</p>