def keras_fit(model, X_train, Y_train, X_test, Y_test, batch_size=128,
              nb_classes=None, epochs=12, fLOG=None):
    """
    Fits a :epkg:`keras` model.

    @param      model       :epkg:`keras` model
    @param      X_train     training features
    @param      Y_train     training target
    @param      X_test      test features
    @param      Y_test      test target
    @param      batch_size  batch size
    @param      nb_classes  nb_classes
    @param      epochs      number of iterations
    @param      fLOG        logging function
    @return                 model
    """
    # numpy.random.seed(1337)  # for reproducibility

    if nb_classes is None:
        nb_classes = Y_train.shape[1]
        if fLOG:
            fLOG("[keras_fit] nb_classes=%d" % nb_classes)
    try:
        model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
                  verbose=1, validation_data=(X_test, Y_test))
    except Exception:  # pylint: disable=W0703
        model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=epochs,
                  verbose=1, validation_data=(X_test, Y_test))
    return model