title: How to save the best hyperopt optimized keras models and its weights?
body: <p>I optimized my keras model using hyperopt. Now how do we save the best optimized keras model and its weights to disk. </p>
  
  <p>My code:</p>
  
  <pre><code>from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
  from sklearn.metrics import roc_auc_score
  import sys
  
  X = []
  y = []
  X_val = []
  y_val = []
  
  space = {'choice': hp.choice('num_layers',
                      [ {'layers':'two', },
                      {'layers':'three',
                      'units3': hp.uniform('units3', 64,1024), 
                      'dropout3': hp.uniform('dropout3', .25,.75)}
                      ]),
  
              'units1': hp.choice('units1', [64,1024]),
              'units2': hp.choice('units2', [64,1024]),
  
              'dropout1': hp.uniform('dropout1', .25,.75),
              'dropout2': hp.uniform('dropout2',  .25,.75),
  
              'batch_size' : hp.uniform('batch_size', 20,100),
  
              'nb_epochs' :  100,
              'optimizer': hp.choice('optimizer',['adadelta','adam','rmsprop']),
              'activation': 'relu'
          }
  
  def f_nn(params):   
      from keras.models import Sequential
      from keras.layers.core import Dense, Dropout, Activation
      from keras.optimizers import Adadelta, Adam, rmsprop
  
      print ('Params testing: ', params)
      model = Sequential()
      model.add(Dense(output_dim=params['units1'], input_dim = X.shape[1])) 
      model.add(Activation(params['activation']))
      model.add(Dropout(params['dropout1']))
  
      model.add(Dense(output_dim=params['units2'], init = "glorot_uniform")) 
      model.add(Activation(params['activation']))
      model.add(Dropout(params['dropout2']))
  
      if params['choice']['layers']== 'three':
          model.add(Dense(output_dim=params['choice']['units3'], init = "glorot_uniform")) 
          model.add(Activation(params['activation']))
          model.add(Dropout(params['choice']['dropout3']))    
  
      model.add(Dense(1))
      model.add(Activation('sigmoid'))
      model.compile(loss='binary_crossentropy', optimizer=params['optimizer'])
  
      model.fit(X, y, nb_epoch=params['nb_epochs'], batch_size=params['batch_size'], verbose = 0)
  
      pred_auc =model.predict_proba(X_val, batch_size = 128, verbose = 0)
      acc = roc_auc_score(y_val, pred_auc)
      print('AUC:', acc)
      sys.stdout.flush() 
      return {'loss': -acc, 'status': STATUS_OK}
  
  
  trials = Trials()
  best = fmin(f_nn, space, algo=tpe.suggest, max_evals=100, trials=trials)
  print 'best: '
  print best
  </code></pre>