rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
831 stars 282 forks source link

matthews_correlation(y_true, y_pred) #587

Closed saveriofrancini closed 5 years ago

saveriofrancini commented 5 years ago

Is the matthews_correlation metric available?

model %>% compile(
  loss = 'kld',
  optimizer = optimizer_rmsprop(),
  metrics = c ( 'matthews_correlation' )
)
 Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: Unknown metric function:matthews_correlation 

Thanks in advance

turgut090 commented 5 years ago

Hi, info from official site:

screen shot 2018-11-04 at 11 48 20
saveriofrancini commented 5 years ago

Thank you.

I asked couse I read this:

https://faroit.github.io/keras-docs/1.2.2/metrics/

Il giorno dom 4 nov 2018 alle ore 08:49 Turqut notifications@github.com ha scritto:

Hi, info from official site: https://keras.rstudio.com/reference/index.html#sts=Metrics [image: screen shot 2018-11-04 at 11 48 20] https://user-images.githubusercontent.com/38523699/47961596-94d2f600-e027-11e8-89bd-d299dbe724bb.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/587#issuecomment-435649533, or mute the thread https://github.com/notifications/unsubscribe-auth/Aj_OU_7fMJt7_zHlzP_5YsQ6YQhP8umhks5urpwFgaJpZM4YM77C .

turgut090 commented 5 years ago

@saveriofrancini maybe it was in Keras version 1.2.2 Right now it is not given as available metrics on keras.io

saveriofrancini commented 5 years ago

Do you know if is possible use it as custom metric?

I made a lot of attempts but I wasn't able to solve this problem. I mandatorily need this metric, or precision, couse I have a lot o true negatives and only some true positives.

Il giorno dom 4 nov 2018 alle ore 10:29 Turqut notifications@github.com ha scritto:

maybe it was available only for Keras 1.2.2 For current version it is not given as an available metric on keras.io https://keras.io/metrics/#available-metrics

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/587#issuecomment-435654633, or mute the thread https://github.com/notifications/unsubscribe-auth/Aj_OU7VtpAPgDl2isoh5fd7TuQkBhKk-ks5urrNjgaJpZM4YM77C .

turgut090 commented 5 years ago

According to this post, I think you can use the following custom metric:

metric_matthews_pred <- custom_metric("matthews_cor", function(y_true, y_pred) { y_pred_pos = k_round(k_clip(y_pred, 0, 1)) y_pred_neg = 1 - y_pred_pos

y_pos = k_round(k_clip(y_true, 0, 1)) y_neg = 1 - y_pos

tp = k_sum(y_pos y_pred_pos) tn = k_sum(y_neg y_pred_neg)

fp = k_sum(y_neg y_pred_pos) fn = k_sum(y_pos y_pred_neg)

numerator = (tp tn - fp fn) denominator = k_sqrt((tp + fp) (tp + fn) (tn + fp) * (tn + fn)) cor = numerator / (denominator + k_epsilon()) cor } )

model %>% compile( optimizer = optimizer_rmsprop(), loss = loss_binary_crossentropy, metrics = c( metric_matthews_pred) )

saveriofrancini commented 5 years ago

It works.

Thank you very much

Il giorno dom 4 nov 2018 alle ore 12:24 Turqut notifications@github.com ha scritto:

According to this post https://stackoverflow.com/questions/39895742/matthews-correlation-coefficient-with-keras, I think you can use the following custom metric:

metric_matthews_pred <- custom_metric("matthews_cor", function(y_true, y_pred) { y_pred_pos = k_round(k_clip(y_pred, 0, 1)) y_pred_neg = 1 - y_pred_pos

y_pos = k_round(k_clip(y_true, 0, 1)) y_neg = 1 - y_pos

tp = k_sum(y_pos y_pred_pos) tn = k_sum(y_neg y_pred_neg)

fp = k_sum(y_neg y_pred_pos) fn = k_sum(y_pos y_pred_neg)

numerator = (tp tn - fp fn) denominator = k_sqrt((tp + fp) (tp + fn) (tn + fp) * (tn + fn)) cor = numerator / (denominator + k_epsilon()) cor } )

model %>% compile( optimizer = optimizer_rmsprop(), loss = loss_binary_crossentropy, metrics = c( metric_matthews_pred) )

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rstudio/keras/issues/587#issuecomment-435661481, or mute the thread https://github.com/notifications/unsubscribe-auth/Aj_OU6SUvubU1jQNk7m8S6vqwBNyEuFeks5urs6IgaJpZM4YM77C .