How to define a custom metric function in R for Keras? I just started using keras and would like to use unweighted kappa as a metric when compiling my model. Following the instructions from here, I tried to define my custom metric as follows:

library(DescTools)  # includes function to calculate kappa
library(keras)

metric_kappa &lt;- function(y_true, y_pred) {
   CohenKappa(y_true, y_pred)
} 

model %&gt;% compile(
   loss = 'categorical_crossentropy',
   optimizer = optimizer_rmsprop(),
   metrics = metric_kappa
)


However, there must be something wrong with my definition since I get a segmentation fault. Any idea on how to solve this and how to properly set up a custom metric would be highly appreciated.
This answers suggests a wrapper function . Does that work in your usecase ? 

https://stackoverflow.com/a/51415066/655855