Git Product home page Git Product logo

Comments (6)

jjallaire avatar jjallaire commented on June 27, 2024 1

Actually you might be able to do it by converting to R vectors and back using the K$eval() and K$constant() functions. For example (haven't tried this!):

metric_custom <- function( y_true, y_pred ) {
    # convert tensors to R objects
    K <- backend()
    y_true <- K$eval(y_true)
    y_pred <- K$eval(y_pred)
     
    # calculate the metric
    metric <- mean( y_true[ y_pred >= quantile(y_pred, .985) ] ) 

    # convert to tensor
    K$constant(metric)
}

If this works as expected for you I'll add this as an example to the documentation. Let me know!

from keras.

deanmckee007 avatar deanmckee007 commented on June 27, 2024 1

Hey, thanks for looking at it again.

Compiling with your function above produced an error:
InvalidArgumentError: Shape [-1,-1] has negative dimensions

Edit - is your last line valid? Coercing a scalar to a tensor?

from keras.

jjallaire avatar jjallaire commented on June 27, 2024

Custom metrics operate on tensors so you need to build these expressions using backend tensor functions. For example:

K <- backend()
mean_pred <- function(y_true, y_pred) {
  K$mean(y_pred) 
}

Docs on available back end functions are here: https://rstudio.github.io/keras/articles/backend.html

Note that even once you get this working the name of the metric won't show up properly in training progress (it will just say "python_function". Working on a change now to remedy this.

from keras.

jjallaire avatar jjallaire commented on June 27, 2024

I've added the ability to provide names for custom metrics and enhanced the docs a bit: file:///Users/jjallaire/packages/keras/docs/reference/metric_binary_accuracy.html#custom-metrics

from keras.

deanmckee007 avatar deanmckee007 commented on June 27, 2024

Yeah, I don't see that I'll be able to do it within those confines, BUT, to anyone else who might stumble on this thread - you can always Run an epoch, manually predict on validation, check your metric of interest, loop. Since prediction is slow with keras/TF it'll add a bit of time, but it's worth it in my case.

from keras.

jjallaire avatar jjallaire commented on June 27, 2024

Yes, you can indeed convert a scalar to a tensor, but perhaps Keras is expecting a different type? (there are other arguments to K$constant which can provide type/shape/etc

from keras.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.