Keras offers the possibility to define custom evaluation metrics --I am interested in variations of the F metric, e.g. F1, F2 etc which are provided by scikit learn-- but instructs us to do so by invoking Keras backend functions which are limited in that respect.
My aim is to use these metrics in conjunction with the Early-Stopping method of Keras. So I should find a method to integrate the metric with the learning process of the Keras Model. (Of course outside the learning/fitting process I can simply invoke Scikit-Learn with the results).
What are my options here?
Update
Having implemented Aaron's solution with titanic_all_numeric dataset from Kaggle, I get the following:
# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy', f1])
# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)
Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 642us/step - loss: 0.8037 - acc: 0.6132 - f1: 0.6132 - val_loss: 0.5815 - val_acc: 0.7537 - val_f1: 0.7537
# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)
Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 658us/step - loss: 0.8148 - acc: 0.6404 - val_loss: 0.7056 - val_acc: 0.7313
# Compile the model
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = [f1])
# Fit the model
hist = model.fit(predictors, target, validation_split = 0.3)
Train on 623 samples, validate on 268 samples
Epoch 1/1
623/623 [==============================] - 0s 690us/step - loss: 0.6554 - f1: 0.6709 - val_loss: 0.5107 - val_f1: 0.7612
I am wondering if these results are fine. For once, the accuracy and f1 score are the same.