code: def _train_and_eval_single(train, valid, model,
                             batch_size=32, epochs=300, use_weight=False,
                             callbacks=[], eval_best=False, add_eval_metrics={}):
      
      def _format_keras_history(history):
          """nicely format keras history
          """
          return {"params": history.params,
                  "loss": merge_dicts({"epoch": history.epoch}, history.history),
                  }
      if use_weight:
          sample_weight = train[2]
      else:
          sample_weight = None
      # train the model
      logger.info("Fit...")
      history = History()
      model.fit(train[0], train[1],
                batch_size=batch_size,
                validation_data=valid[:2],
                epochs=epochs,
                sample_weight=sample_weight,
                verbose=2,
                callbacks=[history] + callbacks)
  
      # get history
      hist = _format_keras_history(history)
      # load and eval the best model
      if eval_best:
          mcp = [x for x in callbacks if isinstance(x, ModelCheckpoint)]
          assert len(mcp) == 1
          model = load_model(mcp[0].filepath)
  
      return eval_model(model, valid, add_eval_metrics), hist
docs: Fit and evaluate a keras model
  
      eval_best: if True, load the checkpointed model for evaluation